Last active
November 7, 2024 22:18
-
-
Save alanshaw/c1a3508311f015cc670db5f471e7b904 to your computer and use it in GitHub Desktop.
Delegations for the Storacha Network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node mkdelegate.js <private-key> | |
import { delegate } from '@ucanto/core' | |
import * as ed25519 from '@ucanto/principal/ed25519' | |
import * as Link from 'multiformats/link' | |
import { identity } from 'multiformats/hashes/identity' | |
import { base64 } from 'multiformats/bases/base64' | |
import * as DID from '@ipld/dag-ucan/did' | |
const indexingServiceDID = 'did:web:staging.indexer.storacha.network' | |
const uploadServiceDID = 'did:web:staging.upload.storacha.network' | |
const storageProviderDID = 'did:key:...' | |
const delegateIndexingServiceToUploadService = async () => { | |
const issuer = ed25519.parse(process.argv[2]).withDID(indexingServiceDID) | |
const audience = DID.parse(uploadServiceDID) | |
const abilities = ['assert/equals', 'assert/index'] | |
const delegation = await delegate({ | |
issuer, | |
audience, | |
capabilities: abilities.map(can => ({ can, with: issuer.did() })), | |
expiration: Infinity | |
}) | |
console.log(await formatDelegation(delegation)) | |
} | |
// delegateIndexingServiceToUploadService() | |
const delegateStorageProviderToUploadService = async () => { | |
const issuer = ed25519.parse(process.argv[2]) | |
const audience = DID.parse(uploadServiceDID) | |
const abilities = ['blob/allocate', 'blob/accept'] | |
const delegation = await delegate({ | |
issuer, | |
audience, | |
capabilities: abilities.map(can => ({ can, with: issuer.did() })), | |
expiration: Infinity | |
}) | |
console.log(await formatDelegation(delegation)) | |
} | |
// delegateStorageProviderToUploadService() | |
const delegateIndexingServiceToStorageProvider = async () => { | |
const issuer = ed25519.parse(process.argv[2]).withDID(indexingServiceDID) | |
const audience = DID.parse(storageProviderDID) | |
const abilities = ['claim/cache'] | |
const delegation = await delegate({ | |
issuer, | |
audience, | |
capabilities: abilities.map(can => ({ can, with: issuer.did() })), | |
expiration: Infinity | |
}) | |
console.log(await formatDelegation(delegation)) | |
} | |
// delegateIndexingServiceToStorageProvider() | |
/** @param {import('@ucanto/interface').Delegation} */ | |
const formatDelegation = async delegation => { | |
const { ok: archive, error } = await delegation.archive() | |
if (error) throw error | |
const digest = identity.digest(archive) | |
const link = Link.create(0x0202, digest) | |
return link.toString(base64) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment