diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f4672cc..bfa1c66 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/karma.conf.js b/karma.conf.js deleted file mode 100644 index f546aaf..0000000 --- a/karma.conf.js +++ /dev/null @@ -1,52 +0,0 @@ -/*! - * Copyright (c) 2024 Digital Credentials Consortium. All rights reserved. - */ -module.exports = function (config) { - config.set({ - // base path that will be used to resolve all patterns (eg. files, exclude) - basePath: '', - - frameworks: ['mocha', 'chai', 'karma-typescript'], - - files: [ - 'src/**/*.ts', - 'test/**/*.ts' - ], - - karmaTypescriptConfig: { - bundlerOptions: { - transforms: [ - require('karma-typescript-es6-transform')() - ] - }, - reports: {} - }, - - preprocessors: { - '**/*.ts': 'karma-typescript' - }, - - reporters: ['mocha', 'karma-typescript'], - - browsers: ['ChromeHeadless'], - - // Continuous Integration mode - // if true, Karma captures browsers, runs the test and exits - singleRun: true, - - // level of logging - // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG - logLevel: config.LOG_INFO, - - // Mocha - client: { - mocha: { - // increase from default 2s - timeout: 10000 - // reporter: 'html' - } - }, - - colors: true - }) -} diff --git a/package.json b/package.json index 85178cb..392719e 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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": { @@ -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 .", diff --git a/post-test.js b/post-test.js deleted file mode 100644 index 8a893db..0000000 --- a/post-test.js +++ /dev/null @@ -1,26 +0,0 @@ -const PackageJson = require('@npmcli/package-json'); -const fs = require('fs'); - -// update package.json -const updatePackageJson = async () => { - const pkgJson = await PackageJson.load('./'); - pkgJson.update({ type: 'module' }); - await pkgJson.save(); -}; - -// update tsconfig.spec.json -const updateTsconfig = async () => { - const tsconfigFilePath = './tsconfig.spec.json'; - const tsconfigJson = require(tsconfigFilePath); - tsconfigJson.compilerOptions.module = 'es2022'; - fs.writeFileSync(tsconfigFilePath, JSON.stringify(tsconfigJson, null, 2) + '\n'); -}; - -// combine post-test subscripts -const runPostTest = async () => { - await updatePackageJson(); - await updateTsconfig(); -}; - -// run post-test subscripts -runPostTest(); diff --git a/pre-test.js b/pre-test.js deleted file mode 100644 index 864c427..0000000 --- a/pre-test.js +++ /dev/null @@ -1,26 +0,0 @@ -import PackageJson from '@npmcli/package-json'; -import fs from 'fs'; - -// update package.json -const updatePackageJson = async () => { - const pkgJson = await PackageJson.load('./'); - pkgJson.update({ type: undefined }); - await pkgJson.save(); -}; - -// update tsconfig.spec.json -const updateTsconfig = async () => { - const tsconfigFilePath = './tsconfig.spec.json'; - const tsconfigJson = JSON.parse(fs.readFileSync(tsconfigFilePath)); - tsconfigJson.compilerOptions.module = 'commonjs'; - fs.writeFileSync(tsconfigFilePath, JSON.stringify(tsconfigJson, null, 2) + '\n'); -}; - -// combine pre-test subscripts -const runPreTest = async () => { - await updatePackageJson(); - await updateTsconfig(); -}; - -// run pre-test subscripts -runPreTest(); diff --git a/src/helpers.ts b/src/helpers.ts index 66d92cf..3aa7cf6 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -63,6 +63,7 @@ interface GetSigningKeysResult { issuerDid: string; keyPairs: Map; verificationMethod: string; + signingKey: any; } // validates credential @@ -122,16 +123,15 @@ export async function signCredential({ didWebUrl }: SignCredentialOptions): Promise { 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, @@ -145,39 +145,54 @@ export async function getSigningMaterial({ didSeed, didWebUrl }: GetSigningKeysOptions) -: Promise { + : Promise { 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 }; } @@ -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(); } @@ -199,7 +214,7 @@ function decodeSeed(didSeed: string): Uint8Array { function extractId(objectOrString: any): string { if (typeof objectOrString === 'string') { return objectOrString; - } + } return objectOrString.id; } diff --git a/tsconfig.esm.json b/tsconfig.esm.json deleted file mode 100644 index 8e7f813..0000000 --- a/tsconfig.esm.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "compilerOptions": { - "strict": true, - "target": "es2022", - "lib": ["es2022", "dom"], - "module": "es2022", - "moduleResolution": "node", - "outDir": "dist/esm", - "noImplicitAny": true, - "removeComments": false, - "preserveConstEnums": true, - "baseUrl": ".", - "allowSyntheticDefaultImports": true, - "skipLibCheck": true, - "resolveJsonModule": true - }, - "ts-node": { - "files": true - }, - "include": [ - "src/**/*", - ".eslintrc.js", - "karma.conf.js" - ], - "exclude": ["node_modules", "dist", "test"] -} diff --git a/tsconfig.json b/tsconfig.json index 954d13b..58244c8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,8 @@ "compilerOptions": { "strict": true, "target": "es2022", - "lib": ["es2022", "dom"], - "module": "commonjs", + "lib": ["es2022"], + "module": "es2022", "moduleResolution": "node", "outDir": "dist", "noImplicitAny": true, @@ -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"] } diff --git a/tsconfig.spec.json b/tsconfig.spec.json index 5789033..74fdeb9 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -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" + ] + } \ No newline at end of file