Skip to content

Commit

Permalink
chore: alan feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Nov 21, 2022
1 parent 5385f33 commit fbf5b27
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/access-api/src/kvs/spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/access-api/src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/access-api/test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions packages/access-api/test/voucher-redeem.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions packages/access-client/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions packages/access-client/src/capabilities/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/*`
Expand Down
6 changes: 3 additions & 3 deletions packages/access-client/src/capabilities/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/*',
/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions packages/access-client/src/capabilities/types.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -22,11 +22,12 @@ export type UploadAdd = InferInvokedCapability<typeof UploadCaps.add>
export type UploadRemove = InferInvokedCapability<typeof UploadCaps.remove>
export type UploadList = InferInvokedCapability<typeof UploadCaps.list>
// Store
export type Store = InferInvokedCapability<typeof store>
export type StoreAdd = InferInvokedCapability<typeof add>
export type StoreRemove = InferInvokedCapability<typeof remove>
export type StoreList = InferInvokedCapability<typeof list>
// Any
export type Any = InferInvokedCapability<typeof any>
// Top
export type Top = InferInvokedCapability<typeof top>

export type Abilities =
| Space['can']
Expand All @@ -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']
6 changes: 3 additions & 3 deletions packages/access-client/src/capabilities/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/*',
/**
Expand All @@ -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.
Expand Down
17 changes: 5 additions & 12 deletions packages/access-client/src/capabilities/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

/**
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions packages/access-client/src/capabilities/voucher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:' }),
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/access-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions packages/access-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface Service {
>
recover: ServiceMethod<
SpaceRecover,
Array<EncodedDelegation<[Any]>>,
Array<EncodedDelegation<[Top]>>,
Failure
>
}
Expand Down
6 changes: 3 additions & 3 deletions packages/access-client/test/awake.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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)
}

Expand Down
Loading

0 comments on commit fbf5b27

Please sign in to comment.