Skip to content

Commit

Permalink
Ucan-core refactor (#82)
Browse files Browse the repository at this point in the history
* refactor

* fleshing out plugins

* fleshed out plugin api & got all tests running again

* cleaned up plugins

* refactored plugins library based on cryptosystem

* cleaning up, removed KeyType

* patched up DID operations

* cleanup

* Cleanup root package.json

* tsconfigs in root

* PR fixups

* DidableKey

* Inject plugins by passing through fns instead of global state var

* add test for rsa

* minor naming chnage

* setup -> lib

* Moved jest config to root

* v0.0.1-alpha

* v0.0.1-alpha2

* mkBuilder & mkState

* moved tests to ucans

* bump dependencies

* no alpha version

* workspace cmds

* update version
  • Loading branch information
dholms authored Jul 22, 2022
1 parent 3b537b5 commit 6fad150
Show file tree
Hide file tree
Showing 78 changed files with 2,420 additions and 2,410 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ jobs:
- name: Install Dependencies
run: yarn install --frozen-lockfile --network-concurrency 1

- name: Build & Test
- name: Build
run: yarn build

- name: Test
run: yarn test

# Run lint after testing for more info
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = { // eslint-disable-line
export default {
transform: {
".(ts|tsx)": "ts-jest"
},
Expand Down
77 changes: 8 additions & 69 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,87 +1,26 @@
{
"name": "ucans",
"version": "0.10.0",
"description": "Typescript implementation of UCANs",
"author": "Daniel Holmgren <daniel@fission.codes>",
"repository": {
"type": "git",
"url": "https://github.com/fission-suite/ucan"
"url": "https://github.com/ucan-wg/ts-ucan"
},
"homepage": "https://guide.fission.codes",
"license": "Apache-2.0",
"engines": {
"node": ">=15"
},
"scripts": {
"prebuild": "rimraf dist",
"build": "yarn run dist",
"dev": "tsc --watch --module commonjs --outDir ./dist/cjs/ --sourceMap",
"dist": "yarn run dist:prep && yarn run dist:src && yarn run dist:cjs && yarn run dist:esm && yarn run dist:types && yarn run dist:pkg",
"dist:cjs": "tsc --project ./dist/ --module commonjs --outDir ./dist/cjs/ --sourceMap",
"dist:esm": "tsc --project ./dist/ --module es2020 --outDir ./dist/esm/ --sourceMap",
"dist:pkg": "node ./scripts/package.js",
"dist:prep": "copyfiles --error tsconfig.json ./dist/",
"dist:src": "copyfiles --error --up 1 \"./src/**/*\" ./dist/src/",
"dist:types": "tsc --project ./dist/ --emitDeclarationOnly --declaration --declarationDir ./dist/types/",
"lint": "eslint src/**/*.ts src/*.ts tests/**/*.ts tests/*.ts",
"prepare": "yarn build",
"publish-alpha": "yarn publish --tag alpha",
"publish-stable": "yarn publish --tag latest",
"test": "jest",
"test:watch": "jest --watch"
"build": "yarn workspace @ucans/core build && yarn workspace @ucans/default-plugins build && yarn workspace @ucans/ucans build",
"test": "yarn workspace @ucans/core test && yarn workspace @ucans/default-plugins test && yarn workspace @ucans/ucans test",
"lint": "yarn workspace @ucans/core lint && yarn workspace @ucans/default-plugins lint && yarn workspace @ucans/ucans lint"
},
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts"
},
"./*.js": {
"import": "./dist/esm/*.js",
"require": "./dist/cjs/*.js",
"types": "./dist/types/*.d.ts"
},
"./*": {
"import": "./dist/esm/*.js",
"require": "./dist/cjs/*.js",
"types": "./dist/types/*.d.ts"
}
},
"typesVersions": {
"*": {
"index.d.ts": [
"dist/types/index.d.ts"
],
"*": [
"dist/types/*"
]
}
},
"files": [
"dist",
"docs",
"CHANGELOG.md",
"LICENSE",
"README.md"
"private": true,
"workspaces": [
"packages/*"
],
"dependencies": {
"@stablelib/ed25519": "^1.0.2",
"big-integer": "^1.6.51",
"one-webcrypto": "^1.0.3",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"copyfiles": "^2.4.1",
"eslint": "^8.12.0",
"fast-check": "^2.24.0",
"jest": "^27.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3",
"yarn": "^1.22.18"
"ts-node": "^10.8.2"
}
}
3 changes: 3 additions & 0 deletions packages/core/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import baseConfig from "../../jest.config"

export default baseConfig
84 changes: 84 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"name": "@ucans/core",
"version": "0.11.0",
"description": "Core UCAN implementation",
"author": "Daniel Holmgren <daniel@fission.codes>",
"repository": {
"type": "git",
"url": "https://github.com/ucan-wg/ts-ucan"
},
"homepage": "https://guide.fission.codes",
"license": "Apache-2.0",
"engines": {
"node": ">=15"
},
"scripts": {
"prebuild": "rimraf dist",
"build": "yarn run dist",
"dev": "tsc --watch --module commonjs --outDir ./dist/cjs/ --sourceMap",
"dist": "yarn run dist:prep && yarn run dist:src && yarn run dist:cjs && yarn run dist:esm && yarn run dist:types && yarn run dist:pkg",
"dist:cjs": "tsc --project ./dist/ --module commonjs --outDir ./dist/cjs/ --sourceMap",
"dist:esm": "tsc --project ./dist/ --module es2020 --outDir ./dist/esm/ --sourceMap",
"dist:pkg": "node ../../scripts/package.js",
"dist:prep": "copyfiles --error tsconfig.json ./dist/",
"dist:src": "copyfiles --error --up 1 \"./src/**/*\" ./dist/src/",
"dist:types": "tsc --project ./dist/ --emitDeclarationOnly --declaration --declarationDir ./dist/types/",
"lint": "eslint src/**/*.ts src/*.ts tests/**/*.ts tests/*.ts",
"prepare": "yarn build",
"publish-alpha": "yarn publish --tag alpha --access public",
"publish-stable": "yarn publish --tag latest --access public",
"test": "jest",
"test:watch": "jest --watch"
},
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts"
},
"./*.js": {
"import": "./dist/esm/*.js",
"require": "./dist/cjs/*.js",
"types": "./dist/types/*.d.ts"
},
"./*": {
"import": "./dist/esm/*.js",
"require": "./dist/cjs/*.js",
"types": "./dist/types/*.d.ts"
}
},
"typesVersions": {
"*": {
"index.d.ts": [
"dist/types/index.d.ts"
],
"*": [
"dist/types/*"
]
}
},
"files": [
"dist",
"docs",
"CHANGELOG.md",
"LICENSE",
"README.md"
],
"dependencies": {
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"copyfiles": "^2.4.1",
"eslint": "^8.12.0",
"fast-check": "^2.24.0",
"jest": "^27.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3",
"yarn": "^1.22.18"
}
}
40 changes: 23 additions & 17 deletions src/attenuation.ts → packages/core/src/attenuation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as token from "./token.js"
import Plugins from "./plugins.js"
import { Capability } from "./capability/index.js"
import { Ucan } from "./types.js"
import { ResourcePointer } from "./capability/resource-pointer.js"
Expand Down Expand Up @@ -119,19 +120,20 @@ export type OwnershipScope
* out different ways of delegating a capability from the attenuations.
* It also makes it possible to return early if a valid delegation chain has been found.
*/
export async function* delegationChains(
semantics: DelegationSemantics,
ucan: Ucan,
isRevoked: (ucan: Ucan) => Promise<boolean> = async () => false
): AsyncIterable<DelegationChain | Error> {
export const delegationChains = (plugins: Plugins) =>
async function* (
semantics: DelegationSemantics,
ucan: Ucan,
isRevoked: (ucan: Ucan) => Promise<boolean> = async () => false,
): AsyncIterable<DelegationChain | Error> {

if (await isRevoked(ucan)) {
yield new Error(`UCAN Revoked: ${token.encode(ucan)}`)
return
}

yield* capabilitiesFromParenthood(ucan)
yield* capabilitiesFromDelegation(semantics, ucan, isRevoked)
yield* capabilitiesFromDelegation(plugins, semantics, ucan, isRevoked)
}


Expand Down Expand Up @@ -265,14 +267,15 @@ function* capabilitiesFromParenthood(ucan: Ucan): Iterable<DelegationChain> {


async function* capabilitiesFromDelegation(
plugins: Plugins,
semantics: DelegationSemantics,
ucan: Ucan,
isRevoked: (ucan: Ucan) => Promise<boolean>
isRevoked: (ucan: Ucan) => Promise<boolean>,
): AsyncIterable<DelegationChain | Error> {

let proofIndex = 0

for await (const proof of token.validateProofs(ucan)) {
for await (const proof of token.validateProofs(plugins)(ucan)) {
if (proof instanceof Error) {
yield proof
continue
Expand All @@ -283,15 +286,15 @@ async function* capabilitiesFromDelegation(
switch (capability.with.scheme.toLowerCase()) {
case "my": continue // cannot be delegated, only introduced by parenthood.
case "as": {
yield* handleAsDelegation(semantics, capability, ucan, proof, isRevoked)
yield* handleAsDelegation(plugins, semantics, capability, ucan, proof, isRevoked)
break
}
case "prf": {
yield* handlePrfDelegation(semantics, capability, ucan, proof, proofIndex, isRevoked)
yield* handlePrfDelegation(plugins, semantics, capability, ucan, proof, proofIndex, isRevoked)
break
}
default: {
yield* handleNormalDelegation(semantics, capability, ucan, proof, isRevoked)
yield* handleNormalDelegation(plugins, semantics, capability, ucan, proof, isRevoked)
}
}
} catch (e) {
Expand All @@ -313,11 +316,12 @@ async function* capabilitiesFromDelegation(


async function* handleAsDelegation(
plugins: Plugins,
semantics: DelegationSemantics,
capability: Capability,
ucan: Ucan,
proof: Ucan,
isRevoked: (ucan: Ucan) => Promise<boolean>
isRevoked: (ucan: Ucan) => Promise<boolean>,
): AsyncIterable<DelegatedOwnership | Error> {
const split = capability.with.hierPart.split(":")
const scheme = split[ split.length - 1 ]
Expand All @@ -326,7 +330,7 @@ async function* handleAsDelegation(
? SUPERUSER
: { scheme, ability: capability.can }

for await (const delegationChain of delegationChains(semantics, proof, isRevoked)) {
for await (const delegationChain of delegationChains(plugins)(semantics, proof, isRevoked)) {
if (delegationChain instanceof Error) {
yield delegationChain
continue
Expand All @@ -352,12 +356,13 @@ async function* handleAsDelegation(


async function* handlePrfDelegation(
plugins: Plugins,
semantics: DelegationSemantics,
capability: Capability,
ucan: Ucan,
proof: Ucan,
proofIndex: number,
isRevoked: (ucan: Ucan) => Promise<boolean>
isRevoked: (ucan: Ucan) => Promise<boolean>,
): AsyncIterable<DelegatedCapability | Error> {
if (
capability.with.hierPart !== SUPERUSER
Expand All @@ -367,7 +372,7 @@ async function* handlePrfDelegation(
// we only process the delegation if proofIndex === 2
return
}
for await (const delegationChain of delegationChains(semantics, proof, isRevoked)) {
for await (const delegationChain of delegationChains(plugins)(semantics, proof, isRevoked)) {
if (delegationChain instanceof Error) {
yield delegationChain
continue
Expand All @@ -385,13 +390,14 @@ async function* handlePrfDelegation(


async function* handleNormalDelegation(
plugins: Plugins,
semantics: DelegationSemantics,
capability: Capability,
ucan: Ucan,
proof: Ucan,
isRevoked: (ucan: Ucan) => Promise<boolean>
isRevoked: (ucan: Ucan) => Promise<boolean>,
): AsyncIterable<DelegatedCapability | Error> {
for await (const delegationChain of delegationChains(semantics, proof, isRevoked)) {
for await (const delegationChain of delegationChains(plugins)(semantics, proof, isRevoked)) {
if (delegationChain instanceof Error) {
yield delegationChain
continue
Expand Down
Loading

0 comments on commit 6fad150

Please sign in to comment.