Skip to content

Commit

Permalink
chore: appease linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Jun 13, 2024
1 parent 7fc32a7 commit 70b87fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
11 changes: 7 additions & 4 deletions packages/w3up-client/src/proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CarReader } from '@ipld/car'
import * as Link from 'multiformats/link'
import { base64 } from 'multiformats/bases/base64'
import { identity } from 'multiformats/hashes/identity'

/**
* Parses a base64 encoded CIDv1 CAR of proofs (delegations).
*
Expand All @@ -17,12 +17,15 @@ export const parse = async (str) => {
throw new Error(`non CAR codec found: 0x${cid.code.toString(16)}`)
}
if (cid.multihash.code !== identity.code) {
throw new Error(`non identity multihash: 0x${cid.multihash.code.toString(16)}`)
throw new Error(
`non identity multihash: 0x${cid.multihash.code.toString(16)}`
)
}

try {
const { ok, error } = await extract(cid.multihash.digest)
if (error) throw new Error('failed to extract delegation', { cause: error })
if (error)
throw new Error('failed to extract delegation', { cause: error })
return ok
} catch {
// Before `delegation.archive()` we used `delegation.export()` to create
Expand All @@ -36,7 +39,7 @@ export const parse = async (str) => {
}
}

/**
/**
* Reads a plain CAR file, assuming the last block is the delegation root.
*
* @param {Uint8Array} bytes
Expand Down
33 changes: 22 additions & 11 deletions packages/w3up-client/test/proof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export const testProof = {
const delegation = await delegate({
issuer: alice,
audience: bob,
capabilities: [{ can: 'test/thing', with: alice.did() }]
capabilities: [{ can: 'test/thing', with: alice.did() }],
})

const bytes = Result.unwrap(await delegation.archive())
const str = Link.create(CAR.codec.code, identity.digest(bytes)).toString(base64)
const str = Link.create(CAR.codec.code, identity.digest(bytes)).toString(
base64
)

const proof = await parse(str)
assert.equal(proof.issuer.did(), delegation.issuer.did())
Expand All @@ -38,7 +40,7 @@ export const testProof = {
const delegation = await delegate({
issuer: alice,
audience: bob,
capabilities: [{ can: 'test/thing', with: alice.did() }]
capabilities: [{ can: 'test/thing', with: alice.did() }],
})

const bytes = Result.unwrap(await delegation.archive())
Expand All @@ -53,22 +55,27 @@ export const testProof = {
const delegation = await delegate({
issuer: alice,
audience: bob,
capabilities: [{ can: 'test/thing', with: alice.did() }]
capabilities: [{ can: 'test/thing', with: alice.did() }],
})

const bytes = Result.unwrap(await delegation.archive())
const str = Link.create(CAR.codec.code, await sha256.digest(bytes)).toString(base64)
const str = Link.create(
CAR.codec.code,
await sha256.digest(bytes)
).toString(base64)

await assert.rejects(parse(str))
},

'should parse a base64 encoded CIDv1 "proof" as plain CAR (legacy)': async (assert) => {
'should parse a base64 encoded CIDv1 "proof" as plain CAR (legacy)': async (
assert
) => {
const alice = await Signer.generate()
const bob = await Signer.generate()
const delegation = await delegate({
issuer: alice,
audience: bob,
capabilities: [{ can: 'test/thing', with: alice.did() }]
capabilities: [{ can: 'test/thing', with: alice.did() }],
})

const blocks = new Map()
Expand All @@ -77,7 +84,9 @@ export const testProof = {
}

const bytes = CAR.codec.encode({ blocks })
const str = Link.create(CAR.codec.code, identity.digest(bytes)).toString(base64)
const str = Link.create(CAR.codec.code, identity.digest(bytes)).toString(
base64
)

const proof = await parse(str)
assert.equal(proof.issuer.did(), delegation.issuer.did())
Expand All @@ -86,13 +95,15 @@ export const testProof = {
assert.equal(proof.capabilities[0].with, delegation.capabilities[0].with)
},

'should parse a base64 encoded "proof" as plain CAR (legacy)': async (assert) => {
'should parse a base64 encoded "proof" as plain CAR (legacy)': async (
assert
) => {
const alice = await Signer.generate()
const bob = await Signer.generate()
const delegation = await delegate({
issuer: alice,
audience: bob,
capabilities: [{ can: 'test/thing', with: alice.did() }]
capabilities: [{ can: 'test/thing', with: alice.did() }],
})

const blocks = new Map()
Expand All @@ -108,7 +119,7 @@ export const testProof = {
assert.equal(proof.audience.did(), delegation.audience.did())
assert.equal(proof.capabilities[0].can, delegation.capabilities[0].can)
assert.equal(proof.capabilities[0].with, delegation.capabilities[0].with)
}
},
}

Test.test({ Proof: testProof })

0 comments on commit 70b87fe

Please sign in to comment.