|
| 1 | +/** |
| 2 | + * Create a "ucan/attest" delegation allowing the gateway to validate |
| 3 | + * attestations issued by the upload-service. |
| 4 | + * |
| 5 | + * This generates the GATEWAY_VALIDATOR_PROOF environment variable value. |
| 6 | + * |
| 7 | + * Usage: node scripts/mk-validator-proof.js <upload-service-did-web> <upload-service-private-key> <gateway-did-web> |
| 8 | + * |
| 9 | + * Example (staging): |
| 10 | + * node scripts/mk-validator-proof.js \ |
| 11 | + * did:web:staging.up.storacha.network \ |
| 12 | + * MgCZT5J+...your-key-here... \ |
| 13 | + * did:web:staging.w3s.link |
| 14 | + * |
| 15 | + * Example (production): |
| 16 | + * node scripts/mk-validator-proof.js \ |
| 17 | + * did:web:up.storacha.network \ |
| 18 | + * MgCZT5J+...your-key-here... \ |
| 19 | + * did:web:w3s.link |
| 20 | + */ |
| 21 | +import * as DID from '@ipld/dag-ucan/did' |
| 22 | +import { CAR, delegate } from '@ucanto/core' |
| 23 | +import * as ed25519 from '@ucanto/principal/ed25519' |
| 24 | +import { base64 } from 'multiformats/bases/base64' |
| 25 | +import { identity } from 'multiformats/hashes/identity' |
| 26 | +import * as Link from 'multiformats/link' |
| 27 | + |
| 28 | +// CORRECT DIRECTION (staging): |
| 29 | +// - issuer should be did:web:staging.up.storacha.network (upload-service) |
| 30 | +// - audience should be did:web:staging.w3s.link (gateway) |
| 31 | +// - can should be 'ucan/attest' |
| 32 | +// - with should be issuer.did() (i.e. did:web:staging.up.storacha.network) |
| 33 | +// The private key must be the upload-service private key. This makes the |
| 34 | +// gateway trust attestations issued by the upload-service. |
| 35 | + |
| 36 | +const uploadServiceDIDWeb = process.argv[2] |
| 37 | +const uploadServicePrivateKey = process.argv[3] |
| 38 | +const gatewayDIDWeb = process.argv[4] |
| 39 | + |
| 40 | +if (!uploadServiceDIDWeb || !uploadServicePrivateKey || !gatewayDIDWeb) { |
| 41 | + console.error('Error: Missing required arguments') |
| 42 | + console.error('Usage: node scripts/mk-validator-proof.js <upload-service-did-web> <upload-service-private-key> <gateway-did-web>') |
| 43 | + console.error('') |
| 44 | + console.error('Example (staging):') |
| 45 | + console.error(' node scripts/mk-validator-proof.js \\') |
| 46 | + console.error(' did:web:staging.up.storacha.network \\') |
| 47 | + console.error(' MgCZT5J+...your-key-here... \\') |
| 48 | + console.error(' did:web:staging.w3s.link') |
| 49 | + process.exit(1) |
| 50 | +} |
| 51 | + |
| 52 | +console.log(`Upload Service DID: ${uploadServiceDIDWeb}`) |
| 53 | +console.log(`Upload Service Private Key: ${uploadServicePrivateKey.slice(0, 7)}...${uploadServicePrivateKey.slice(-7)}`) |
| 54 | +console.log(`Gateway DID: ${gatewayDIDWeb}`) |
| 55 | +console.log('') |
| 56 | + |
| 57 | +const issuer = ed25519 |
| 58 | + .parse(uploadServicePrivateKey) |
| 59 | + .withDID(DID.parse(uploadServiceDIDWeb).did()) |
| 60 | +const audience = DID.parse(gatewayDIDWeb) |
| 61 | + |
| 62 | +// Note: variable names are confusing - "uploadService" is actually the issuer (gateway in our case) |
| 63 | +// and "gateway" is actually the audience (upload service in our case) |
| 64 | +// The 'with' should be the issuer's DID per colleague's instructions |
| 65 | +const delegation = await delegate({ |
| 66 | + issuer, |
| 67 | + audience, |
| 68 | + capabilities: [{ can: 'ucan/attest', with: issuer.did() }], |
| 69 | + expiration: Infinity |
| 70 | +}) |
| 71 | + |
| 72 | +console.log('✅ Delegation created:') |
| 73 | +console.log(` Issuer: ${issuer.did()}`) |
| 74 | +console.log(` Audience: ${audience.did()}`) |
| 75 | +console.log(` Capability: ucan/attest with ${issuer.did()}`) |
| 76 | +console.log('') |
| 77 | + |
| 78 | +const res = await delegation.archive() |
| 79 | +if (res.error) { |
| 80 | + console.error('❌ Error archiving delegation:', res.error) |
| 81 | + throw res.error |
| 82 | +} |
| 83 | + |
| 84 | +const proof = Link.create(CAR.code, identity.digest(res.ok)).toString(base64) |
| 85 | + |
| 86 | +console.log('✅ Validator proof generated successfully!') |
| 87 | +console.log('') |
| 88 | +console.log('Add this to your environment variables:') |
| 89 | +console.log('') |
| 90 | +console.log('GATEWAY_VALIDATOR_PROOF=' + proof) |
| 91 | +console.log('') |
0 commit comments