From c8f86c988a01ce8b72432978fd01ba7e530cb613 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 21 Nov 2022 18:25:21 +0000 Subject: [PATCH] chore: alan feedback --- packages/access-api/src/kvs/spaces.js | 2 +- packages/access-api/src/service/index.js | 4 ++-- packages/access-api/test/helpers/utils.js | 6 +++--- .../access-api/test/voucher-redeem.test.js | 6 +++--- packages/access-client/src/agent.js | 4 ++-- .../access-client/src/capabilities/space.js | 8 ++++---- .../access-client/src/capabilities/store.js | 6 +++--- .../src/capabilities/{wildcard.js => top.js} | 10 ++++++---- .../access-client/src/capabilities/types.ts | 12 ++++++----- .../access-client/src/capabilities/upload.js | 6 +++--- .../access-client/src/capabilities/utils.js | 17 +++++----------- .../access-client/src/capabilities/voucher.js | 6 +++--- packages/access-client/src/index.js | 2 +- packages/access-client/src/types.ts | 4 ++-- .../access-client/test/awake.node.test.js | 6 +++--- .../test/capabilities/store.test.js | 20 +++++++++---------- packages/upload-client/test/utils.test.js | 4 ++-- 17 files changed, 60 insertions(+), 63 deletions(-) rename packages/access-client/src/capabilities/{wildcard.js => top.js} (52%) diff --git a/packages/access-api/src/kvs/spaces.js b/packages/access-api/src/kvs/spaces.js index a700289af..386ef43db 100644 --- a/packages/access-api/src/kvs/spaces.js +++ b/packages/access-api/src/kvs/spaces.js @@ -110,7 +110,7 @@ export class Spaces { return } - return /** @type {import('@web3-storage/access/types').EncodedDelegation<[import('@web3-storage/access/types').Any]>[]} */ ( + return /** @type {import('@web3-storage/access/types').EncodedDelegation<[import('@web3-storage/access/types').Top]>[]} */ ( r ) } diff --git a/packages/access-api/src/service/index.js b/packages/access-api/src/service/index.js index 5f923de49..94ee126e8 100644 --- a/packages/access-api/src/service/index.js +++ b/packages/access-api/src/service/index.js @@ -8,7 +8,7 @@ import { delegationToString, stringToDelegation, } from '@web3-storage/access/encoding' -import { any } from '@web3-storage/access/capabilities/wildcard' +import { top } from '@web3-storage/access/capabilities/top' /** * @param {import('../bindings').RouteContext} ctx @@ -52,7 +52,7 @@ export function service(ctx) { const results = [] for (const e of encoded) { const proof = await stringToDelegation(e) - const del = await any.delegate({ + const del = await top.delegate({ audience: invocation.issuer, issuer: ctx.signer, with: proof.capabilities[0].with, diff --git a/packages/access-api/test/helpers/utils.js b/packages/access-api/test/helpers/utils.js index 1759ce7a9..7299d6c49 100644 --- a/packages/access-api/test/helpers/utils.js +++ b/packages/access-api/test/helpers/utils.js @@ -2,7 +2,7 @@ import * as UCAN from '@ipld/dag-ucan' // eslint-disable-next-line no-unused-vars import * as Types from '@ucanto/interface' -import * as Any from '@web3-storage/access/capabilities/wildcard' +import * as Top from '@web3-storage/access/capabilities/top' import * as Voucher from '@web3-storage/access/capabilities/voucher' import { stringToDelegation } from '@web3-storage/access/encoding' import { Signer } from '@ucanto/principal/ed25519' @@ -40,7 +40,7 @@ export async function createSpace(issuer, service, conn, email) { service: service.did(), }, proofs: [ - await Any.any.delegate({ + await Top.top.delegate({ issuer: space, audience: issuer, with: space.did(), @@ -67,7 +67,7 @@ export async function createSpace(issuer, service, conn, email) { }, proofs: [ delegation, - await Any.any.delegate({ + await Top.top.delegate({ issuer: space, audience: service, with: space.did(), diff --git a/packages/access-api/test/voucher-redeem.test.js b/packages/access-api/test/voucher-redeem.test.js index 5d4891ab1..b0276803d 100644 --- a/packages/access-api/test/voucher-redeem.test.js +++ b/packages/access-api/test/voucher-redeem.test.js @@ -1,5 +1,5 @@ /* eslint-disable unicorn/prefer-number-properties */ -import * as Any from '@web3-storage/access/capabilities/wildcard' +import * as Top from '@web3-storage/access/capabilities/top' import * as Voucher from '@web3-storage/access/capabilities/voucher' import { stringToDelegation } from '@web3-storage/access/encoding' import { context, test } from './helpers/context.js' @@ -26,7 +26,7 @@ test('should return voucher/redeem', async (t) => { service: service.did(), }, proofs: [ - await Any.any.delegate({ + await Top.top.delegate({ issuer: space, audience: issuer, with: space.did(), @@ -57,7 +57,7 @@ test('should return voucher/redeem', async (t) => { }, proofs: [ delegation, - await Any.any.delegate({ + await Top.top.delegate({ issuer: space, audience: service, with: space.did(), diff --git a/packages/access-client/src/agent.js b/packages/access-client/src/agent.js index d76b2a13d..535644640 100644 --- a/packages/access-client/src/agent.js +++ b/packages/access-client/src/agent.js @@ -11,7 +11,7 @@ import { URI } from '@ucanto/validator' import { Peer } from './awake/peer.js' import * as Space from './capabilities/space.js' import * as Voucher from './capabilities/voucher.js' -import { any as Any } from './capabilities/wildcard.js' +import { top as Top } from './capabilities/top.js' import { stringToDelegation } from './encoding.js' import { Websocket, AbortError } from './utils/ws.js' import { Signer } from '@ucanto/principal/ed25519' @@ -225,7 +225,7 @@ export class Agent { */ async createSpace(name) { const signer = await Signer.generate() - const proof = await Any.delegate({ + const proof = await Top.delegate({ issuer: signer, audience: this.issuer, with: signer.did(), diff --git a/packages/access-client/src/capabilities/space.js b/packages/access-client/src/capabilities/space.js index 90594663b..f45237367 100644 --- a/packages/access-client/src/capabilities/space.js +++ b/packages/access-client/src/capabilities/space.js @@ -9,21 +9,21 @@ * @module */ -import { any } from './wildcard.js' +import { top } from './top.js' import { store } from './store.js' import { capability, URI } from '@ucanto/validator' import { canDelegateURI, equalWith, fail } from './utils.js' -export const space = any.derive({ +export const space = top.derive({ to: capability({ - can: 'account/*', + can: 'space/*', with: URI.match({ protocol: 'did:' }), derives: equalWith, }), derives: equalWith, }) -const base = any.or(space) +const base = top.or(space) /** * `space/info` can be derived from any of the `store/*` diff --git a/packages/access-client/src/capabilities/store.js b/packages/access-client/src/capabilities/store.js index 6bf21052e..934cbf666 100644 --- a/packages/access-client/src/capabilities/store.js +++ b/packages/access-client/src/capabilities/store.js @@ -10,14 +10,14 @@ */ import { capability, Failure, Link, URI, Schema } from '@ucanto/validator' import { equalLink, equalWith } from './utils.js' -import { any } from './wildcard.js' +import { top } from './top.js' /** * Capability can only be delegated (but not invoked) allowing audience to * derived any `store/` prefixed capability for the (memory) space identified * by did:key in the `with` field. */ -export const store = any.derive({ +export const store = top.derive({ to: capability({ can: 'store/*', /** @@ -39,7 +39,7 @@ export const store = any.derive({ // derived from `store/*`. As a workaround we just define base capability // here so all store capabilities could be derived from either `*` or // `store/*`. -const base = any.or(store) +const base = top.or(store) /** * `store/add` capability allows agent to store a CAR file into a (memory) space diff --git a/packages/access-client/src/capabilities/wildcard.js b/packages/access-client/src/capabilities/top.js similarity index 52% rename from packages/access-client/src/capabilities/wildcard.js rename to packages/access-client/src/capabilities/top.js index c25a59e7e..3e3f7ac7c 100644 --- a/packages/access-client/src/capabilities/wildcard.js +++ b/packages/access-client/src/capabilities/top.js @@ -1,9 +1,9 @@ /** - * Wildcard Capabilities + * Top Capabilities * * These can be imported directly with: * ```js - * import * as Account from '@web3-storage/access/capabilities/wildcard' + * import * as Account from '@web3-storage/access/capabilities/top' * ``` * * @module @@ -13,10 +13,12 @@ import { capability, URI } from '@ucanto/validator' import { equalWith } from './utils.js' /** - * Represents `{ can: '*', with: 'did:key:zAlice' }` capability, which we often + * Represents the top `{ can: '*', with: 'did:key:zAlice' }` capability, which we often * also call account linking. + * + * @see {@link https://github.com/ucan-wg/spec#52-top} */ -export const any = capability({ +export const top = capability({ can: '*', with: URI.match({ protocol: 'did:' }), derives: equalWith, diff --git a/packages/access-client/src/capabilities/types.ts b/packages/access-client/src/capabilities/types.ts index 9546d6ae1..3db23a629 100644 --- a/packages/access-client/src/capabilities/types.ts +++ b/packages/access-client/src/capabilities/types.ts @@ -1,7 +1,7 @@ import { InferInvokedCapability } from '@ucanto/interface' import { space, info, recover, recoverValidation } from './space.js' -import { any } from './wildcard.js' -import { add, list, remove } from './store.js' +import { top } from './top.js' +import { add, list, remove, store } from './store.js' import * as UploadCaps from './upload.js' import { claim, redeem } from './voucher.js' @@ -22,11 +22,12 @@ export type UploadAdd = InferInvokedCapability export type UploadRemove = InferInvokedCapability export type UploadList = InferInvokedCapability // Store +export type Store = InferInvokedCapability export type StoreAdd = InferInvokedCapability export type StoreRemove = InferInvokedCapability export type StoreList = InferInvokedCapability -// Any -export type Any = InferInvokedCapability +// Top +export type Top = InferInvokedCapability export type Abilities = | Space['can'] @@ -39,7 +40,8 @@ export type Abilities = | UploadAdd['can'] | UploadRemove['can'] | UploadList['can'] + | Store['can'] | StoreAdd['can'] | StoreRemove['can'] | StoreList['can'] - | Any['can'] + | Top['can'] diff --git a/packages/access-client/src/capabilities/upload.js b/packages/access-client/src/capabilities/upload.js index 9ae1ed5ed..eb1f25250 100644 --- a/packages/access-client/src/capabilities/upload.js +++ b/packages/access-client/src/capabilities/upload.js @@ -11,14 +11,14 @@ import { capability, Link, URI } from '@ucanto/validator' import { codec as CAR } from '@ucanto/transport/car' import { equalWith, fail, equal } from './utils.js' -import { any } from './wildcard.js' +import { top } from './top.js' /** * Capability can only be delegated (but not invoked) allowing audience to * derived any `upload/` prefixed capability for the (memory) space identified * by did:key in the `with` field. */ -export const upload = any.derive({ +export const upload = top.derive({ to: capability({ can: 'upload/*', /** @@ -40,7 +40,7 @@ export const upload = any.derive({ // derived from `upload/*`. As a workaround we just define base capability // here so all store capabilities could be derived from either `*` or // `upload/*`. -const base = any.or(upload) +const base = top.or(upload) /** * Schema representing a link (a.k.a CID) to a CAR file. Enforces CAR codec code and CID v1. diff --git a/packages/access-client/src/capabilities/utils.js b/packages/access-client/src/capabilities/utils.js index d5e10da60..2b984be01 100644 --- a/packages/access-client/src/capabilities/utils.js +++ b/packages/access-client/src/capabilities/utils.js @@ -94,15 +94,8 @@ export function fail(value) { * @param {import('@ucanto/interface').Ability} ability */ function parseAbility(ability) { - switch (ability) { - case '*': { - return '*' - } - default: { - const [namespace, ...segments] = ability.split('/') - return { namespace, segments } - } - } + const [namespace, ...segments] = ability.split('/') + return { namespace, segments } } /** @@ -117,12 +110,12 @@ export function canDelegateAbility(parent, child) { const parsedChild = parseAbility(child) // Parent is wildcard - if (parsedParent === '*') { + if (parsedParent.namespace === '*' && parsedParent.segments.length === 0) { return true } // Child is wild card so it can not be delegated from anything - if (parsedChild === '*') { + if (parsedChild.namespace === '*' && parsedChild.segments.length === 0) { return false } @@ -131,7 +124,7 @@ export function canDelegateAbility(parent, child) { return false } - // given namespaces match and parent first segment is wildcard + // given that namespaces match and parent first segment is wildcard if (parsedParent.segments[0] === '*') { return true } diff --git a/packages/access-client/src/capabilities/voucher.js b/packages/access-client/src/capabilities/voucher.js index dc85aa9a8..786ae9cf2 100644 --- a/packages/access-client/src/capabilities/voucher.js +++ b/packages/access-client/src/capabilities/voucher.js @@ -13,7 +13,7 @@ import { capability, URI, DID } from '@ucanto/validator' // eslint-disable-next-line no-unused-vars import * as Types from '@ucanto/interface' import { equalWith, fail, equal } from './utils.js' -import { any } from './wildcard.js' +import { top } from './top.js' /** * Products are identified by the CID of the DAG that describes them. @@ -39,7 +39,7 @@ export const Service = DID.match({ method: 'key' }) * Currently DID in the `with` field will always be web3.storage DID since we * do not support other types of vouchers yet. */ -export const voucher = any.derive({ +export const voucher = top.derive({ to: capability({ can: 'voucher/*', with: URI.match({ protocol: 'did:' }), @@ -48,7 +48,7 @@ export const voucher = any.derive({ derives: equalWith, }) -const base = any.or(voucher) +const base = top.or(voucher) /** * Capability can be invoked by an agent to claim a voucher for a specific diff --git a/packages/access-client/src/index.js b/packages/access-client/src/index.js index 738fb0701..99fb59e81 100644 --- a/packages/access-client/src/index.js +++ b/packages/access-client/src/index.js @@ -3,7 +3,7 @@ export * from './agent.js' // Workaround for typedoc until 0.24 support export maps export * as Space from './capabilities/space.js' -export * as Wildcard from './capabilities/wildcard.js' +export * as Top from './capabilities/top.js' export * as Store from './capabilities/store.js' export * as Upload from './capabilities/upload.js' export * as Voucher from './capabilities/voucher.js' diff --git a/packages/access-client/src/types.ts b/packages/access-client/src/types.ts index b16947012..204fb1c4c 100644 --- a/packages/access-client/src/types.ts +++ b/packages/access-client/src/types.ts @@ -27,7 +27,7 @@ import type { SpaceInfo, SpaceRecover, SpaceRecoverValidation, - Any, + Top, } from './capabilities/types' import { VoucherClaim, VoucherRedeem } from './capabilities/types.js' import { IStore } from './stores/types.js' @@ -58,7 +58,7 @@ export interface Service { > recover: ServiceMethod< SpaceRecover, - Array>, + Array>, Failure > } diff --git a/packages/access-client/test/awake.node.test.js b/packages/access-client/test/awake.node.test.js index 4f1cdc207..5f55a2684 100644 --- a/packages/access-client/test/awake.node.test.js +++ b/packages/access-client/test/awake.node.test.js @@ -42,8 +42,8 @@ describe('awake', function () { store: await StoreMemory.create(), url: new URL('http://127.0.0.1:8787'), }) - const account = await agent1.createSpace('responder') - await agent1.setCurrentSpace(account.did) + const space = await agent1.createSpace('responder') + await agent1.setCurrentSpace(space.did) const agent2 = await Agent.create({ store: await StoreMemory.create(), url: new URL('http://127.0.0.1:8787'), @@ -104,7 +104,7 @@ describe('awake', function () { // @ts-ignore if (link) { assert.deepEqual(requestor.did, link.delegation.audience.did()) - assert.deepEqual(account.did, link.delegation.capabilities[0].with) + assert.deepEqual(space.did, link.delegation.capabilities[0].with) assert.deepEqual('*', link.delegation.capabilities[0].can) } diff --git a/packages/access-client/test/capabilities/store.test.js b/packages/access-client/test/capabilities/store.test.js index 9f85157e9..d574be53b 100644 --- a/packages/access-client/test/capabilities/store.test.js +++ b/packages/access-client/test/capabilities/store.test.js @@ -4,7 +4,7 @@ import { access } from '@ucanto/validator' import { Verifier } from '@ucanto/principal' import { delegate, parseLink } from '@ucanto/core' import * as Store from '../../src/capabilities/store.js' -import * as Capability from '../../src/capabilities/wildcard.js' +import * as Capability from '../../src/capabilities/top.js' import { alice, @@ -14,8 +14,8 @@ import { } from '../helpers/fixtures.js' import { createCarCid } from '../helpers/utils.js' -const any = async () => - Capability.any.delegate({ +const top = async () => + Capability.top.delegate({ issuer: account, audience: alice, with: account.did(), @@ -26,7 +26,7 @@ const store = async () => issuer: account, audience: alice, with: account.did(), - proofs: [await any()], + proofs: [await top()], }) describe('store capabilities', function () { @@ -39,7 +39,7 @@ describe('store capabilities', function () { link: parseLink('bafkqaaa'), size: 0, }, - proofs: [await any()], + proofs: [await top()], }) const result = await access(await add.delegate(), { @@ -93,7 +93,7 @@ describe('store capabilities', function () { issuer: alice, audience: bob, with: account.did(), - proofs: [await any()], + proofs: [await top()], }) const add = Store.add.invoke({ @@ -132,7 +132,7 @@ describe('store capabilities', function () { nb: { size: 1024, }, - proofs: [await any()], + proofs: [await top()], }) { @@ -190,7 +190,7 @@ describe('store capabilities', function () { for (const size of fixtures) { const json = JSON.stringify(size) it(`store/add size must be an int not ${json}`, async () => { - const proofs = [await any()] + const proofs = [await top()] assert.throws(() => { Store.add.invoke({ issuer: alice, @@ -220,7 +220,7 @@ describe('store capabilities', function () { }, }, ], - proofs: [await any()], + proofs: [await top()], }) // @ts-expect-error - size type doesnt not match because we are testing fails @@ -235,7 +235,7 @@ describe('store capabilities', function () { } it('store/add size must be an int', async () => { - const proofs = [await any()] + const proofs = [await top()] assert.throws(() => { Store.add.invoke({ issuer: alice, diff --git a/packages/upload-client/test/utils.test.js b/packages/upload-client/test/utils.test.js index 0c7cba25d..d73ac27c2 100644 --- a/packages/upload-client/test/utils.test.js +++ b/packages/upload-client/test/utils.test.js @@ -1,7 +1,7 @@ import assert from 'assert' import * as Signer from '@ucanto/principal/ed25519' import { capability, URI } from '@ucanto/validator' -import { any } from '@web3-storage/access/capabilities/wildcard' +import { top } from '@web3-storage/access/capabilities/top' import * as StoreCapabilities from '@web3-storage/access/capabilities/store' import { equalWith } from '@web3-storage/access/capabilities/utils' import { serviceSigner } from './fixtures.js' @@ -49,7 +49,7 @@ describe('findCapability', () => { it('matches any wildcard capability', async () => { const issuer = await Signer.generate() const proofs = [ - await any.delegate({ + await top.delegate({ issuer, audience: serviceSigner, with: issuer.did(),