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 2 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 packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"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: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",
Expand Down
64 changes: 0 additions & 64 deletions packages/core/src/did.ts

This file was deleted.

55 changes: 9 additions & 46 deletions packages/core/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import * as uint8arrays from "uint8arrays"
export type DidKeyPlugin = {
prefix: Uint8Array
jwtAlg: string
didToPublicKey: (did: string) => Uint8Array
publicKeyToDid: (pubkey: Uint8Array) => string
verifySignature: (publicKey: Uint8Array, data: Uint8Array, sig: Uint8Array) => Promise<boolean>
verifySignature: (did: string, data: Uint8Array, sig: Uint8Array) => Promise<boolean>
}

export type DidMethodPlugin = {
isMatch: (method: string, did: string) => boolean
checkJwtAlg: (did: string, jwtAlg: string) => boolean
verifySignature: (did: string, data: Uint8Array, sig: Uint8Array) => Promise<boolean>
}

export type Plugins = {
keys: DidKeyPlugin[]
methods: DidMethodPlugin[]
methods: Record<string, DidMethodPlugin>
}

let plugins: Plugins | null = null
Expand All @@ -38,10 +35,9 @@ export const verifyIssuerAlg = (did: string, jwtAlg: string): boolean => {
}
}
} else {
for (const didPlugin of plugins.methods) {
if(didPlugin.isMatch(didMethod, did)) {
return didPlugin.checkJwtAlg(did, jwtAlg)
}
const maybePlugin = plugins.methods[didMethod]
if(maybePlugin) {
return maybePlugin.checkJwtAlg(did, jwtAlg)
}
}
throw new Error(`DID method not supported by plugins: ${did}`)
Expand All @@ -56,51 +52,18 @@ export const verifySignature = async (did: string, data: Uint8Array, sig: Uint8A
const bytes = parsePrefixedBytes(did)
for (const keyPlugin of plugins.keys) {
if(hasPrefix(bytes, keyPlugin.prefix)) {
const publicKey = keyPlugin.didToPublicKey(did)
return keyPlugin.verifySignature(publicKey, data, sig)
return keyPlugin.verifySignature(did, data, sig)
}
}
} else {
for (const didPlugin of plugins.methods) {
if(didPlugin.isMatch(didMethod, did)) {
return didPlugin.verifySignature(did, data, sig)
}
}
}
throw new Error(`DID method not supported by plugins: ${did}`)
}

export const didToPublicKeyBytes = (did: string): { publicKey: Uint8Array; jwtAlg: string } => {
if(plugins === null) {
throw new Error("No plugins loaded")
}
const didMethod = parseDidMethod(did)
if(didMethod === "key") {
const bytes = parsePrefixedBytes(did)
for (const keyPlugin of plugins.keys) {
if(hasPrefix(bytes, keyPlugin.prefix)) {
return {
publicKey: keyPlugin.didToPublicKey(did),
jwtAlg: keyPlugin.jwtAlg
}
}
const maybePlugin = plugins.methods[didMethod]
if (maybePlugin) {
return maybePlugin.verifySignature(did, data, sig)
}
}
throw new Error(`DID method not supported by plugins: ${did}`)
}

export const publicKeyBytesToDid = (publicKey: Uint8Array, jwtAlg: string): string => {
if(plugins === null) {
throw new Error("No plugins loaded")
}
for(const keyPlugin of plugins.keys) {
if(keyPlugin.jwtAlg === jwtAlg) {
return keyPlugin.publicKeyToDid(publicKey)
}
}
throw new Error(`JWT alg not supported by plugins: ${jwtAlg}`)
}

export const hasPrefix = (
prefixedKey: Uint8Array,
prefix: Uint8Array
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ export type Fact = Record<string, unknown>

// CRYPTOGRAPHY


/** Unlike tslib's CryptoKeyPair, this requires the `privateKey` and `publicKey` fields */

export interface Didable {
did: () => string
}
Expand Down
68 changes: 0 additions & 68 deletions packages/core/tests/did.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/plugins/src/default-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { rsaPlugin, rsaOldPlugin } from "./rsa/plugin.js"

export const defaults: Plugins = {
keys: [ed25519Plugin, p256Plugin, rsaPlugin, rsaOldPlugin],
methods: [],
methods: {},
}
2 changes: 1 addition & 1 deletion packages/plugins/src/ed25519/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EDWARDS_DID_PREFIX } from "../prefixes"
import { didFromKeyBytes, keyBytesFromDid } from "../util"

export const didToPublickey = (did: string): Uint8Array => {
export const didToPublicKey = (did: string): Uint8Array => {
return keyBytesFromDid(did, EDWARDS_DID_PREFIX)
}

Expand Down
5 changes: 2 additions & 3 deletions packages/plugins/src/ed25519/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { EDWARDS_DID_PREFIX } from "../prefixes.js"
export const ed25519Plugin: DidKeyPlugin = {
prefix: EDWARDS_DID_PREFIX,
jwtAlg: "EdDSA",
didToPublicKey: crypto.didToPublickey,
publicKeyToDid: crypto.publicKeyToDid,
verifySignature: async (publicKey: Uint8Array, data: Uint8Array, sig: Uint8Array) => {
verifySignature: async (did: string, data: Uint8Array, sig: Uint8Array) => {
const publicKey = crypto.didToPublicKey(did)
return ed25519.verify(publicKey, data, sig)
}
}
7 changes: 4 additions & 3 deletions packages/plugins/src/p256/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { P256_DID_PREFIX } from "../prefixes.js"
export const p256Plugin: DidKeyPlugin = {
prefix: P256_DID_PREFIX,
jwtAlg: "ES256",
didToPublicKey: crypto.didToPublicKey,
publicKeyToDid: crypto.publicKeyToDid,
verifySignature: crypto.verify,
verifySignature: async (did: string, data: Uint8Array, sig: Uint8Array) => {
const publicKey = crypto.didToPublicKey(did)
return crypto.verify(publicKey, data, sig)
}
}
14 changes: 8 additions & 6 deletions packages/plugins/src/rsa/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { RSA_DID_PREFIX, RSA_DID_PREFIX_OLD } from "../prefixes.js"
export const rsaPlugin: DidKeyPlugin = {
prefix: RSA_DID_PREFIX,
jwtAlg: "RS256",
didToPublicKey: crypto.didToPublicKey,
publicKeyToDid: crypto.publicKeyToDid,
verifySignature: crypto.verify,
verifySignature: async (did: string, data: Uint8Array, sig: Uint8Array) => {
const publicKey = crypto.didToPublicKey(did)
return crypto.verify(publicKey, data, sig)
}
}

export const rsaOldPlugin: DidKeyPlugin = {
prefix: RSA_DID_PREFIX_OLD,
jwtAlg: "RS256",
didToPublicKey: crypto.oldDidToPublicKey,
publicKeyToDid: crypto.publicKeyToOldDid,
verifySignature: crypto.verify,
verifySignature: async (did: string, data: Uint8Array, sig: Uint8Array) => {
const publicKey = crypto.oldDidToPublicKey(did)
return crypto.verify(publicKey, data, sig)
}
}

10 changes: 1 addition & 9 deletions packages/plugins/tests/ecdsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@ describe("ecdsa", () => {
expect(keypair.jwtAlg).toEqual("ES256")
})

it("can transform between DID & public key", () => {
const did = keypair.did()
const publicKey = p256Plugin.didToPublicKey(did)
const didAgain = p256Plugin.publicKeyToDid(publicKey)
expect(did).toEqual(didAgain)
})

it("signs data", async () => {
signature = await keypair.sign(data)
})

it("can verify signature", async () => {
const publicKey = p256Plugin.didToPublicKey(keypair.did())
const isValid = await p256Plugin.verifySignature(publicKey, data, signature)
const isValid = await p256Plugin.verifySignature(keypair.did(), data, signature)
expect(isValid).toBeTruthy()
})

Expand Down
10 changes: 1 addition & 9 deletions packages/plugins/tests/ed25519.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@ describe("ed25519", () => {
expect(keypair.jwtAlg).toEqual("EdDSA")
})

it("can transform between DID & public key", () => {
const did = keypair.did()
const publicKey = ed25519Plugin.didToPublicKey(did)
const didAgain = ed25519Plugin.publicKeyToDid(publicKey)
expect(did).toEqual(didAgain)
})

it("signs data", async () => {
signature = await keypair.sign(data)
})

it("can verify signature", async () => {
const publicKey = ed25519Plugin.didToPublicKey(keypair.did())
const isValid = await ed25519Plugin.verifySignature(publicKey, data, signature)
const isValid = await ed25519Plugin.verifySignature(keypair.did(), data, signature)
expect(isValid).toBeTruthy()
})

Expand Down
10 changes: 1 addition & 9 deletions packages/plugins/tests/rsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ describe("rsa", () => {
expect(keypair.jwtAlg).toEqual("RS256")
})

it("returns a publicKeyStr and did", () => {
const did = keypair.did()
const publicKey = rsaPlugin.didToPublicKey(did)
const didAgain = rsaPlugin.publicKeyToDid(publicKey)
expect(did).toEqual(didAgain)
})

it("signs data", async () => {
signature = await keypair.sign(data)
})

it("can verify signature", async () => {
const publicKey = rsaPlugin.didToPublicKey(keypair.did())
const isValid = await rsaPlugin.verifySignature(publicKey, data, signature)
const isValid = await rsaPlugin.verifySignature(keypair.did(), data, signature)
expect(isValid).toBeTruthy()
})

Expand Down
5 changes: 5 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["**/*.ts"],
"exclude": []
}
Loading