Skip to content

Commit

Permalink
feat: sync encode/decode delegations (#276)
Browse files Browse the repository at this point in the history
- add methods toBytes and fromBytes useful to store as bytes instead of
strings
- `encodeDelegations` is now `delegationsToString``
- `decodeDelegations` is now `stringToDelegations``
- all functions are sync now

Writing delegations to a car uses `@ipld/car/buffer-writer` now to make
it sync, reading from a CAR it's more complicated 🤫.
`ipld/car` does not support sync reading so this PR includes a refactor
of `CarReader` to be sync, this is a temporary solution until i can get
a proper PR to `ipld/car` to add support for sync reading. It's not
trivial to do so because of the way ipld/car code is structured and i
dont want to just add a sync copy/paste of the decoder functions to it.
  • Loading branch information
hugomrdias committed Dec 12, 2022
1 parent e8326ec commit 9d48372
Show file tree
Hide file tree
Showing 24 changed files with 1,319 additions and 420 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"docs": "pnpm run build && typedoc --out docs"
},
"devDependencies": {
"lint-staged": "^13.0.4",
"prettier": "2.8.0",
"lint-staged": "^13.1.0",
"prettier": "2.8.1",
"simple-git-hooks": "^2.8.1",
"typescript": "4.9.3",
"wrangler": "^2.5.0"
"typescript": "4.9.4",
"wrangler": "^2.6.2"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
Expand Down
10 changes: 5 additions & 5 deletions packages/access-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"@types/assert": "^1.5.6",
"@types/git-rev-sync": "^2.0.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.9",
"@types/node": "^18.11.12",
"@types/qrcode": "^1.5.0",
"better-sqlite3": "8.0.0",
"better-sqlite3": "8.0.1",
"buffer": "^6.0.3",
"dotenv": "^16.0.3",
"esbuild": "^0.15.16",
"esbuild": "^0.16.3",
"git-rev-sync": "^3.0.2",
"hd-scripts": "^3.0.2",
"is-subset": "^0.1.1",
Expand All @@ -56,8 +56,8 @@
"process": "^0.11.10",
"readable-stream": "^4.2.0",
"sade": "^1.8.1",
"typescript": "4.9.3",
"wrangler": "^2.5.0"
"typescript": "4.9.4",
"wrangler": "^2.6.2"
},
"eslintConfig": {
"extends": [
Expand Down
6 changes: 3 additions & 3 deletions packages/access-api/src/kvs/spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export class Spaces {
email: capability.nb.identity.replace('mailto:', ''),
agent: invocation.issuer.did(),
metadata: JSON.stringify(invocation.facts[0]),
invocation: await delegationToString(invocation),
invocation: delegationToString(invocation),
// eslint-disable-next-line unicorn/no-null
delegation: !delegation ? null : await delegationToString(delegation),
delegation: !delegation ? null : delegationToString(delegation),
},
})
return { data: result }
Expand Down Expand Up @@ -136,7 +136,7 @@ export class Spaces {
metadata: JSON.parse(r.metadata),
delegation: !r.delegation
? undefined
: await stringToDelegation(
: stringToDelegation(
/** @type {import('@web3-storage/access/types').EncodedDelegation<[import('@web3-storage/access/types').Top]>} */ (
r.delegation
)
Expand Down
2 changes: 1 addition & 1 deletion packages/access-api/src/kvs/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Validations {
async put(ucan) {
const delegation =
/** @type {import('@ucanto/interface').Delegation<T>} */ (
await stringToDelegation(ucan)
stringToDelegation(ucan)
)

await this.kv.put(delegation.audience.did(), ucan, {
Expand Down
4 changes: 2 additions & 2 deletions packages/access-api/src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function service(ctx) {
facts: [metadata],
})

results.push(await delegationToString(del))
results.push(delegationToString(del))
}
}

Expand Down Expand Up @@ -108,7 +108,7 @@ export function service(ctx) {
})
.delegate()

const encoded = await delegationToString(inv)
const encoded = delegationToString(inv)
const url = `${ctx.url.protocol}//${ctx.url.host}/validate-email?ucan=${encoded}&mode=recover`

// For testing
Expand Down
2 changes: 1 addition & 1 deletion packages/access-api/src/service/voucher-claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function voucherClaimProvider(ctx) {
})
.delegate()

const encoded = await delegationToString(inv)
const encoded = delegationToString(inv)
// For testing
if (ctx.config.ENV === 'test') {
return encoded
Expand Down
2 changes: 1 addition & 1 deletion packages/access-api/test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function createSpace(issuer, service, conn, email) {
throw new Error('failed to create space')
}

const delegation = await stringToDelegation(claim)
const delegation = stringToDelegation(claim)
const serviceDelegation = await Voucher.top.delegate({
issuer: space,
audience: service,
Expand Down
10 changes: 5 additions & 5 deletions packages/access-api/test/space-recover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('space-recover', function () {
url.searchParams.get('ucan')
)

const del = await stringToDelegation(encoded)
const del = stringToDelegation(encoded)

assert.deepEqual(del.audience.did(), issuer.did())
assert.deepEqual(del.issuer.did(), service.did())
Expand All @@ -81,7 +81,7 @@ describe('space-recover', function () {
)

assert.ok(recoverEncoded)
const recover = await stringToDelegation(recoverEncoded)
const recover = stringToDelegation(recoverEncoded)
assert.deepEqual(recover.audience.did(), issuer.did())
assert.deepEqual(recover.issuer.did(), service.did())
assert.deepEqual(recover.capabilities[0].can, 'space/recover')
Expand All @@ -105,7 +105,7 @@ describe('space-recover', function () {
)

assert.ok(encoded)
const recover = await stringToDelegation(encoded)
const recover = stringToDelegation(encoded)
assert.deepEqual(recover.audience.did(), issuer.did())
assert.deepEqual(recover.issuer.did(), service.did())
assert.deepEqual(recover.capabilities[0].can, 'space/recover')
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('space-recover', function () {
url.searchParams.get('ucan')
)

const del = await stringToDelegation(encoded)
const del = stringToDelegation(encoded)

assert.deepEqual(del.audience.did(), issuer.did())
assert.deepEqual(del.issuer.did(), service.did())
Expand All @@ -173,7 +173,7 @@ describe('space-recover', function () {
return assert.fail('failed to recover')
}

const spaceDelegation = await stringToDelegation(inv2[0])
const spaceDelegation = stringToDelegation(inv2[0])
assert.deepEqual(spaceDelegation.audience.did(), issuer.did())
assert.deepEqual(spaceDelegation.capabilities[0].can, '*')
assert.deepEqual(spaceDelegation.capabilities[0].with, space.did())
Expand Down
2 changes: 1 addition & 1 deletion packages/access-api/test/voucher-claim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('ucan', function () {
return t.fail(inv.message)
}

const delegation = await stringToDelegation(inv)
const delegation = stringToDelegation(inv)

t.deepEqual(delegation.issuer.did(), service.did())
t.deepEqual(delegation.audience.did(), issuer.did())
Expand Down
4 changes: 2 additions & 2 deletions packages/access-api/test/voucher-redeem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('ucan', function () {
return t.fail(claim.message)
}

const delegation = await stringToDelegation(claim)
const delegation = stringToDelegation(claim)

const redeem = await Voucher.redeem
.invoke({
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('ucan', function () {
return t.fail(claim.message)
}

const delegation = await stringToDelegation(claim)
const delegation = stringToDelegation(claim)

const redeemInv = Voucher.redeem.invoke({
issuer,
Expand Down
7 changes: 5 additions & 2 deletions packages/access-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
],
"dependencies": {
"@ipld/car": "^5.0.1",
"@ipld/dag-cbor": "^8.0.0",
"@ipld/dag-ucan": "^3.0.1",
"@ucanto/client": "^4.0.2",
"@ucanto/core": "^4.0.2",
Expand All @@ -78,14 +79,16 @@
"p-wait-for": "^5.0.0",
"type-fest": "^3.3.0",
"uint8arrays": "^4.0.2",
"varint": "^6.0.0",
"ws": "^8.11.0",
"zod": "^3.19.1"
},
"devDependencies": {
"@types/assert": "^1.5.6",
"@types/inquirer": "^9.0.3",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.10",
"@types/node": "^18.11.12",
"@types/varint": "^6.0.1",
"@types/ws": "^8.5.3",
"@ucanto/server": "^4.0.2",
"assert": "^2.0.0",
Expand All @@ -96,7 +99,7 @@
"p-queue": "^7.3.0",
"playwright-test": "^8.1.1",
"sade": "^1.8.1",
"typescript": "4.9.3",
"typescript": "4.9.4",
"watch": "^1.0.2"
},
"eslintConfig": {
Expand Down
4 changes: 2 additions & 2 deletions packages/access-client/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export class Agent {

const dels = []
for (const del of recoverInv) {
dels.push(await stringToDelegation(del))
dels.push(stringToDelegation(del))
}

return dels
Expand Down Expand Up @@ -483,7 +483,7 @@ export class Agent {
}

if (msg.type === 'delegation') {
const delegation = await stringToDelegation(msg.delegation)
const delegation = stringToDelegation(msg.delegation)
ws.close()
return delegation
}
Expand Down
6 changes: 3 additions & 3 deletions packages/access-client/src/awake/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Signature from '@ipld/dag-ucan/signature'
import { Verifier } from '@ucanto/principal/ed25519'
import { sha256 } from 'multiformats/hashes/sha2'
import * as u8 from 'uint8arrays'
import { decodeDelegations, encodeDelegations } from '../encoding.js'
import { stringToDelegations, delegationsToString } from '../encoding.js'
import * as Messages from './messages.js'

export class Peer {
Expand Down Expand Up @@ -139,7 +139,7 @@ export class Peer {

/** @type {import('./types').LinkResponse} */
const capsRsp = await this.channel.awaitMsg(this.nextdid)
const delegations = await decodeDelegations(capsRsp.msg.delegation)
const delegations = stringToDelegations(capsRsp.msg.delegation)

await this.channel.sendFin(this.nextdid)

Expand Down Expand Up @@ -169,7 +169,7 @@ export class Peer {
name: this.agent.did(),
type: 'device',
},
delegation: await encodeDelegations([d]),
delegation: delegationsToString([d]),
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/access-client/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ prog
abilities,
})

const delString = await delegationToString(delegation)
const delString = delegationToString(delegation)

if (opts.file) {
fs.writeFileSync(path.join(process.cwd(), opts.file), delString, {
Expand Down Expand Up @@ -143,7 +143,7 @@ prog
encoding: 'utf8',
})

await agent.importSpaceFromDelegation(await stringToDelegation(del))
await agent.importSpaceFromDelegation(stringToDelegation(del))
} else {
console.error(`Run "${NAME} setup" first`)
}
Expand Down
Loading

0 comments on commit 9d48372

Please sign in to comment.