Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Created December 13, 2022 17:20
Show Gist options
  • Save vasco-santos/16a54bc6272521c02c3dc805d747bf7b to your computer and use it in GitHub Desktop.
Save vasco-santos/16a54bc6272521c02c3dc805d747bf7b to your computer and use it in GitHub Desktop.
import * as Signer from '@ucanto/principal/ed25519'
import { CAR } from '@ucanto/transport'
import * as CBOR from '@ucanto/transport/cbor'
import * as UcantoClient from '@ucanto/client'
import * as DAGJSON from '@ipld/dag-json'
const uploadService = await Signer.generate()
const alice = await Signer.generate()
const { proof, spaceDid } = await createSpace(alice)
const data = new Uint8Array([11, 22, 34, 44, 55])
const link = await CAR.codec.link(data)
const nb = { link, size: data.byteLength }
const can = 'store/add'
const request = await CAR.encode([
{
issuer: alice,
audience: uploadService,
capabilities: [{
can,
with: spaceDid,
nb
}],
proofs: [proof],
}
])
const car = await CAR.codec.decode(request.body)
console.log('car', car)
const dagCbor = await CBOR.codec.decode(car.roots[0].bytes)
const encodedDagJson = DAGJSON.encode(dagCbor)
const dagJson = DAGJSON.decode(encodedDagJson)
console.log('dagJson', JSON.stringify(dagJson))
/**
* @param {Signer.EdSigner} audience
*/
async function createSpace (audience) {
const space = await Signer.generate()
const spaceDid = space.did()
return {
proof: await UcantoClient.delegate({
issuer: space,
audience,
capabilities: [{ can: '*', with: spaceDid }]
}),
spaceDid
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment