Skip to content

Commit

Permalink
fix: fixed test to resolve did key
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Nov 1, 2024
1 parent a0bbd9e commit 4549961
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 55 deletions.
6 changes: 2 additions & 4 deletions packages/capabilities/src/usage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { DID, capability, ok, Schema } from '@ucanto/validator'
import { and, equal, equalWith, SpaceDID } from './utils.js'

export const ProviderDID = DID.match({ method: 'web' })
import { capability, DID, ok, Schema } from '@ucanto/validator'
import { and, equal, equalWith, ProviderDID, SpaceDID } from './utils.js'

/**
* Capability can only be delegated (but not invoked) allowing audience to
Expand Down
50 changes: 14 additions & 36 deletions packages/capabilities/test/capabilities/usage.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert'
import { access } from '@ucanto/validator'
import { access, Schema } from '@ucanto/validator'
import { Verifier } from '@ucanto/principal'
import * as Usage from '../../src/usage.js'
import * as Capability from '../../src/top.js'
Expand Down Expand Up @@ -204,60 +204,38 @@ describe('usage capabilities', function () {
}, /Expected value of type integer instead got 6\.6/)
})

it('usage/record should fail to be derived from *', async () => {
const data = {
space: mallory.did(),
resource: readmeCID,
bytes: 100,
servedAt: 1714204800,
}
const record = Usage.record.invoke({
issuer: alice,
audience: w3,
with: gateway.did(),
nb: { ...data },
proofs: [await top()],
})

const result = await access(await record.delegate(), {
capability: Usage.record,
principal: Verifier,
authority: w3,
validateAuthorization,
})

assert.ok(result.error, 'Expected an error but none was found')
})

it('usage/record can be derived from usage/record', async () => {
it('should delegate and invoke usage/record', async () => {
const data = {
space: mallory.did(),
resource: readmeCID,
bytes: 100,
servedAt: 1714204800,
}

// W3 delegates ability to record usage to Gateway
const usageRecordDelegationProof = await Usage.record.delegate({
issuer: alice,
audience: bob,
with: gateway.did(),
nb: { ...data },
proofs: [await top()],
issuer: w3,
audience: gateway,
with: w3.did(),
expiration: Infinity,
})

const record = Usage.record.invoke({
issuer: bob,
// Gateway invokes usage/record and indicates the w3 as the audience
const recordInvocation = Usage.record.invoke({
issuer: gateway,
audience: w3,
with: gateway.did(),
nb: { ...data },
proofs: [usageRecordDelegationProof],
})

const result = await access(await record.delegate(), {
// W3 validates the delegation from Gateway to itself
const result = await access(await recordInvocation.delegate(), {
capability: Usage.record,
principal: Verifier,
authority: w3,
validateAuthorization,
resolveDIDKey: () => Schema.ok(gateway.toDIDKey()),
})

if (result.error) {
Expand All @@ -266,6 +244,6 @@ describe('usage capabilities', function () {

assert.deepEqual(result.ok.audience.did(), w3.did())
assert.equal(result.ok.capability.can, 'usage/record')
assert.deepEqual(result.ok.capability.nb, { data })
assert.deepEqual(result.ok.capability.nb, { ...data })
})
})
8 changes: 4 additions & 4 deletions packages/capabilities/test/helpers/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseLink } from '@ucanto/core'
import { DID, parseLink } from '@ucanto/core'
import { Absentee } from '@ucanto/principal'
import { Signer } from '@ucanto/principal/ed25519'

Expand Down Expand Up @@ -37,6 +37,6 @@ export const readmeCID = parseLink(
'bafybeihqfdg2ereoijjoyrqzr2x2wsasqm2udurforw7pa3tvbnxhojao4'
)

export const gateway = Absentee.from({
id: 'did:web:freeway.storacha.network:gateway',
})
export const gateway = Signer.parse(
'MgCaNpGXCEX0+BxxE4SjSStrxU9Ru/Im+HGNQ/JJx3lDoI+0B3NWjWW3G8OzjbazZjanjM3kgfcZbvpyxv20jHtmcTtg=' // random key
).withDID('did:web:dag.haus:freeway.com:test')
13 changes: 8 additions & 5 deletions packages/w3up-client/src/capability/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ export class UsageClient extends Base {
* Required delegated capabilities:
* - `usage/record`
*
* @param {import('../types.js').SpaceDID} space
* @param {object} egressData
* @param {import('../types.js').SpaceDID} egressData.space
* @param {API.UnknownLink} egressData.resource
* @param {number} egressData.bytes
* @param {string} egressData.servedAt
* @param {API.ProviderDID} provider
* @param {object} [options]
* @param {string} [options.nonce]
*/
async record(space, egressData, options) {
async record(egressData, provider, options) {
const out = await record(
{ agent: this.agent },
{ space, ...egressData },
{ provider, ...egressData },
{ ...options }
)
/* c8 ignore next 5 */
Expand Down Expand Up @@ -98,6 +99,7 @@ export const report = async (
*
* @param {{agent: API.Agent}} client
* @param {object} egressData
* @param {API.ProviderDID} egressData.provider
* @param {API.SpaceDID} egressData.space
* @param {API.UnknownLink} egressData.resource
* @param {number} egressData.bytes
Expand All @@ -109,14 +111,15 @@ export const report = async (
*/
export const record = async (
{ agent },
{ space, resource, bytes, servedAt },
{ provider, space, resource, bytes, servedAt },
{ nonce, proofs = [] }
) => {
const receipt = await agent.invokeAndExecute(UsageCapabilities.record, {
with: space,
with: provider,
proofs,
nonce,
nb: {
space,
resource,
bytes,
servedAt: Math.floor(new Date(servedAt).getTime() / 1000),
Expand Down
19 changes: 13 additions & 6 deletions packages/w3up-client/test/capability/usage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Client } from '../../src/client.js'
import * as Test from '../test.js'
import { receiptsEndpoint } from '../helpers/utils.js'
import { randomCAR } from '../helpers/random.js'
import { Absentee } from '@ucanto/principal'

export const UsageClient = Test.withContext({
report: {
Expand Down Expand Up @@ -74,6 +75,9 @@ export const UsageClient = Test.withContext({
assert,
{ connection, provisionsStorage }
) => {
const gateway = Absentee.from({
id: 'did:web:freeway.storacha.network',
})
const alice = new Client(await AgentData.create(), {
// @ts-ignore
serviceConf: {
Expand All @@ -85,7 +89,6 @@ export const UsageClient = Test.withContext({
const space = await alice.createSpace('test')
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)

// Then we setup a billing for this account
await provisionsStorage.put({
// @ts-expect-error
Expand All @@ -101,11 +104,15 @@ export const UsageClient = Test.withContext({
const result = await alice.capability.upload.get(car.roots[0])
assert.ok(result)

const record = await alice.capability.usage.record(space.did(), {
resource: resource.link(),
bytes: car.size,
servedAt: new Date().toISOString(),
})
const record = await alice.capability.usage.record(
{
space: space.did(),
resource: resource.link(),
bytes: car.size,
servedAt: new Date().toISOString(),
},
gateway.did()
)

assert.ok(record)
},
Expand Down

0 comments on commit 4549961

Please sign in to comment.