Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delegation.toJSON #186

Merged
merged 3 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"devDependencies": {
"mocha": "^10.1.0",
"prettier": "2.7.1",
"typescript": "4.8.3"
"typescript": "^4.8.4"
},
"prettier": {
"trailingComma": "es5",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"@ipld/car": "^5.0.0",
"@ipld/dag-cbor": "^8.0.0",
"@ipld/dag-ucan": "^3.0.1",
"@ipld/dag-ucan": "^3.1.1",
"@ucanto/interface": "^4.0.3",
"multiformats": "^10.0.2"
},
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/delegation.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ export class Delegation {
iterate() {
return it(this)
}

/**
* @returns {API.DelegationJSON<this>}
*/
toJSON() {
return /** @type {any} */ ({
...this.data.toJSON(),
'/': this.cid.toString(),
prf: this.proofs.map(proof =>
isDelegation(proof) ? proof : { '/': proof.toString() }
),
})
}
}

/**
Expand Down
251 changes: 251 additions & 0 deletions packages/core/test/delegation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
import { assert, test } from './test.js'
import { Delegation, UCAN, delegate, parseLink } from '../src/lib.js'
import { alice, bob, mallory, service as w3 } from './fixtures.js'
import { base64 } from 'multiformats/bases/base64'
const utf8 = new TextEncoder()

const link = parseLink(
'bafybeid4cy7pj33wuead6zioxdtx3zwalhr6hd572tgqubgmy2ahrmi6vu'
)
/**
* @param {unknown} value
*/
const toJSON = value => JSON.parse(JSON.stringify(value))

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

assert.deepEqual(toJSON(ucan.data), {
v: UCAN.VERSION,
iss: alice.did(),
aud: bob.did(),
att: [
{
can: 'store/add',
with: alice.did(),
},
],
exp: ucan.expiration,
prf: [],
s: { '/': { bytes: base64.baseEncode(ucan.signature) } },
})
})

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

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

assert.deepEqual(toJSON(ucan.data), {
v: UCAN.VERSION,
iss: bob.did(),
aud: mallory.did(),
att: [
{
can: 'store/add',
with: alice.did(),
root: { '/': link.toString() },
},
],
exp: ucan.expiration,
prf: [
{
'/': proof.cid.toString(),
},
],
s: { '/': { bytes: base64.baseEncode(ucan.signature) } },
})
})

test('delegation.data.toJSON with bytes', async () => {
const content = utf8.encode('hello world')
const proof = await Delegation.delegate({
issuer: alice,
audience: bob,
capabilities: [
{
can: 'store/add',
with: alice.did(),
},
],
})

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

assert.deepEqual(toJSON(ucan.data), {
v: UCAN.VERSION,
iss: bob.did(),
aud: mallory.did(),
att: [
{
can: 'store/add',
with: alice.did(),
root: { '/': { bytes: base64.baseEncode(content) } },
},
],
exp: ucan.expiration,
prf: [
{
'/': proof.cid.toString(),
},
],
s: { '/': { bytes: base64.baseEncode(ucan.signature) } },
})
})

test('toJSON delegation', async () => {
const ucan = await delegate({
issuer: alice,
audience: w3,
capabilities: [
{
with: alice.did(),
can: 'test/echo',
nb: {
message: 'data:1',
},
},
],
expiration: Infinity,
})

assert.deepEqual(toJSON(ucan), {
'/': ucan.cid.toString(),
v: ucan.version,
iss: alice.did(),
aud: w3.did(),
att: [
{
nb: {
message: 'data:1',
},
can: 'test/echo',
with: alice.did(),
},
],
exp: null,
prf: [],
s: {
'/': { bytes: base64.baseEncode(ucan.signature) },
},
})
})

test('toJSON delegation chain', async () => {
const proof = await delegate({
issuer: bob,
audience: alice,
capabilities: [
{
with: bob.did(),
can: 'test/echo',
},
],
})

const proof2 = await delegate({
issuer: mallory,
audience: alice,
capabilities: [
{
with: mallory.did(),
can: 'test/echo',
},
],
})

const ucan = await delegate({
issuer: alice,
audience: w3,
capabilities: [
{
with: bob.did(),
can: 'test/echo',
nb: {
message: 'data:hi',
},
},
],
proofs: [proof, proof2.cid],
})

assert.deepEqual(toJSON(ucan), {
'/': ucan.cid.toString(),
v: ucan.version,
iss: alice.did(),
aud: w3.did(),
att: [
{
with: bob.did(),
can: 'test/echo',
nb: {
message: 'data:hi',
},
},
],
exp: ucan.expiration,
prf: [
{
'/': proof.cid.toString(),
iss: bob.did(),
aud: alice.did(),
att: [
{
with: bob.did(),
can: 'test/echo',
},
],
exp: proof.expiration,
v: proof.version,
s: { '/': { bytes: base64.baseEncode(proof.signature) } },
prf: [],
},
{
'/': proof2.cid.toString(),
},
],
s: {
'/': { bytes: base64.baseEncode(ucan.signature) },
},
})
})
2 changes: 1 addition & 1 deletion packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"build": "tsc --build"
},
"dependencies": {
"@ipld/dag-ucan": "^3.0.1",
"@ipld/dag-ucan": "^3.1.1",
"multiformats": "^10.0.2"
},
"devDependencies": {
Expand Down
46 changes: 44 additions & 2 deletions packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ import {
Principal,
MulticodecCode,
SigAlg,
ToJSON,
SignatureJSON,
JSONUnknown,
IntoJSON,
JSONObject,
} from '@ipld/dag-ucan'
import { Link, Block as IPLDBlock } from 'multiformats'
import { Link, UnknownLink, Block as IPLDBlock, ToString } from 'multiformats'
import * as UCAN from '@ipld/dag-ucan'
import {
CanIssue,
Expand Down Expand Up @@ -59,6 +64,10 @@ export type {
MultibaseEncoder,
MulticodecCode,
Principal,
ToJSON,
ToString,
UnknownLink,
JSONUnknown,
}
export * as UCAN from '@ipld/dag-ucan'

Expand Down Expand Up @@ -151,16 +160,49 @@ export interface Delegation<C extends Capabilities = Capabilities> {
issuer: UCAN.Principal
audience: UCAN.Principal
capabilities: C
expiration?: UCAN.UTCUnixTimestamp
expiration: UCAN.UTCUnixTimestamp
notBefore?: UCAN.UTCUnixTimestamp

nonce?: UCAN.Nonce

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

signature: Signature
version: UCAN.Version

toJSON(): DelegationJSON<this>
}

export type DelegationJSON<T extends Delegation = Delegation> = ToJSON<
T,
{
'/': ToString<T['cid']>
v: T['version']
iss: DID
aud: DID
att: ToJSON<
T['capabilities'],
T['capabilities'] &
UCAN.Tuple<{ with: UCAN.Resource; can: UCAN.Ability; nb?: JSONObject }>
>
exp: T['expiration']
nbf?: T['notBefore'] & {}
nnc?: T['nonce'] & {}
fct: ToJSON<T['facts']>
prf: ProofJSON[] & JSONUnknown[]
s: SignatureJSON<T['signature']>
}
>

export type ProofJSON = DelegationJSON | LinkJSON<UCANLink>

export type LinkJSON<T extends UnknownLink = UnknownLink> = ToJSON<
T,
{ '/': ToString<T> }
>

/**
* An Invocation represents a UCAN that can be presented to a service provider to
* invoke or "exercise" a {@link Capability}. You can think of invocations as a
Expand Down
2 changes: 1 addition & 1 deletion packages/principal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"build": "tsc --build"
},
"dependencies": {
"@ipld/dag-ucan": "^3.0.1",
"@ipld/dag-ucan": "^3.1.1",
"@noble/ed25519": "^1.7.1",
"@ucanto/interface": "^4.0.3",
"multiformats": "^10.0.2",
Expand Down
Loading