Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ucan-core refactor #82

Merged
merged 26 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
75 changes: 5 additions & 70 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,18 @@
"author": "Daniel Holmgren <[email protected]>",
"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"
},
"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
85 changes: 85 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"name": "@ucans/core",
"version": "0.0.1-alpha2",
"description": "Core UCAN implementation",
"author": "Daniel Holmgren <[email protected]>",
"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",
"@ucans/plugins": "*",
"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