Skip to content

Commit

Permalink
fix: toJSON behavior on the ucan.data (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored Dec 15, 2022
1 parent cb1a3bc commit d1ee6b6
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 12 deletions.
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
134 changes: 134 additions & 0 deletions packages/core/test/delegation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { assert, test } from './test.js'
import { Delegation, UCAN, isDelegation, parseLink } from '../src/lib.js'
import { alice, bob, mallory, service } 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) } },
})
})
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
4 changes: 3 additions & 1 deletion packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ 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
}

/**
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
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1ee6b6

Please sign in to comment.