Skip to content

Commit

Permalink
feat: delgation iterate, more errors and types
Browse files Browse the repository at this point in the history
* fix(ucanto): add types and tweaks
* feat(ucanto): delegation iterate, server error

Co-authored-by: Irakli Gozalishvili <[email protected]>
  • Loading branch information
hugomrdias and Gozala authored Jul 28, 2022
1 parent a6ca603 commit 0606168
Show file tree
Hide file tree
Showing 20 changed files with 507 additions and 295 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ dist
.nyc_output
tmp
node_modules

coverage
.pnpm-debug.log
.env
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"typecheck": "pnpm run -r typecheck"
},
"devDependencies": {
"typescript": "4.7.3",
"mocha": "^9.2.2",
"prettier": "2.7.1"
"prettier": "2.7.1",
"typescript": "4.7.4"
},
"prettier": {
"trailingComma": "es5",
Expand Down
14 changes: 7 additions & 7 deletions packages/authority/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
"build": "tsc --build"
},
"dependencies": {
"@ucanto/interface": "^0.6.2",
"@noble/ed25519": "^1.6.1",
"@ipld/dag-ucan": "^1.7.0-beta",
"@noble/ed25519": "^1.6.1",
"@ucanto/interface": "^0.6.2",
"multiformats": "^9.6.4"
},
"devDependencies": {
"@types/mocha": "^9.1.0",
"@types/chai": "^4.3.0",
"mocha": "^9.2.2",
"chai": "^4.3.6",
"playwright-test": "^7.3.0",
"@types/mocha": "^9.1.0",
"c8": "^7.11.0",
"chai": "^4.3.6",
"mocha": "^9.2.2",
"nyc": "^15.1.0",
"typescript": "^4.7.2"
"playwright-test": "^7.3.0",
"typescript": "^4.7.4"
},
"type": "module",
"main": "src/lib.js",
Expand Down
22 changes: 11 additions & 11 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@
"build": "tsc --build"
},
"dependencies": {
"multiformats": "^9.6.4",
"@ucanto/interface": "^0.6.2"
"@ucanto/interface": "^0.6.2",
"multiformats": "^9.6.4"
},
"devDependencies": {
"@types/mocha": "^9.1.0",
"@types/chai": "^4.3.0",
"@types/chai-subset": "^1.3.3",
"mocha": "^9.2.2",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"playwright-test": "^7.3.0",
"@types/mocha": "^9.1.0",
"@ucanto/authority": "^0.4.5",
"@ucanto/core": "^0.5.4",
"@ucanto/transport": "^0.6.3",
"@web-std/fetch": "^4.1.0",
"@web-std/file": "^3.0.2",
"c8": "^7.11.0",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"mocha": "^9.2.2",
"nyc": "^15.1.0",
"typescript": "^4.7.2",
"@ucanto/authority": "^0.4.5",
"@ucanto/transport": "^0.6.3",
"@ucanto/core": "^0.5.4"
"playwright-test": "^7.3.0",
"typescript": "^4.7.4"
},
"type": "module",
"main": "src/lib.js",
Expand Down
18 changes: 9 additions & 9 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@
"test:web": "playwright-test test/*.spec.js --cov && nyc report",
"test:node": "c8 --check-coverage --branches 100 --functions 100 --lines 100 mocha test/*.spec.js",
"test": "npm run test:node",
"coverage": "c8 --reporter=html mocha test/test-*.js && npm_config_yes=true npx st -d coverage -p 8080",
"coverage": "c8 --reporter=html mocha test/*.spec.js && npm_config_yes=true npx st -d coverage -p 8080",
"typecheck": "tsc --build",
"build": "tsc --build"
},
"dependencies": {
"@ipld/car": "^4.1.0",
"@ipld/dag-cbor": "^7.0.1",
"multiformats": "^9.6.4",
"@ipld/dag-ucan": "^1.7.0-beta",
"@ucanto/interface": "^0.6.2"
"@ucanto/interface": "^0.6.2",
"multiformats": "^9.6.4"
},
"devDependencies": {
"@types/mocha": "^9.1.0",
"@types/chai": "^4.3.0",
"@types/chai-subset": "^1.3.3",
"mocha": "^9.2.2",
"@types/mocha": "^9.1.0",
"@ucanto/authority": "^0.4.5",
"c8": "^7.11.0",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"playwright-test": "^8.0.0",
"c8": "^7.11.0",
"mocha": "^9.2.2",
"nyc": "^15.1.0",
"typescript": "^4.7.2",
"@ucanto/authority": "^0.4.5"
"playwright-test": "^8.0.0",
"typescript": "^4.7.4"
},
"type": "module",
"main": "src/lib.js",
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/delegation.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ export class Delegation {
get facts() {
return this.data.facts
}

/**
* Iterate over the proofs
*
* @returns {IterableIterator<API.Delegation>}
*/
iterate() {
return it(this)
}
}

/**
* @param {API.Delegation} delegation
* @returns {IterableIterator<API.Delegation>}
*/
const it = function* (delegation) {
for (const proof of delegation.proofs) {
if (isDelegation(proof)) {
yield* it(proof)
yield proof
}
}
}

const decodeCache = new WeakMap()
Expand Down
59 changes: 59 additions & 0 deletions packages/core/test/lib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,62 @@ test('delegation with several proofs', async () => {

assert.deepEqual(Delegation.import(invocation.export()), invocation)
})

test('delegation iterate over proofs', async () => {
const proof1 = await Delegation.delegate({
issuer: alice,
audience: bob,
capabilities: [
{
can: 'store/add',
with: alice.did(),
},
],
})

const proof2 = await Delegation.delegate({
issuer: service,
audience: mallory,
capabilities: [
{
can: 'identity/prove',
with: 'mailto:[email protected]',
},
],
})

const delegation = await Delegation.delegate({
issuer: bob,
audience: mallory,
capabilities: [
{
can: 'store/add',
with: alice.did(),
},
],
proofs: [proof1],
})

const invocation = await Delegation.delegate({
issuer: mallory,
audience: service,
capabilities: [
{
can: 'store/add',
with: alice.did(),
},
{
can: 'identity/prove',
with: 'mailto:[email protected]',
},
],
proofs: [delegation, proof2],
})

const proofs = []
for (const proof of invocation.iterate()) {
proofs.push(proof.cid)
}

assert.deepEqual(proofs, [proof1.cid, delegation.cid, proof2.cid])
})
2 changes: 1 addition & 1 deletion packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"multiformats": "^9.6.4"
},
"devDependencies": {
"typescript": "^4.7.2"
"typescript": "^4.7.4"
},
"exports": {
".": {
Expand Down
4 changes: 4 additions & 0 deletions packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export interface Delegation<

facts: Fact[]
proofs: Proof[]
iterate(): IterableIterator<Delegation>
}

export interface Invocation<C extends Capability = Capability>
Expand Down Expand Up @@ -359,10 +360,13 @@ export interface Server<T> extends ServerOptions {
* Actual service providing capability handlers.
*/
readonly service: T

readonly catch?: (err: HandlerExecutionError) => void
}

export interface ServerView<T> extends Server<T>, Transport.Channel<T> {
context: InvocationContext
catch: (err: HandlerExecutionError) => void
}

export type Service = Record<
Expand Down
24 changes: 12 additions & 12 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@
"test:web": "playwright-test test/**/*.spec.js --cov && nyc report",
"test:node": "c8 --check-coverage --branches 100 --functions 100 --lines 100 mocha test/**/*.spec.js",
"test": "npm run test:node",
"coverage": "c8 --reporter=html mocha test/test-*.js && npm_config_yes=true npx st -d coverage -p 8080",
"coverage": "c8 --reporter=html mocha test/**/*.spec.js && npm_config_yes=true npx st -d coverage -p 8080",
"typecheck": "tsc --build",
"build": "tsc --build"
},
"dependencies": {
"@ucanto/interface": "^0.6.2",
"@ucanto/core": "^0.5.4",
"@ucanto/interface": "^0.6.2",
"@ucanto/validator": "^0.5.5"
},
"devDependencies": {
"@types/mocha": "^9.1.0",
"@types/chai": "^4.3.0",
"@types/chai-subset": "^1.3.3",
"mocha": "^9.2.2",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"playwright-test": "^7.3.0",
"@types/mocha": "^9.1.0",
"@ucanto/authority": "^0.4.5",
"@ucanto/client": "^0.5.4",
"@ucanto/transport": "^0.6.3",
"@web-std/fetch": "^4.1.0",
"@web-std/file": "^3.0.2",
"c8": "^7.11.0",
"chai": "^4.3.6",
"chai-subset": "^1.6.0",
"mocha": "^9.2.2",
"multiformats": "^9.6.4",
"nyc": "^15.1.0",
"typescript": "^4.7.2",
"@ucanto/client": "^0.5.4",
"@ucanto/transport": "^0.6.3",
"@ucanto/authority": "^0.4.5",
"multiformats": "^9.6.4"
"playwright-test": "^7.3.0",
"typescript": "^4.7.4"
},
"exports": {
".": {
Expand Down
Loading

0 comments on commit 0606168

Please sign in to comment.