Skip to content

Commit

Permalink
fix signing to work with breaking changes in digital bazaar libs
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed Sep 4, 2024
1 parent f8ecb60 commit 8768b7d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 203 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- name: Run test with Node.js ${{ matrix.node-version }}
run: npm run pretest; npm run test-node; npm run posttest
run: npm run test-node
env:
CI: true
# FIXME: fix karma tests
Expand Down
52 changes: 0 additions & 52 deletions karma.conf.js

This file was deleted.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@digitalcredentials/credential-status-manager-db",
"description": "A Typescript library for managing the status of Verifiable Credentials in a Database using Bitstring Status List.",
"version": "0.0.1",
"version": "0.0.1-beta.0",
"license": "MIT",
"engines": {
"node": ">=20.0"
Expand All @@ -25,12 +25,11 @@
"LICENSE.md"
],
"main": "dist/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/index.js"
"import": "./dist/index.js"
}
},
"publishConfig": {
Expand All @@ -44,8 +43,8 @@
"spec": "dist/test/**/*.js"
},
"scripts": {
"build": "npm run clear && tsc -d && tsc -p tsconfig.esm.json",
"build-test": "npm run clear && tsc -d && tsc -p tsconfig.spec.json",
"build": "npm run clear && tsc -p tsconfig.json",
"build-test": "npm run clear && tsc -p tsconfig.spec.json",
"clear": "rimraf dist/*",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
Expand Down
26 changes: 0 additions & 26 deletions post-test.js

This file was deleted.

26 changes: 0 additions & 26 deletions pre-test.js

This file was deleted.

73 changes: 44 additions & 29 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface GetSigningKeysResult {
issuerDid: string;
keyPairs: Map<string, any>;
verificationMethod: string;
signingKey: any;
}

// validates credential
Expand Down Expand Up @@ -122,16 +123,15 @@ export async function signCredential({
didWebUrl
}: SignCredentialOptions): Promise<VerifiableCredential> {
const {
keyPairs,
verificationMethod
signingKey
} = await getSigningMaterial({
didMethod,
didSeed,
didWebUrl
});
const key = keyPairs.get(verificationMethod);

const date = getDateString();
const suite = new Ed25519Signature2020({ key, date });
const suite = new Ed25519Signature2020({ key:signingKey, date });
return sign({
credential,
documentLoader,
Expand All @@ -145,39 +145,54 @@ export async function getSigningMaterial({
didSeed,
didWebUrl
}: GetSigningKeysOptions)
: Promise<GetSigningKeysResult> {
: Promise<GetSigningKeysResult> {
let didDocument;
let keyPairs;
let methodFor;
let signingKey;
let verificationMethod;
const didSeedBytes = decodeSeed(didSeed);
switch (didMethod) {
case DidMethod.Key:
const verificationKeyPair = await Ed25519VerificationKey2020.generate({
seed: didSeedBytes
if (didMethod === DidMethod.Key) {
const verificationKeyPair = await Ed25519VerificationKey2020.generate({
seed: didSeedBytes
});
({ didDocument, keyPairs, methodFor } = await didKeyDriver.fromKeyPair({
verificationKeyPair
}));

const assertionMethod = methodFor({ purpose: 'assertionMethod' })
signingKey = await Ed25519VerificationKey2020.from({
type: assertionMethod.type,
controller: assertionMethod.controller,
id: assertionMethod.id,
publicKeyMultibase: assertionMethod.publicKeyMultibase,
privateKeyMultibase: verificationKeyPair.privateKeyMultibase
})
verificationMethod = extractId(didDocument.assertionMethod[0]);
} else if (didMethod === DidMethod.Web) {
({ didDocument, keyPairs } = await didWebDriver.generate({
seed: didSeedBytes,
url: didWebUrl
}));
verificationMethod = extractId(didDocument.assertionMethod[0]);
signingKey = keyPairs.get(verificationMethod);
} else {
throw new BadRequestError({
message:
'"didMethod" must be one of the following values: ' +
`${Object.values(DidMethod).map(m => `"${m}"`).join(', ')}.`
});
({ didDocument, keyPairs } = await didKeyDriver.fromKeyPair({
verificationKeyPair
}));
break;
case DidMethod.Web:
({ didDocument, keyPairs } = await didWebDriver.generate({
seed: didSeedBytes,
url: didWebUrl
}));
break;
default:
throw new BadRequestError({
message:
'"didMethod" must be one of the following values: ' +
`${Object.values(DidMethod).map(m => `"${m}"`).join(', ')}.`
});
}

const issuerDid = didDocument.id;
const verificationMethod = extractId(didDocument.assertionMethod[0]);


return {
didDocument,
issuerDid,
keyPairs,
verificationMethod
verificationMethod,
signingKey
};
}

Expand All @@ -188,7 +203,7 @@ function decodeSeed(didSeed: string): Uint8Array {
// This is a multibase-encoded seed
didSeedBytes = decodeSecretKeySeed({ secretKeySeed: didSeed });
} else if (didSeed.length >= 32) {
didSeedBytes = (new TextEncoder()).encode(didSeed).slice(0, 32);
didSeedBytes = (new TextEncoder()).encode(didSeed).slice(0, 32);
} else {
throw new InvalidDidSeedError();
}
Expand All @@ -199,7 +214,7 @@ function decodeSeed(didSeed: string): Uint8Array {
function extractId(objectOrString: any): string {
if (typeof objectOrString === 'string') {
return objectOrString;
}
}
return objectOrString.id;
}

Expand Down
26 changes: 0 additions & 26 deletions tsconfig.esm.json

This file was deleted.

8 changes: 3 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"compilerOptions": {
"strict": true,
"target": "es2022",
"lib": ["es2022", "dom"],
"module": "commonjs",
"lib": ["es2022"],
"module": "es2022",
"moduleResolution": "node",
"outDir": "dist",
"noImplicitAny": true,
Expand All @@ -20,9 +20,7 @@
"include": [
"src/**/*",
".eslintrc.js",
"karma.conf.js",
"pre-test.js",
"post-test.js"
"karma.conf.js"
],
"exclude": ["node_modules", "dist", "test"]
}
62 changes: 31 additions & 31 deletions tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
{
"compilerOptions": {
"strict": true,
"target": "es2022",
"lib": [
"es2022",
"dom"
"compilerOptions": {
"strict": true,
"target": "es2022",
"lib": [
"es2022",
"dom"
],
"module": "es2022",
"moduleResolution": "node",
"outDir": "dist",
"noImplicitAny": true,
"removeComments": false,
"preserveConstEnums": true,
"baseUrl": ".",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"ts-node": {
"files": true
},
"include": [
"src/**/*",
"test/**/*",
".eslintrc.js",
"karma.conf.js"
],
"module": "es2022",
"moduleResolution": "node",
"outDir": "dist",
"noImplicitAny": true,
"removeComments": false,
"preserveConstEnums": true,
"baseUrl": ".",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"ts-node": {
"files": true
},
"include": [
"src/**/*",
"test/**/*",
".eslintrc.js",
"karma.conf.js"
],
"exclude": [
"node_modules",
"dist"
]
}
"exclude": [
"node_modules",
"dist"
]
}

0 comments on commit 8768b7d

Please sign in to comment.