From c714c429760c7a94bfb5e7c77f01ef909529fea0 Mon Sep 17 00:00:00 2001 From: Zach Ferland Date: Fri, 13 Jan 2023 16:56:46 -0500 Subject: [PATCH 1/2] feat(recaps): add proposed api --- packages/recaps/.eslintignore | 1 + packages/recaps/.eslintrc.json | 11 ++++ packages/recaps/.gitignore | 4 ++ packages/recaps/LICENSE-APACHE | 5 ++ packages/recaps/LICENSE-MIT | 19 ++++++ packages/recaps/README.md | 21 +++++++ packages/recaps/jest.config.json | 22 +++++++ packages/recaps/package.json | 57 ++++++++++++++++++ packages/recaps/src/index.ts | 93 ++++++++++++++++++++++++++++++ packages/recaps/test/siwe.test.ts | 3 + packages/recaps/tsconfig.json | 7 +++ packages/recaps/tsconfig.lint.json | 4 ++ tsconfig.docs.json | 6 +- tsconfig.json | 3 +- 14 files changed, 253 insertions(+), 3 deletions(-) create mode 100644 packages/recaps/.eslintignore create mode 100644 packages/recaps/.eslintrc.json create mode 100644 packages/recaps/.gitignore create mode 100644 packages/recaps/LICENSE-APACHE create mode 100644 packages/recaps/LICENSE-MIT create mode 100644 packages/recaps/README.md create mode 100644 packages/recaps/jest.config.json create mode 100644 packages/recaps/package.json create mode 100644 packages/recaps/src/index.ts create mode 100644 packages/recaps/test/siwe.test.ts create mode 100644 packages/recaps/tsconfig.json create mode 100644 packages/recaps/tsconfig.lint.json diff --git a/packages/recaps/.eslintignore b/packages/recaps/.eslintignore new file mode 100644 index 00000000..c90e3fb1 --- /dev/null +++ b/packages/recaps/.eslintignore @@ -0,0 +1 @@ +src/abnf.ts \ No newline at end of file diff --git a/packages/recaps/.eslintrc.json b/packages/recaps/.eslintrc.json new file mode 100644 index 00000000..acb22d58 --- /dev/null +++ b/packages/recaps/.eslintrc.json @@ -0,0 +1,11 @@ +{ + "extends": ["3box", "3box/jest", "3box/typescript"], + "parserOptions": { + "project": ["tsconfig.lint.json"] + }, + "rules": { + "@typescript-eslint/ban-ts-comment": "off", + "@typescript-eslint/no-namespace": "off", + "@typescript-eslint/no-unsafe-assignment": "off" + } +} diff --git a/packages/recaps/.gitignore b/packages/recaps/.gitignore new file mode 100644 index 00000000..a8d7c85b --- /dev/null +++ b/packages/recaps/.gitignore @@ -0,0 +1,4 @@ +*.log +.DS_Store +node_modules +dist \ No newline at end of file diff --git a/packages/recaps/LICENSE-APACHE b/packages/recaps/LICENSE-APACHE new file mode 100644 index 00000000..4c83a284 --- /dev/null +++ b/packages/recaps/LICENSE-APACHE @@ -0,0 +1,5 @@ +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. \ No newline at end of file diff --git a/packages/recaps/LICENSE-MIT b/packages/recaps/LICENSE-MIT new file mode 100644 index 00000000..749aa1ec --- /dev/null +++ b/packages/recaps/LICENSE-MIT @@ -0,0 +1,19 @@ +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/packages/recaps/README.md b/packages/recaps/README.md new file mode 100644 index 00000000..0b385779 --- /dev/null +++ b/packages/recaps/README.md @@ -0,0 +1,21 @@ +> ⚠️ This package is experimental and based on a specification that is yet to be finalized. + +# ReCaps + +A library to create and manage ReCaps, including their (de)serialization and human-readable ReCap statements. Follows [EIP-5573](https://github.com/spruceid/EIPs/blob/19dae81ecc8354199bbe28f3b6248851f4cf49cf/EIPS/eip-5573.md) (not finalized). + + + +## Examples + +### TODO + +```typescript +const recapstr = new Recap({ + ... +}) +``` + +## License + +Dual licensed with APACHE and MIT diff --git a/packages/recaps/jest.config.json b/packages/recaps/jest.config.json new file mode 100644 index 00000000..53bb7cd5 --- /dev/null +++ b/packages/recaps/jest.config.json @@ -0,0 +1,22 @@ +{ + "moduleFileExtensions": ["js", "json", "ts"], + "testRegex": ".(spec|test).ts$", + "testEnvironment": "node", + "extensionsToTreatAsEsm": [".ts"], + "globals": { + "ts-jest": { + "useESM": true + } + }, + "moduleNameMapper": { + "^(\\.{1,2}/.*)\\.js$": "$1" + }, + "transform": { + "^.+\\.(t|j)s$": [ + "@swc/jest", + { + "root": "../.." + } + ] + } +} \ No newline at end of file diff --git a/packages/recaps/package.json b/packages/recaps/package.json new file mode 100644 index 00000000..23c5d7a1 --- /dev/null +++ b/packages/recaps/package.json @@ -0,0 +1,57 @@ +{ + "name": "@didtools/recaps", + "version": "0.0.1", + "description": "Typescript library for ReCaps", + "author": "3Box Labs", + "license": "(Apache-2.0 OR MIT)", + "type": "module", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "exports": { + ".": "./dist/index.js" + }, + "files": [ + "dist" + ], + "engines": { + "node": ">=14.14" + }, + "sideEffects": false, + "scripts": { + "build:clean": "del dist", + "build:js": "swc src -d ./dist --config-file ../../.swcrc", + "build:types": "tsc --emitDeclarationOnly --skipLibCheck", + "build": "pnpm run build:clean && pnpm run build:types && pnpm run build:js", + "lint": "eslint src test --fix", + "test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js", + "test:ci": "pnpm run test --ci --coverage", + "prepare": "pnpm run build", + "prepublishOnly": "package-check" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ceramicnetwork/js-did.git" + }, + "keywords": [ + "DID", + "identity", + "OCAP", + "capabilities" + ], + "bugs": { + "url": "https://github.com/ceramicnetwork/js-did/issues" + }, + "homepage": "https://github.com/ceramicnetwork/js-did#readme", + "dependencies": { + "@ipld/dag-cbor": "^7.0.1", + "apg-js": "^4.1.1", + "caip": "^1.1.0", + "multiformats": "^9.5.1", + "uint8arrays": "^4.0.2" + }, + "devDependencies": { + "@types/luxon": "^3.0.0", + "@types/node": "^18.11.7", + "luxon": "^3.0.1" + } +} diff --git a/packages/recaps/src/index.ts b/packages/recaps/src/index.ts new file mode 100644 index 00000000..38193334 --- /dev/null +++ b/packages/recaps/src/index.ts @@ -0,0 +1,93 @@ +type CeramicWrite = { + "ceramic/write": [] +} + +type Actions = CeramicWrite +type Capability = [string, Actions] +type Capablities = Array + +function validCeramicTarget(s: string): Uint8Array { + // const modelResource = + // const streamResource = + // const allResource = "" + return true +} + +export class ReCap { + _caps: Capablities + + constructor(caps:Capablities) { + this._caps = caps + } + + /** + * Create ReCap instance from JSON representation + */ + static fromJson(json:Map):ReCap { + // + } + + /** + * Create ReCap instance from URI representation, typically from a SIWX message + */ + static fromURI(recap: string): Recap { + + } + + /** + * Encode ReCap to ReCap URI (urn:recap:base64url-encoded-recap) to be used in SIWX message + */ + encode():string { + // + } + + /** + * Query if this ReCap is valid for a given action and resource/target + */ + can(action: Actions, target: string):boolean { + + } + + /** + * Generate the human readable ReCap statement for this ReCap, to be used in SIWX message + */ + toStatement():string { + + } + + /** + * Get JSON representation of ReCap + */ + toJson():Map { + + } + + /** + * Add a capability (action & target) to a ReCap, returns new recap + */ + addCapability(action: Actions, target: string):ReCap { + + } + + /** + * Verify that a ReCap Statement is valid for this ReCap and encodes the same delegations + */ + verifyStatment(statement:string):boolean { + + } + + /** + * Get array of resources inlcuded in ReCap + */ + get resourceList(): Array { + + } + + /** + * Get array of actions for given resource/target + */ + get actionsByResource(resource:string): Array { + + } +} + \ No newline at end of file diff --git a/packages/recaps/test/siwe.test.ts b/packages/recaps/test/siwe.test.ts new file mode 100644 index 00000000..afbf6e83 --- /dev/null +++ b/packages/recaps/test/siwe.test.ts @@ -0,0 +1,3 @@ +describe('Cacao Auth Verify', () => { + test.todo('Verify') +}) diff --git a/packages/recaps/tsconfig.json b/packages/recaps/tsconfig.json new file mode 100644 index 00000000..34756dd4 --- /dev/null +++ b/packages/recaps/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.build.json", + "compilerOptions": { + "outDir": "./dist" + }, + "include": ["src"] +} diff --git a/packages/recaps/tsconfig.lint.json b/packages/recaps/tsconfig.lint.json new file mode 100644 index 00000000..c3ab1eff --- /dev/null +++ b/packages/recaps/tsconfig.lint.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "include": ["src", "test"] +} \ No newline at end of file diff --git a/tsconfig.docs.json b/tsconfig.docs.json index baedbe71..eafb6767 100644 --- a/tsconfig.docs.json +++ b/tsconfig.docs.json @@ -9,7 +9,8 @@ "./packages/key-did-provider-ed25519/src", "./packages/key-did-provider-secp256k1/src", "./packages/key-did-resolver/src", - "./packages/pkh-did-resolver/src" + "./packages/pkh-did-resolver/src", + "./packages/recaps/src" ], "compilerOptions": { "jsx": "preserve", @@ -29,7 +30,8 @@ "./packages/key-did-provider-ed25519/src/index.ts", "./packages/key-did-provider-secp256k1/src/index.ts", "./packages/key-did-resolver/src/index.ts", - "./packages/pkh-did-resolver/src/index.ts" + "./packages/pkh-did-resolver/src/index.ts", + "./packages/recaps/src/index.ts" ], "disableSources": true, "excludeExternals": true, diff --git a/tsconfig.json b/tsconfig.json index 5a8cb686..f4ac2463 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,8 @@ "@didtools/pkh-ethereum": ["packages/pkh-ethereum/src"], "@didtools/pkh-solana": ["packages/pkh-solana/src"], "pkh-did-resolver": ["packages/pkh-did-resolver/src"], - "key-did-resolver": ["packages/key-did-resolver/src"] + "key-did-resolver": ["packages/key-did-resolver/src"], + "@didtools/recaps": ["packages/recaps/src"] } } } \ No newline at end of file From c21108422df5230753792bd1d1c614b01fdf9e81 Mon Sep 17 00:00:00 2001 From: Zach Ferland Date: Tue, 17 Jan 2023 15:33:04 -0500 Subject: [PATCH 2/2] feat(recaps): imp outlined lib --- packages/recaps/package.json | 5 +- packages/recaps/src/index.ts | 76 ++++++++++++++++++++++-------- packages/recaps/test/recap.test.ts | 3 ++ packages/recaps/test/siwe.test.ts | 3 -- 4 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 packages/recaps/test/recap.test.ts delete mode 100644 packages/recaps/test/siwe.test.ts diff --git a/packages/recaps/package.json b/packages/recaps/package.json index 23c5d7a1..0ce021fd 100644 --- a/packages/recaps/package.json +++ b/packages/recaps/package.json @@ -43,10 +43,7 @@ }, "homepage": "https://github.com/ceramicnetwork/js-did#readme", "dependencies": { - "@ipld/dag-cbor": "^7.0.1", - "apg-js": "^4.1.1", - "caip": "^1.1.0", - "multiformats": "^9.5.1", + "dids": "workspace:^3.2.0", "uint8arrays": "^4.0.2" }, "devDependencies": { diff --git a/packages/recaps/src/index.ts b/packages/recaps/src/index.ts index 38193334..a28f24ac 100644 --- a/packages/recaps/src/index.ts +++ b/packages/recaps/src/index.ts @@ -1,3 +1,6 @@ +import { encodeBase64Url, base64urlToJSON } from 'did' +import * as u8a from 'uint8arrays' + type CeramicWrite = { "ceramic/write": [] } @@ -6,15 +9,35 @@ type Actions = CeramicWrite type Capability = [string, Actions] type Capablities = Array + +type ReCapObject = { + "att": Record + "prf": Array +} + function validCeramicTarget(s: string): Uint8Array { + // TODO // const modelResource = // const streamResource = // const allResource = "" return true } +//TODO validate recap urn +//TODO validate, add JSON schema, add ABNF + +function formatRecap(attenuations: { [k: string]: Actions}, prf: Array) { + return { att: attenuations, prf: prf ? prf : []} +} + +const getAttenuations = (recap: ReCapObject): Record => recap.att + +const formatStatement = (delegee: string, attenuations: string) => + `I further authorize ${delegee} to perform the following actions on my behalf: ${attenuations}` + export class ReCap { _caps: Capablities + _map: Map | undefined constructor(caps:Capablities) { this._caps = caps @@ -23,71 +46,84 @@ export class ReCap { /** * Create ReCap instance from JSON representation */ - static fromJson(json:Map):ReCap { - // + static fromJSON(json:ReCapObject):ReCap { + return new ReCap(Object.entries(getAttenuations(json))) } /** * Create ReCap instance from URI representation, typically from a SIWX message */ - static fromURI(recap: string): Recap { - + static fromURI(recap: string): ReCap { + return ReCap.fromJSON(base64urlToJSON(recap.split(`:`).pop())) } /** * Encode ReCap to ReCap URI (urn:recap:base64url-encoded-recap) to be used in SIWX message */ - encode():string { - // + encodeURI():string { + return `urn:recap:${encodeBase64Url(u8a.fromString(JSON.stringify(this.toJSON())))}` } /** * Query if this ReCap is valid for a given action and resource/target */ - can(action: Actions, target: string):boolean { - + // TODO only support action + // @ts-ignore + can(target: string, action: Actions):boolean { + return this.map.has(target) ? Boolean(this.map.get(target)?.["ceramic/write"]) : false } /** - * Generate the human readable ReCap statement for this ReCap, to be used in SIWX message + * Generate the human readable ReCap statement for this ReCap, to be used in SIWX message. */ - toStatement():string { - + toStatement(delegee: string):string { + const att = this._caps.reduce( + (acc, cap, i) => `${acc} (${i+1}) "${Object.keys(cap[1]).pop()}" for "${cap[0]}".`, + ``) + return formatStatement(delegee, att) } /** * Get JSON representation of ReCap */ - toJson():Map { - + toJSON():ReCapObject { + return formatRecap(Object.fromEntries(this.map), []) + } + + //TODO assummes one action per target here + get map():Map { + if (this._map) return this._map + return this._caps.reduce((acc, cap) => Object.assign(acc, { [cap[0]]: cap[1]}), new Map()) } /** * Add a capability (action & target) to a ReCap, returns new recap */ - addCapability(action: Actions, target: string):ReCap { - + addCapability(target: string, action: Actions):ReCap { + validCeramicTarget(target) + return new ReCap(this._caps.concat([[target, action]])) } /** * Verify that a ReCap Statement is valid for this ReCap and encodes the same delegations */ - verifyStatment(statement:string):boolean { - + // TODO att/caps may not be ordered, unless spec says otherwise + verifyStatment(statement:string, delegee: string):boolean { + return this.toStatement(delegee) === statement } /** * Get array of resources inlcuded in ReCap */ get resourceList(): Array { - + return this._caps.map(cap => cap[0]) } /** * Get array of actions for given resource/target */ - get actionsByResource(resource:string): Array { - + actionsByResource(resource:string): Array { + return this._caps.filter(cap => cap[0] === resource).map(cap => cap[1]) } } \ No newline at end of file diff --git a/packages/recaps/test/recap.test.ts b/packages/recaps/test/recap.test.ts new file mode 100644 index 00000000..cbc76acb --- /dev/null +++ b/packages/recaps/test/recap.test.ts @@ -0,0 +1,3 @@ +describe('ReCaps', () => { + test.todo('Test') +}) diff --git a/packages/recaps/test/siwe.test.ts b/packages/recaps/test/siwe.test.ts deleted file mode 100644 index afbf6e83..00000000 --- a/packages/recaps/test/siwe.test.ts +++ /dev/null @@ -1,3 +0,0 @@ -describe('Cacao Auth Verify', () => { - test.todo('Verify') -})