Skip to content

Commit

Permalink
packages/access-client no longer uses hd-scripts (fixes eslint warning)
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed Nov 1, 2023
1 parent 38fa0d3 commit 540aa04
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 430 deletions.
42 changes: 35 additions & 7 deletions packages/access-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@
"@types/sinon": "^10.0.19",
"@types/varint": "^6.0.1",
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"@ucanto/server": "^9.0.1",
"assert": "^2.0.0",
"hd-scripts": "^4.0.0",
"eslint": "^8.52.0",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-unicorn": "^49.0.0",
"mocha": "^10.2.0",
"playwright-test": "^12.3.4",
"sinon": "^15.0.3",
Expand All @@ -95,17 +99,39 @@
},
"eslintConfig": {
"extends": [
"./node_modules/hd-scripts/eslint/index.js"
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jsdoc/recommended"
],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-export-from": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/ban-ts-comment": "off",
"unicorn/expiring-todo-comments": "off",
"unicorn/no-array-reduce": "off",
"unicorn/explicit-length-check": "off",
"unicorn/no-array-reduce": "off",
"unicorn/no-for-loop": "off",
"unicorn/prefer-export-from": "off",
"unicorn/prefer-node-protocol": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prevent-abbreviations": "off",
"jsdoc/check-values": "off",
"jsdoc/check-tag-names": "off",
"jsdoc/require-returns": "off",
"jsdoc/require-returns-type": "off",
"jsdoc/require-throws": "off",
"jsdoc/require-param-description": "off",
"jsdoc/require-returns-description": "off",
"jsdoc/valid-types": "off",
"jsdoc/tag-lines": [
"error",
"any",
{
"startLines": 1
}
],
"jsdoc/no-undefined-types": [
"error",
{
Expand All @@ -125,7 +151,10 @@
]
},
"env": {
"mocha": true
"es2022": true,
"mocha": true,
"browser": true,
"node": true
},
"ignorePatterns": [
"dist"
Expand All @@ -137,7 +166,6 @@
],
"ignores": [
"@types/*",
"hd-scripts",
"assert"
]
}
Expand Down
29 changes: 21 additions & 8 deletions packages/access-client/src/agent-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,27 @@ export function getSessionProofs(data) {
* @returns {boolean} whether the ucan matches the options
*/
export function matchSessionProof(ucan, options) {
if ( ! isSessionProof(ucan)) { return false; }
if (!isSessionProof(ucan)) {
return false
}
const cap = ucan.capabilities[0]
const matchesRequiredIssuer = (options.issuer === undefined) || options.issuer === ucan.issuer.did()
const isExpiredButNotAllowed = ( ! options.allowExpired) && isExpired(ucan)
const matchesRequiredProof = ( ! options.attestedProof) || (options.attestedProof.toString() === cap.nb.proof.toString())
if ( ! isSessionProof(ucan)) { return false; }
if (isExpiredButNotAllowed) { return false; }
if ( ! matchesRequiredIssuer) { return false; }
if ( ! matchesRequiredProof) { return false; }
const matchesRequiredIssuer =
options.issuer === undefined || options.issuer === ucan.issuer.did()
const isExpiredButNotAllowed = !options.allowExpired && isExpired(ucan)
const matchesRequiredProof =
!options.attestedProof ||
options.attestedProof.toString() === cap.nb.proof.toString()
if (!isSessionProof(ucan)) {
return false
}
if (isExpiredButNotAllowed) {
return false
}
if (!matchesRequiredIssuer) {
return false
}
if (!matchesRequiredProof) {
return false
}
return true
}
1 change: 1 addition & 0 deletions packages/access-client/src/agent-use-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export async function pollAccessClaimUntil(
opts
) {
const interval = opts?.interval || 250
// eslint-disable-next-line no-constant-condition
while (true) {
if (opts?.signal?.aborted)
throw opts.signal.reason ?? new Error('operation aborted')
Expand Down
52 changes: 32 additions & 20 deletions packages/access-client/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const PRINCIPAL = DID.parse('did:web:web3.storage')
/**
* Keeps track of AgentData for all Agents constructed.
* Used by
* * addSpacesFromDelegations - so it can only accept Agent as param, but still mutate corresponding AgentData
* addSpacesFromDelegations - so it can only accept Agent as param, but still mutate corresponding AgentData
*
* @deprecated - remove this when deprecated addSpacesFromDelegations is removed
*/
Expand Down Expand Up @@ -169,44 +169,56 @@ export class Agent {
* Query the delegations store for all the delegations matching the capabilities provided.
*
* @param {import('@ucanto/interface').Capability[]} [caps]
* @param {Ucanto.DID} [invocationAudience] - audience of invocation these proofs will be bundled with.
* @param {Ucanto.DID} [invocationAudience] - audience of invocation these proofs will be bundled with.
*/
#delegations(caps, invocationAudience) {
const _caps = new Set(caps)
/** @type {Array<{ delegation: Ucanto.Delegation, meta: import('./types.js').DelegationMeta }>} */
const values = []
for (const [, value] of this.#data.delegations) {
// check expiration
if (isExpired(value.delegation)) { continue; }
if (isTooEarly(value.delegation)) { continue; }
if (isExpired(value.delegation)) {
continue
}
if (isTooEarly(value.delegation)) {
continue
}

if (Array.isArray(caps) && caps.length > 0) {
// caps param is provided. Ensure that the delegations we're looping over can delegate to these caps
for (const cap of _caps) {
const capProbablyRequiresSessionProof = value.delegation.signature.code === ucanto.Signature.NON_STANDARD
const findSessionProofs = () => [...this.#data.delegations.values()].filter(m => matchSessionProof(m.delegation, {
attestedProof: value.delegation.cid,
issuer: invocationAudience,
}))
const capProbablyRequiresSessionProof =
value.delegation.signature.code === ucanto.Signature.NON_STANDARD
const findSessionProofs = () =>
[...this.#data.delegations.values()].filter((m) =>
matchSessionProof(m.delegation, {
attestedProof: value.delegation.cid,
issuer: invocationAudience,
})
)
if (canDelegateCapability(value.delegation, cap)) {
const noSessionRequired = ! capProbablyRequiresSessionProof
const sessionProofs = capProbablyRequiresSessionProof ? findSessionProofs() : []
const hasRequiredSessionProof = capProbablyRequiresSessionProof && findSessionProofs().length > 0
const noSessionRequired = !capProbablyRequiresSessionProof
const sessionProofs = capProbablyRequiresSessionProof
? findSessionProofs()
: []
const hasRequiredSessionProof =
capProbablyRequiresSessionProof && findSessionProofs().length > 0
const proofs =
// eslint-disable-next-line no-nested-ternary
(noSessionRequired)
? [value]
: hasRequiredSessionProof
? [value, ...sessionProofs]
: []
noSessionRequired
? [value]
: hasRequiredSessionProof
? [value, ...sessionProofs]
: []

values.push(...proofs)
if (proofs.length) {
_caps.delete(cap)
}
}
}
} else { // no caps param is provided. Caller must want all delegations.
} else {
// no caps param is provided. Caller must want all delegations.
values.push(value)
}
}
Expand Down Expand Up @@ -268,7 +280,7 @@ export class Agent {
* proofs matching the passed capabilities require it.
*
* @param {import('@ucanto/interface').Capability[]} [caps] - Capabilities to filter by. Empty or undefined caps with return all the proofs.
* @param {Ucanto.DID} [invocationAudience] - audience of invocation these proofs will be bundled with.
* @param {Ucanto.DID} [invocationAudience] - audience of invocation these proofs will be bundled with.
*/
proofs(caps, invocationAudience) {
const arr = []
Expand Down
1 change: 0 additions & 1 deletion packages/access-client/src/crypto/aes-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export class AesKey {
)
const encryptedBytes = new Uint8Array(buf)
const encrypted = uint8arrays.toString(
// eslint-disable-next-line unicorn/prefer-spread
uint8arrays.concat([iv, encryptedBytes]),
'base64pad'
)
Expand Down
1 change: 0 additions & 1 deletion packages/access-client/src/crypto/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export function decompressP256(comp) {
yPadded.set(y, offset)

// concat coords & prepend P-256 prefix
// eslint-disable-next-line unicorn/prefer-spread
const publicKey = uint8arrays.concat([[0x04], x, yPadded])
return publicKey
}
5 changes: 1 addition & 4 deletions packages/access-client/src/crypto/p256-ecdh.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ async function didFromPubkey(pubkey) {
const buf = await webcrypto.subtle.exportKey('raw', pubkey)
const bytes = new Uint8Array(buf)
return DID.format(
DID.decode(
// eslint-disable-next-line unicorn/prefer-spread
uint8arrays.concat([P256_DID_PREFIX, compressP256Pubkey(bytes)])
)
DID.decode(uint8arrays.concat([P256_DID_PREFIX, compressP256Pubkey(bytes)]))
)
}

Expand Down
1 change: 0 additions & 1 deletion packages/access-client/src/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*
* @module
*/
/* eslint-disable unicorn/prefer-spread */
import { CarBufferReader } from '@ipld/car/buffer-reader'
import * as CarBufferWriter from '@ipld/car/buffer-writer'
import { Delegation } from '@ucanto/core/delegation'
Expand Down
1 change: 1 addition & 0 deletions packages/access-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export interface SpaceMeta {
* Agent class types
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface AgentOptions<S extends Record<string, any>> {
url?: URL
connection?: ConnectionView<S>
Expand Down
1 change: 0 additions & 1 deletion packages/access-client/test/agent-data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AgentData } from '../src/agent-data.js'

describe('AgentData', () => {
it('should not destructure store methods', async () => {
// eslint-disable-next-line unicorn/no-await-expression-member
const raw = (await AgentData.create()).export()
class Store {
async open() {}
Expand Down
15 changes: 8 additions & 7 deletions packages/access-client/test/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ describe('Agent', function () {
const { nb: input } = capability
const ucan = Delegation.view(
{ root: input.ucan, blocks: invocation.blocks },
// eslint-disable-next-line unicorn/no-null
null
)
return ucan
Expand Down Expand Up @@ -348,7 +347,9 @@ describe('Agent', function () {
*/
it('should include session proof based on connection', async () => {
// const space = await ed25519.Signer.generate()
const account = DidMailto.fromEmail(`test-${Math.random().toString().slice(2)}@dag.house`)
const account = DidMailto.fromEmail(
`test-${Math.random().toString().slice(2)}@dag.house`
)
const serviceA = await ed25519.Signer.generate()
const serviceAWeb = serviceA.withDID('did:web:a.up.web3.storage')
const serviceB = await ed25519.Signer.generate()
Expand All @@ -369,8 +370,8 @@ describe('Agent', function () {
{
can: 'provider/add',
with: 'ucan:*',
}
]
},
],
})
const session = await Access.session.delegate({
issuer: service,
Expand All @@ -388,10 +389,10 @@ describe('Agent', function () {
[
{
can: 'provider/add',
with: account
}
with: account,
},
],
desiredInvocationAudience.did(),
desiredInvocationAudience.did()
)
assert.ok(proofsA)
assert.equal(proofsA[1].issuer.did(), desiredInvocationAudience.did())
Expand Down
Loading

0 comments on commit 540aa04

Please sign in to comment.