|
1 | 1 | import sade from 'sade' |
2 | 2 | import { getClient } from '@web3-storage/w3cli/lib.js' |
3 | | -import * as ed25519 from '@ucanto/principal/ed25519' |
4 | 3 | import { Space } from '@web3-storage/capabilities' |
5 | 4 |
|
6 | | -const cli = sade('delegate-serve.js [space] [token]') |
| 5 | +const cli = sade('delegate-serve.js [space] [token] [accountDID] [gatewayDID]') |
7 | 6 |
|
8 | 7 | cli |
| 8 | + .option('--space', 'The space DID to delegate. If not provided, a new space will be created.') |
| 9 | + .option('--token', 'The auth token to use. If not provided, the delegation will not be authenticated.') |
| 10 | + .option('--accountDID', 'The account DID to use when creating a new space.') |
| 11 | + .option('--gatewayDID', 'The gateway DID to use when delegating the space/content/serve capability. Defaults to did:web:staging.w3s.link.') |
9 | 12 | .describe( |
10 | 13 | `Delegates ${Space.contentServe.can} to the Gateway for a test space generated by the script, with an optional auth token. Outputs a base64url string suitable for the stub_delegation query parameter. Pipe the output to pbcopy or similar for the quickest workflow. If the GATEWAY_PRINCIPAL_KEY environment variable is not set, a new key pair will be generated.` |
11 | 14 | ) |
12 | | - .action(async (space, token) => { |
| 15 | + .action(async (space, token, accountDID, gatewayDID, options) => { |
| 16 | + const { space: spaceOption, token: tokenOption, accountDID: accountDIDOption, gatewayDID: gatewayDIDOption } = options |
| 17 | + space = spaceOption || undefined |
| 18 | + token = tokenOption || undefined |
| 19 | + accountDID = accountDIDOption || undefined |
| 20 | + gatewayDID = gatewayDIDOption || 'did:web:staging.w3s.link' |
13 | 21 | const client = await getClient() |
14 | 22 |
|
15 | | - let newSpace |
| 23 | + let spaceDID |
16 | 24 | let proofs = [] |
17 | 25 | if (!space) { |
18 | | - newSpace = await client.createSpace('test') |
| 26 | + const provider = /** @type {`did:web:${string}`} */ (client.defaultProvider()) |
| 27 | + const account = client.accounts()[accountDID] |
| 28 | + const newSpace = await client.agent.createSpace('test') |
| 29 | + const provision = await account.provision(newSpace.did(), { provider }) |
| 30 | + if (provision.error) throw provision.error |
| 31 | + await newSpace.save() |
19 | 32 | const authProof = await newSpace.createAuthorization(client.agent) |
20 | | - await client.addSpace(authProof) |
21 | 33 | proofs = [authProof] |
| 34 | + spaceDID = newSpace.did() |
22 | 35 | } else { |
23 | | - newSpace = space |
| 36 | + client.addSpace(space) |
| 37 | + spaceDID = space |
24 | 38 | proofs = client.proofs([ |
25 | 39 | { |
26 | 40 | can: Space.contentServe.can, |
27 | | - with: newSpace.did(), |
| 41 | + with: spaceDID, |
28 | 42 | } |
29 | 43 | ]) |
30 | 44 | } |
31 | 45 |
|
32 | | - const signer = |
33 | | - process.env.GATEWAY_PRINCIPAL_KEY |
34 | | - ? ed25519.Signer.parse(process.env.GATEWAY_PRINCIPAL_KEY) |
35 | | - : await ed25519.Signer.generate() |
| 46 | + /** @type {import('@ucanto/client').Principal<`did:${string}:${string}`>} */ |
| 47 | + const gatewayIdentity = { |
| 48 | + did: () => gatewayDID, |
| 49 | + } |
36 | 50 |
|
37 | | - const gatewayIdentity = signer.withDID('did:web:w3s.link') |
38 | | - const delegation = await Space.contentServe.delegate({ |
39 | | - issuer: client.agent.issuer, |
40 | | - audience: gatewayIdentity, |
41 | | - with: newSpace.did(), |
| 51 | + // @ts-expect-error - The client still needs to be updated to support the capability type |
| 52 | + const delegation = await client.createDelegation(gatewayIdentity, [Space.contentServe.can], { |
42 | 53 | expiration: Infinity, |
43 | | - proofs |
| 54 | + proofs, |
44 | 55 | }) |
45 | | - |
| 56 | + |
46 | 57 | await client.capability.access.delegate({ |
47 | 58 | delegations: [delegation], |
48 | 59 | }) |
49 | | - |
| 60 | + |
50 | 61 | const carResult = await delegation.archive() |
51 | 62 | if (carResult.error) throw carResult.error |
52 | 63 | const base64Url = Buffer.from(carResult.ok).toString('base64url') |
53 | 64 | process.stdout.write(`Agent Proofs: ${proofs.flatMap(p => p.capabilities).map(c => `${c.can} with ${c.with}`).join('\n')}\n`) |
54 | 65 | process.stdout.write(`Issuer: ${client.agent.issuer.did()}\n`) |
55 | 66 | process.stdout.write(`Audience: ${gatewayIdentity.did()}\n`) |
56 | | - process.stdout.write(`Space: ${newSpace.did()}\n`) |
| 67 | + process.stdout.write(`Space: ${spaceDID}\n`) |
57 | 68 | process.stdout.write(`Token: ${token ?? 'none'}\n`) |
58 | 69 | process.stdout.write(`Delegation: ${delegation.capabilities.map(c => `${c.can} with ${c.with}`).join('\n')}\n`) |
59 | | - process.stdout.write(`Stubs: stub_space=${newSpace.did()}&stub_delegation=${base64Url}&authToken=${token ?? ''}\n`) |
| 70 | + process.stdout.write(`Stubs: stub_space=${spaceDID}&stub_delegation=${base64Url}&authToken=${token ?? ''}\n`) |
60 | 71 | }) |
61 | 72 |
|
62 | 73 | cli.parse(process.argv) |
0 commit comments