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..0ce021fd --- /dev/null +++ b/packages/recaps/package.json @@ -0,0 +1,54 @@ +{ + "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": { + "dids": "workspace:^3.2.0", + "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..a28f24ac --- /dev/null +++ b/packages/recaps/src/index.ts @@ -0,0 +1,129 @@ +import { encodeBase64Url, base64urlToJSON } from 'did' +import * as u8a from 'uint8arrays' + +type CeramicWrite = { + "ceramic/write": [] +} + +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 + } + + /** + * Create ReCap instance from JSON representation + */ + 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 { + return ReCap.fromJSON(base64urlToJSON(recap.split(`:`).pop())) + } + + /** + * Encode ReCap to ReCap URI (urn:recap:base64url-encoded-recap) to be used in SIWX message + */ + 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 + */ + // 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. + */ + 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():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(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 + */ + // 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 + */ + 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/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