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

fix(egress/record): rename capability #1572

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions packages/capabilities/src/usage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { capability, ok, Schema } from '@ucanto/validator'
import { DID, capability, ok, Schema } from '@ucanto/validator'
import { and, equal, equalWith, SpaceDID } from './utils.js'

export const ProviderDID = DID.match({ method: 'web' })

/**
* Capability can only be delegated (but not invoked) allowing audience to
* be derived any `usage/` prefixed capability for the (memory) space identified
Expand Down Expand Up @@ -46,8 +48,10 @@ export const report = capability({
*/
export const record = capability({
can: 'usage/record',
with: SpaceDID,
with: ProviderDID,
nb: Schema.struct({
/** DID of the space where the resource is served from. */
space: SpaceDID,
/** CID of the resource that was served. */
resource: Schema.link(),
/** Amount of bytes served. */
Expand Down
68 changes: 68 additions & 0 deletions packages/capabilities/test/capabilities/usage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
service as w3,
mallory as account,
bob,
gateway,
readmeCID,
mallory,
} from '../helpers/fixtures.js'
import { validateAuthorization } from '../helpers/utils.js'

Expand Down Expand Up @@ -200,4 +203,69 @@ describe('usage capabilities', function () {
})
}, /Expected value of type integer instead got 6\.6/)
})

it('usage/record should fail to be derived from *', async () => {
fforbeck marked this conversation as resolved.
Show resolved Hide resolved
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 () => {
const data = {
space: mallory.did(),
resource: readmeCID,
bytes: 100,
servedAt: 1714204800,
}

const usageRecordDelegationProof = await Usage.record.delegate({
issuer: alice,
audience: bob,
with: gateway.did(),
nb: { ...data },
proofs: [await top()],
})

const record = Usage.record.invoke({
issuer: bob,
audience: w3,
with: gateway.did(),
nb: { ...data },
proofs: [usageRecordDelegationProof],
})

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

if (result.error) {
assert.fail(result.error.message)
}

assert.deepEqual(result.ok.audience.did(), w3.did())
assert.equal(result.ok.capability.can, 'usage/record')
assert.deepEqual(result.ok.capability.nb, { data })
})
})
4 changes: 4 additions & 0 deletions packages/capabilities/test/helpers/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ export const service = Signer.parse(
export const readmeCID = parseLink(
'bafybeihqfdg2ereoijjoyrqzr2x2wsasqm2udurforw7pa3tvbnxhojao4'
)

export const gateway = Absentee.from({
id: 'did:web:freeway.storacha.network:gateway',
})
4 changes: 2 additions & 2 deletions packages/upload-api/src/usage/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const record = async ({ capability, invocation }, context) => {
)
const consumerResponse = await context.provisionsStorage.getConsumer(
provider,
capability.with
capability.nb.space
)
if (consumerResponse.error) {
return consumerResponse
}
const consumer = consumerResponse.ok
const res = await context.usageStorage.record(
// The space which contains the resource that was served.
capability.with,
capability.nb.space,
// The customer that is being billed for the egress traffic.
consumer.customer,
// CID of the resource that was served.
Expand Down
Loading