From 4304deac8c63e7394d5361896d36496b6ab9a9ab Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Wed, 31 Jul 2024 11:43:40 -0400 Subject: [PATCH] refactor: Refactored exceptions (#263) * Refactored exceptions * Synced exceptions in keymaster-app * Added new test for verifyDb --- src/db-json.js | 4 +- src/db-mongodb.js | 6 +- src/db-sqlite.js | 6 +- src/db-wallet-json.js | 4 +- src/exceptions.js | 22 +- src/gatekeeper-lib.js | 52 ++-- src/gatekeeper.test.js | 133 +++++----- src/keymaster-app/src/db-wallet-web.js | 4 +- src/keymaster-app/src/exceptions.js | 22 +- src/keymaster-app/src/keymaster-lib.js | 121 +++++----- src/keymaster-lib.js | 121 +++++----- src/keymaster.test.js | 322 ++++++++++++------------- 12 files changed, 418 insertions(+), 399 deletions(-) diff --git a/src/db-json.js b/src/db-json.js index 878f519f..4fc1847f 100644 --- a/src/db-json.js +++ b/src/db-json.js @@ -40,7 +40,7 @@ export async function addEvent(did, event) { const db = loadDb(); if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const suffix = did.split(':').pop(); @@ -75,7 +75,7 @@ export async function getEvents(did) { export async function setEvents(did, events) { if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const db = loadDb(); diff --git a/src/db-mongodb.js b/src/db-mongodb.js index 315b352e..1a38c50a 100644 --- a/src/db-mongodb.js +++ b/src/db-mongodb.js @@ -22,7 +22,7 @@ export async function resetDb() { export async function addEvent(did, event) { if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const id = did.split(':').pop(); @@ -38,7 +38,7 @@ export async function addEvent(did, event) { export async function getEvents(did) { if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } try { @@ -55,7 +55,7 @@ export async function getEvents(did) { export async function deleteEvents(did) { if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const id = did.split(':').pop(); diff --git a/src/db-sqlite.js b/src/db-sqlite.js index 8c7d5db4..d5be3d95 100644 --- a/src/db-sqlite.js +++ b/src/db-sqlite.js @@ -35,7 +35,7 @@ export async function resetDb() { export async function addEvent(did, event) { if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const events = await getEvents(did); @@ -50,7 +50,7 @@ export async function addEvent(did, event) { export async function getEvents(did) { if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } try { @@ -67,7 +67,7 @@ export async function getEvents(did) { export async function deleteEvents(did) { if (!did) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const id = did.split(':').pop(); diff --git a/src/db-wallet-json.js b/src/db-wallet-json.js index 5a0641a8..d447fb42 100644 --- a/src/db-wallet-json.js +++ b/src/db-wallet-json.js @@ -26,7 +26,7 @@ export function loadWallet() { export function newWallet(mnemonic, overwrite) { if (fs.existsSync(walletName) && !overwrite) { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } try { @@ -50,6 +50,6 @@ export function newWallet(mnemonic, overwrite) { return wallet; } catch (error) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } diff --git a/src/exceptions.js b/src/exceptions.js index 5a0bc1dd..6a687b27 100644 --- a/src/exceptions.js +++ b/src/exceptions.js @@ -1,11 +1,11 @@ -export const INVALID_DID = new Error('Invalid DID'); -export const UNKNOWN_DID = new Error('Unknown DID'); -export const UNKNOWN_ID = new Error('Unknown ID'); -export const INVALID_UPDATE = new Error('Invalid update'); -export const INVALID_OPERATION = new Error('Invalid operation'); -export const INVALID_VERSION = new Error('Invalid version'); -export const INVALID_TYPE = new Error('Invalid type'); -export const INVALID_REGISTRY = new Error('Invalid registry'); -export const INVALID_PARAMETER = new Error('Invalid parameter'); -export const UPDATE_FAILED = new Error('Update failed'); -export const EXPECTED_EXCEPTION = new Error('Expected to throw an exception'); +export const INVALID_DID = 'Invalid DID'; +export const INVALID_OPERATION = 'Invalid operation'; +export const INVALID_VERSION = 'Invalid version'; +export const INVALID_TYPE = 'Invalid type'; +export const INVALID_REGISTRY = 'Invalid registry'; + +export const UNKNOWN_ID = 'Unknown ID'; +export const NO_CURRENT_ID = 'No current ID'; +export const INVALID_PARAMETER = 'Invalid parameter'; +export const UPDATE_FAILED = 'Update failed'; +export const EXPECTED_EXCEPTION = 'Expected to throw an exception'; diff --git a/src/gatekeeper-lib.js b/src/gatekeeper-lib.js index fcc3c9ec..3f7b3156 100644 --- a/src/gatekeeper-lib.js +++ b/src/gatekeeper-lib.js @@ -86,11 +86,11 @@ export async function anchorSeed(seed) { async function verifyCreateAgent(operation) { if (!operation.signature) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } if (!operation.publicJwk) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } const operationCopy = JSON.parse(JSON.stringify(operation)); @@ -102,13 +102,13 @@ async function verifyCreateAgent(operation) { async function verifyCreateAsset(operation) { if (operation.controller !== operation.signature?.signer) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } const doc = await resolveDID(operation.signature.signer, { confirm: true, atTime: operation.signature.signed }); if (doc.mdip.registry === 'local' && operation.mdip.registry !== 'local') { - throw exceptions.INVALID_REGISTRY; + throw new Error(exceptions.INVALID_REGISTRY); } const operationCopy = JSON.parse(JSON.stringify(operation)); @@ -121,28 +121,28 @@ async function verifyCreateAsset(operation) { async function verifyCreate(operation) { if (operation?.type !== "create") { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } if (!operation.created) { // TBD ensure valid timestamp format - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } if (!operation.mdip) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } if (!validVersions.includes(operation.mdip.version)) { - throw exceptions.INVALID_VERSION; + throw new Error(exceptions.INVALID_VERSION); } if (!validTypes.includes(operation.mdip.type)) { - throw exceptions.INVALID_TYPE; + throw new Error(exceptions.INVALID_TYPE); } if (!validRegistries.includes(operation.mdip.registry)) { - throw exceptions.INVALID_REGISTRY; + throw new Error(exceptions.INVALID_REGISTRY); } if (operation.mdip.type === 'agent') { @@ -153,7 +153,7 @@ async function verifyCreate(operation) { return verifyCreateAsset(operation); } - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } export async function createDID(operation) { @@ -183,7 +183,7 @@ export async function createDID(operation) { return did; } else { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } } @@ -289,7 +289,7 @@ async function verifyUpdate(operation, doc) { } export async function resolveDID(did, { atTime, atVersion, confirm, verify } = {}) { - const confirmedCacheable = confirm && !atTime && !atVersion; + const confirmedCacheable = !!confirm && !atTime && !atVersion; const unconfirmedCacheable = !confirm && !atTime && !atVersion; if (confirmedCacheable && !verify && confirmedCache[did]) { @@ -303,7 +303,7 @@ export async function resolveDID(did, { atTime, atVersion, confirm, verify } = { const events = await db.getEvents(did); if (events.length === 0) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const anchor = events[0]; @@ -311,7 +311,7 @@ export async function resolveDID(did, { atTime, atVersion, confirm, verify } = { let mdip = doc?.mdip; if (!mdip) { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } if (atTime && new Date(mdip.created) > new Date(atTime)) { @@ -343,7 +343,7 @@ export async function resolveDID(did, { atTime, atVersion, confirm, verify } = { const valid = await verifyUpdate(operation, doc); if (!valid) { - throw exceptions.INVALID_UPDATE; + throw new Error(exceptions.INVALID_OPERATION); } } @@ -371,7 +371,7 @@ export async function resolveDID(did, { atTime, atVersion, confirm, verify } = { } else { if (verify) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } console.error(`unknown type ${operation.type}`); @@ -484,7 +484,7 @@ export async function exportDIDs(dids) { export async function removeDIDs(dids) { if (!Array.isArray(dids)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } for (const did of dids) { @@ -533,7 +533,7 @@ async function importUpdateEvent(event) { export async function importEvent(event) { if (!event.registry || !event.time || !event.operation) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } let did; @@ -547,11 +547,11 @@ export async function importEvent(event) { } if (!did) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } } catch { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } const current = await exportDID(did); @@ -560,7 +560,7 @@ export async function importEvent(event) { const ok = await importCreateEvent(event); if (!ok) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } return true; @@ -593,7 +593,7 @@ export async function importEvent(event) { const ok = await importUpdateEvent(event); if (!ok) { - throw exceptions.INVALID_OPERATION; + throw new Error(exceptions.INVALID_OPERATION); } delete confirmedCache[did]; @@ -603,7 +603,7 @@ export async function importEvent(event) { export async function importBatch(batch) { if (!batch || !Array.isArray(batch) || batch.length < 1) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } let verified = 0; @@ -638,7 +638,7 @@ export async function importBatch(batch) { export async function getQueue(registry) { if (!validRegistries.includes(registry)) { - throw exceptions.INVALID_REGISTRY; + throw new Error(exceptions.INVALID_REGISTRY); } return db.getQueue(registry); @@ -646,7 +646,7 @@ export async function getQueue(registry) { export async function clearQueue(registry, events) { if (!validRegistries.includes(registry)) { - throw exceptions.INVALID_REGISTRY; + throw new Error(exceptions.INVALID_REGISTRY); } return db.clearQueue(registry, events); diff --git a/src/gatekeeper.test.js b/src/gatekeeper.test.js index c3dddc47..3935e92b 100644 --- a/src/gatekeeper.test.js +++ b/src/gatekeeper.test.js @@ -163,9 +163,9 @@ describe('createDID', () => { try { await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_VERSION); + expect(error.message).toBe(exceptions.INVALID_VERSION); } }); @@ -177,9 +177,9 @@ describe('createDID', () => { try { await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_REGISTRY); + expect(error.message).toBe(exceptions.INVALID_REGISTRY); } }); @@ -192,9 +192,9 @@ describe('createDID', () => { try { await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_TYPE); + expect(error.message).toBe(exceptions.INVALID_TYPE); } }); @@ -203,9 +203,9 @@ describe('createDID', () => { try { await gatekeeper.createDID(); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } const keypair = cipher.generateRandomJwk(); @@ -214,45 +214,45 @@ describe('createDID', () => { const agentOp = await createAgentOp(keypair); agentOp.type = 'mock'; await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } try { const agentOp = await createAgentOp(keypair); agentOp.mdip = null; await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } try { const agentOp = await createAgentOp(keypair); agentOp.created = null; await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } try { const agentOp = await createAgentOp(keypair); agentOp.signature = null; await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } try { const agentOp = await createAgentOp(keypair); agentOp.publicJwk = null; await gatekeeper.createDID(agentOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } }); @@ -279,28 +279,28 @@ describe('createDID', () => { try { const assetOp = await createAssetOp(agent, keypair, 'hyperswarm'); await gatekeeper.createDID(assetOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { // Can't let local IDs create assets on other registries - expect(error).toBe(exceptions.INVALID_REGISTRY); + expect(error.message).toBe(exceptions.INVALID_REGISTRY); } try { const assetOp = await createAssetOp(agent, keypair, 'hyperswarm'); assetOp.controller = 'mock'; await gatekeeper.createDID(assetOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } try { const assetOp = await createAssetOp(agent, keypair, 'hyperswarm'); assetOp.signature = null; await gatekeeper.createDID(assetOp); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_OPERATION); + expect(error.message).toBe(exceptions.INVALID_OPERATION); } }); }); @@ -688,65 +688,65 @@ describe('resolveDID', () => { try { await gatekeeper.resolveDID(); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID(''); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID('mock'); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID([]); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID([1, 2, 3]); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID({}); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID({ mock: 1 }); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID('did:test:xxx'); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await gatekeeper.resolveDID('did:test:z3v8Auah2NPDigFc3qKx183QKL6vY8fJYQk6NeLz7KF2RFtC9c8'); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); }); @@ -937,9 +937,9 @@ describe('removeDIDs', () => { try { await gatekeeper.resolveDID(did); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); @@ -948,9 +948,9 @@ describe('removeDIDs', () => { try { await gatekeeper.removeDIDs(); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -1151,9 +1151,9 @@ describe('importBatch', () => { try { await gatekeeper.importBatch(); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -1162,9 +1162,9 @@ describe('importBatch', () => { try { await gatekeeper.importBatch('mock'); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -1173,9 +1173,9 @@ describe('importBatch', () => { try { await gatekeeper.importBatch([]); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -1217,9 +1217,9 @@ describe('getQueue', () => { try { await gatekeeper.getQueue('mock'); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_REGISTRY); + expect(error.message).toBe(exceptions.INVALID_REGISTRY); } }); }); @@ -1314,9 +1314,9 @@ describe('clearQueue', () => { try { await gatekeeper.clearQueue('mock', []); - throw exceptions.EXPECTED_EXCEPTION; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_REGISTRY); + expect(error.message).toBe(exceptions.INVALID_REGISTRY); } }); }); @@ -1552,4 +1552,25 @@ describe('verifyDb', () => { expect(invalid).toBe(0); }); + + it('should removed invalid DIDs', async () => { + mockFs({}); + + const keypair = cipher.generateRandomJwk(); + const agentOp = await createAgentOp(keypair); + const agentDID = await gatekeeper.createDID(agentOp); + const assetOp = await createAssetOp(agentDID, keypair); + const assetDID = await gatekeeper.createDID(assetOp); + const doc = await gatekeeper.resolveDID(assetDID); + doc.didDocumentData = { mock: 1 }; + const updateOp = await createUpdateOp(keypair, assetDID, doc); + const ok = await gatekeeper.updateDID(updateOp); + expect(ok).toBe(true); + + // Can't verify a DID that has been updated if the controller is removed + await gatekeeper.removeDIDs([agentDID]); + const invalid = await gatekeeper.verifyDb(false); + + expect(invalid).toBe(1); + }); }); diff --git a/src/keymaster-app/src/db-wallet-web.js b/src/keymaster-app/src/db-wallet-web.js index f868baf8..49376848 100644 --- a/src/keymaster-app/src/db-wallet-web.js +++ b/src/keymaster-app/src/db-wallet-web.js @@ -20,7 +20,7 @@ export function loadWallet() { export function newWallet(mnemonic, overwrite = false) { if (!overwrite && window.localStorage.getItem(walletName)) { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } try { @@ -44,6 +44,6 @@ export function newWallet(mnemonic, overwrite = false) { return wallet; } catch (error) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } diff --git a/src/keymaster-app/src/exceptions.js b/src/keymaster-app/src/exceptions.js index 5a0bc1dd..6a687b27 100644 --- a/src/keymaster-app/src/exceptions.js +++ b/src/keymaster-app/src/exceptions.js @@ -1,11 +1,11 @@ -export const INVALID_DID = new Error('Invalid DID'); -export const UNKNOWN_DID = new Error('Unknown DID'); -export const UNKNOWN_ID = new Error('Unknown ID'); -export const INVALID_UPDATE = new Error('Invalid update'); -export const INVALID_OPERATION = new Error('Invalid operation'); -export const INVALID_VERSION = new Error('Invalid version'); -export const INVALID_TYPE = new Error('Invalid type'); -export const INVALID_REGISTRY = new Error('Invalid registry'); -export const INVALID_PARAMETER = new Error('Invalid parameter'); -export const UPDATE_FAILED = new Error('Update failed'); -export const EXPECTED_EXCEPTION = new Error('Expected to throw an exception'); +export const INVALID_DID = 'Invalid DID'; +export const INVALID_OPERATION = 'Invalid operation'; +export const INVALID_VERSION = 'Invalid version'; +export const INVALID_TYPE = 'Invalid type'; +export const INVALID_REGISTRY = 'Invalid registry'; + +export const UNKNOWN_ID = 'Unknown ID'; +export const NO_CURRENT_ID = 'No current ID'; +export const INVALID_PARAMETER = 'Invalid parameter'; +export const UPDATE_FAILED = 'Update failed'; +export const EXPECTED_EXCEPTION = 'Expected to throw an exception'; diff --git a/src/keymaster-app/src/keymaster-lib.js b/src/keymaster-app/src/keymaster-lib.js index 1d3984df..d5574649 100644 --- a/src/keymaster-app/src/keymaster-lib.js +++ b/src/keymaster-app/src/keymaster-lib.js @@ -349,7 +349,7 @@ export function setCurrentId(name) { saveWallet(wallet); } else { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.UNKNOWN_ID); } } @@ -376,12 +376,12 @@ function fetchId(id) { idInfo = wallet.ids[wallet.current]; if (!idInfo) { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.UNKNOWN_ID); } } if (!idInfo) { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.UNKNOWN_ID); } return idInfo; @@ -408,8 +408,7 @@ async function fetchKeyPair(name = null) { if (keypair.publicJwk.x === confirmedPublicKeyJwk.x && keypair.publicJwk.y === confirmedPublicKeyJwk.y - ) - { + ) { return keypair; } } @@ -441,7 +440,7 @@ export async function decrypt(did) { const crypt = await resolveAsset(did); if (!crypt || !crypt.cipher_hash) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const doc = await resolveDID(crypt.sender, { confirm: true, atTime: crypt.created }); @@ -496,7 +495,7 @@ export async function addSignature(obj, controller = null) { }; } catch (error) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } @@ -629,7 +628,7 @@ export async function resolveAsset(did) { export async function createId(name, registry = defaultRegistry) { const wallet = loadWallet(); if (wallet.ids && Object.keys(wallet.ids).includes(name)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const account = wallet.counter; @@ -692,7 +691,7 @@ export function removeId(name) { return true; } else { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.UNKNOWN_ID); } } @@ -739,7 +738,7 @@ export async function recoverId(did) { return `Recovered ${data.name}!`; } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } @@ -789,11 +788,11 @@ export function addName(name, did) { } if (Object.keys(wallet.names).includes(name)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (Object.keys(wallet.ids).includes(name)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } wallet.names[name] = did; @@ -820,7 +819,7 @@ export function lookupDID(name) { } } catch { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const wallet = loadWallet(); @@ -833,7 +832,7 @@ export function lookupDID(name) { return wallet.ids[name].did; } - throw exceptions.UNKNOWN_DID; + throw new Error(exceptions.UNKNOWN_ID); } export async function createAsset(data, registry = defaultRegistry, owner = null) { @@ -847,7 +846,7 @@ export async function createAsset(data, registry = defaultRegistry, owner = null } if (isEmpty(data)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const id = fetchId(owner); @@ -911,7 +910,7 @@ export async function issueCredential(vc, registry = defaultRegistry) { const id = fetchId(); if (vc.issuer !== id.did) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } // Don't allow credentials that will be garbage-collected @@ -959,7 +958,7 @@ export async function acceptCredential(did) { const vc = await decryptJSON(credential); if (vc.credentialSubject.id !== id.did) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return addToHeld(credential); @@ -987,7 +986,7 @@ export async function publishCredential(did, reveal = false) { const vc = await decryptJSON(credential); if (vc.credentialSubject.id !== id.did) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const doc = await resolveDID(id.did); @@ -1009,7 +1008,7 @@ export async function publishCredential(did, reveal = false) { return vc; } else { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } } @@ -1026,7 +1025,7 @@ export async function unpublishCredential(did) { return `OK credential ${did} removed from manifest`; } - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } export async function createChallenge(challenge, registry = ephemeralRegistry) { @@ -1042,11 +1041,11 @@ export async function createChallenge(challenge, registry = ephemeralRegistry) { } if (!challenge.credentials) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!Array.isArray(challenge.credentials)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return createAsset(challenge, registry); @@ -1098,7 +1097,7 @@ export async function createResponse(did, registry = ephemeralRegistry) { const challenge = lookupDID(did); if (!challenge) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const doc = await resolveDID(challenge); @@ -1106,7 +1105,7 @@ export async function createResponse(did, registry = ephemeralRegistry) { const { credentials } = await resolveAsset(challenge); if (!credentials) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const matches = []; @@ -1120,7 +1119,7 @@ export async function createResponse(did, registry = ephemeralRegistry) { } if (!matches) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const pairs = []; @@ -1154,7 +1153,7 @@ export async function verifyResponse(responseDID, challengeDID) { challengeDID = lookupDID(challengeDID); if (!responseDID) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const response = await decryptJSON(responseDID); @@ -1232,7 +1231,7 @@ export async function getGroup(id) { const isGroup = await groupTest(id); if (!isGroup) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return resolveAsset(id); @@ -1244,7 +1243,7 @@ export async function groupAdd(groupId, memberId) { const data = doc.didDocumentData; if (!data.members || !Array.isArray(data.members)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const memberDID = lookupDID(memberId); @@ -1254,7 +1253,7 @@ export async function groupAdd(groupId, memberId) { await resolveDID(memberDID); } catch { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } // If already a member, return immediately @@ -1264,14 +1263,14 @@ export async function groupAdd(groupId, memberId) { // Can't add a group to itself if (memberDID === groupDID) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } // Can't add a mutual membership relation const isMember = await groupTest(memberId, groupId); if (isMember) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const members = new Set(data.members); @@ -1281,7 +1280,7 @@ export async function groupAdd(groupId, memberId) { const ok = await updateDID(groupDID, doc); if (!ok) { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } return data; @@ -1293,7 +1292,7 @@ export async function groupRemove(groupId, memberId) { const data = doc.didDocumentData; if (!data.members) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const memberDID = lookupDID(memberId); @@ -1303,7 +1302,7 @@ export async function groupRemove(groupId, memberId) { await resolveDID(memberDID); } catch { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } // If not already a member, return immediately @@ -1318,7 +1317,7 @@ export async function groupRemove(groupId, memberId) { const ok = await updateDID(groupDID, doc); if (!ok) { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } return data; @@ -1383,14 +1382,14 @@ export const defaultSchema = { function validateSchema(schema) { try { if (!Object.keys(schema).includes('$schema')) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } // Attempt to instantiate the schema JSONSchemaFaker.generate(schema); } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return true; @@ -1438,7 +1437,7 @@ export async function createTemplate(id) { const isSchema = await testSchema(id); if (!isSchema) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const schemaDID = lookupDID(id); @@ -1467,48 +1466,48 @@ export async function pollTemplate() { export async function createPoll(poll) { if (poll.type !== 'poll') { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (poll.version !== 1) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.description) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.options || !Array.isArray(poll.options) || poll.options.length < 2 || poll.options.length > 10) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.roster) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } try { const isValidGroup = await groupTest(poll.roster); if (!isValidGroup) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.deadline) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const deadline = new Date(poll.deadline); if (isNaN(deadline.getTime())) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (deadline < new Date()) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return createAsset(poll); @@ -1521,7 +1520,7 @@ export async function viewPoll(poll) { const data = doc.didDocumentData; if (!data || !data.options || !data.deadline) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } let hasVoted = false; @@ -1605,11 +1604,11 @@ export async function votePoll(poll, vote, spoil = false) { const owner = doc.didDocument.controller; if (!eligible) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (expired) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } let ballot; @@ -1625,7 +1624,7 @@ export async function votePoll(poll, vote, spoil = false) { vote = parseInt(vote); if (!Number.isInteger(vote) || vote < 1 || vote > max) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } ballot = { @@ -1651,11 +1650,11 @@ export async function updatePoll(ballot) { dataBallot = await decryptJSON(didBallot); if (!dataBallot.poll || !dataBallot.vote) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const didPoll = lookupDID(dataBallot.poll); @@ -1664,26 +1663,26 @@ export async function updatePoll(ballot) { const didOwner = docPoll.didDocument.controller; if (id.did !== didOwner) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const eligible = await groupTest(dataPoll.roster, didVoter); if (!eligible) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const expired = (Date(dataPoll.deadline) > new Date()); if (expired) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const max = dataPoll.options.length; const vote = parseInt(dataBallot.vote); if (!vote || vote < 0 || vote > max) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!dataPoll.ballots) { @@ -1705,13 +1704,13 @@ export async function publishPoll(poll, reveal = false) { const owner = doc.didDocument.controller; if (id.did !== owner) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const view = await viewPoll(poll); if (!view.results.final) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!reveal) { @@ -1730,7 +1729,7 @@ export async function unpublishPoll(poll) { const owner = doc.didDocument.controller; if (id.did !== owner) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } delete doc.didDocumentData.results; diff --git a/src/keymaster-lib.js b/src/keymaster-lib.js index 1d3984df..4ceb16f1 100644 --- a/src/keymaster-lib.js +++ b/src/keymaster-lib.js @@ -349,7 +349,7 @@ export function setCurrentId(name) { saveWallet(wallet); } else { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.UNKNOWN_ID); } } @@ -376,12 +376,12 @@ function fetchId(id) { idInfo = wallet.ids[wallet.current]; if (!idInfo) { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.NO_CURRENT_ID); } } if (!idInfo) { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.UNKNOWN_ID); } return idInfo; @@ -408,8 +408,7 @@ async function fetchKeyPair(name = null) { if (keypair.publicJwk.x === confirmedPublicKeyJwk.x && keypair.publicJwk.y === confirmedPublicKeyJwk.y - ) - { + ) { return keypair; } } @@ -441,7 +440,7 @@ export async function decrypt(did) { const crypt = await resolveAsset(did); if (!crypt || !crypt.cipher_hash) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const doc = await resolveDID(crypt.sender, { confirm: true, atTime: crypt.created }); @@ -496,7 +495,7 @@ export async function addSignature(obj, controller = null) { }; } catch (error) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } @@ -629,7 +628,7 @@ export async function resolveAsset(did) { export async function createId(name, registry = defaultRegistry) { const wallet = loadWallet(); if (wallet.ids && Object.keys(wallet.ids).includes(name)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const account = wallet.counter; @@ -692,7 +691,7 @@ export function removeId(name) { return true; } else { - throw exceptions.UNKNOWN_ID; + throw new Error(exceptions.UNKNOWN_ID); } } @@ -739,7 +738,7 @@ export async function recoverId(did) { return `Recovered ${data.name}!`; } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } @@ -789,11 +788,11 @@ export function addName(name, did) { } if (Object.keys(wallet.names).includes(name)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (Object.keys(wallet.ids).includes(name)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } wallet.names[name] = did; @@ -820,7 +819,7 @@ export function lookupDID(name) { } } catch { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } const wallet = loadWallet(); @@ -833,7 +832,7 @@ export function lookupDID(name) { return wallet.ids[name].did; } - throw exceptions.UNKNOWN_DID; + throw new Error(exceptions.UNKNOWN_ID); } export async function createAsset(data, registry = defaultRegistry, owner = null) { @@ -847,7 +846,7 @@ export async function createAsset(data, registry = defaultRegistry, owner = null } if (isEmpty(data)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const id = fetchId(owner); @@ -911,7 +910,7 @@ export async function issueCredential(vc, registry = defaultRegistry) { const id = fetchId(); if (vc.issuer !== id.did) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } // Don't allow credentials that will be garbage-collected @@ -959,7 +958,7 @@ export async function acceptCredential(did) { const vc = await decryptJSON(credential); if (vc.credentialSubject.id !== id.did) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return addToHeld(credential); @@ -987,7 +986,7 @@ export async function publishCredential(did, reveal = false) { const vc = await decryptJSON(credential); if (vc.credentialSubject.id !== id.did) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const doc = await resolveDID(id.did); @@ -1009,7 +1008,7 @@ export async function publishCredential(did, reveal = false) { return vc; } else { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } } @@ -1026,7 +1025,7 @@ export async function unpublishCredential(did) { return `OK credential ${did} removed from manifest`; } - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } export async function createChallenge(challenge, registry = ephemeralRegistry) { @@ -1042,11 +1041,11 @@ export async function createChallenge(challenge, registry = ephemeralRegistry) { } if (!challenge.credentials) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!Array.isArray(challenge.credentials)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return createAsset(challenge, registry); @@ -1098,7 +1097,7 @@ export async function createResponse(did, registry = ephemeralRegistry) { const challenge = lookupDID(did); if (!challenge) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const doc = await resolveDID(challenge); @@ -1106,7 +1105,7 @@ export async function createResponse(did, registry = ephemeralRegistry) { const { credentials } = await resolveAsset(challenge); if (!credentials) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const matches = []; @@ -1120,7 +1119,7 @@ export async function createResponse(did, registry = ephemeralRegistry) { } if (!matches) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const pairs = []; @@ -1154,7 +1153,7 @@ export async function verifyResponse(responseDID, challengeDID) { challengeDID = lookupDID(challengeDID); if (!responseDID) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const response = await decryptJSON(responseDID); @@ -1232,7 +1231,7 @@ export async function getGroup(id) { const isGroup = await groupTest(id); if (!isGroup) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return resolveAsset(id); @@ -1244,7 +1243,7 @@ export async function groupAdd(groupId, memberId) { const data = doc.didDocumentData; if (!data.members || !Array.isArray(data.members)) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const memberDID = lookupDID(memberId); @@ -1254,7 +1253,7 @@ export async function groupAdd(groupId, memberId) { await resolveDID(memberDID); } catch { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } // If already a member, return immediately @@ -1264,14 +1263,14 @@ export async function groupAdd(groupId, memberId) { // Can't add a group to itself if (memberDID === groupDID) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } // Can't add a mutual membership relation const isMember = await groupTest(memberId, groupId); if (isMember) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const members = new Set(data.members); @@ -1281,7 +1280,7 @@ export async function groupAdd(groupId, memberId) { const ok = await updateDID(groupDID, doc); if (!ok) { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } return data; @@ -1293,7 +1292,7 @@ export async function groupRemove(groupId, memberId) { const data = doc.didDocumentData; if (!data.members) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const memberDID = lookupDID(memberId); @@ -1303,7 +1302,7 @@ export async function groupRemove(groupId, memberId) { await resolveDID(memberDID); } catch { - throw exceptions.INVALID_DID; + throw new Error(exceptions.INVALID_DID); } // If not already a member, return immediately @@ -1318,7 +1317,7 @@ export async function groupRemove(groupId, memberId) { const ok = await updateDID(groupDID, doc); if (!ok) { - throw exceptions.UPDATE_FAILED; + throw new Error(exceptions.UPDATE_FAILED); } return data; @@ -1383,14 +1382,14 @@ export const defaultSchema = { function validateSchema(schema) { try { if (!Object.keys(schema).includes('$schema')) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } // Attempt to instantiate the schema JSONSchemaFaker.generate(schema); } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return true; @@ -1438,7 +1437,7 @@ export async function createTemplate(id) { const isSchema = await testSchema(id); if (!isSchema) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const schemaDID = lookupDID(id); @@ -1467,48 +1466,48 @@ export async function pollTemplate() { export async function createPoll(poll) { if (poll.type !== 'poll') { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (poll.version !== 1) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.description) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.options || !Array.isArray(poll.options) || poll.options.length < 2 || poll.options.length > 10) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.roster) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } try { const isValidGroup = await groupTest(poll.roster); if (!isValidGroup) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!poll.deadline) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const deadline = new Date(poll.deadline); if (isNaN(deadline.getTime())) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (deadline < new Date()) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } return createAsset(poll); @@ -1521,7 +1520,7 @@ export async function viewPoll(poll) { const data = doc.didDocumentData; if (!data || !data.options || !data.deadline) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } let hasVoted = false; @@ -1605,11 +1604,11 @@ export async function votePoll(poll, vote, spoil = false) { const owner = doc.didDocument.controller; if (!eligible) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (expired) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } let ballot; @@ -1625,7 +1624,7 @@ export async function votePoll(poll, vote, spoil = false) { vote = parseInt(vote); if (!Number.isInteger(vote) || vote < 1 || vote > max) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } ballot = { @@ -1651,11 +1650,11 @@ export async function updatePoll(ballot) { dataBallot = await decryptJSON(didBallot); if (!dataBallot.poll || !dataBallot.vote) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } } catch { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const didPoll = lookupDID(dataBallot.poll); @@ -1664,26 +1663,26 @@ export async function updatePoll(ballot) { const didOwner = docPoll.didDocument.controller; if (id.did !== didOwner) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const eligible = await groupTest(dataPoll.roster, didVoter); if (!eligible) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const expired = (Date(dataPoll.deadline) > new Date()); if (expired) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const max = dataPoll.options.length; const vote = parseInt(dataBallot.vote); if (!vote || vote < 0 || vote > max) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!dataPoll.ballots) { @@ -1705,13 +1704,13 @@ export async function publishPoll(poll, reveal = false) { const owner = doc.didDocument.controller; if (id.did !== owner) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } const view = await viewPoll(poll); if (!view.results.final) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } if (!reveal) { @@ -1730,7 +1729,7 @@ export async function unpublishPoll(poll) { const owner = doc.didDocument.controller; if (id.did !== owner) { - throw exceptions.INVALID_PARAMETER; + throw new Error(exceptions.INVALID_PARAMETER); } delete doc.didDocumentData.results; diff --git a/src/keymaster.test.js b/src/keymaster.test.js index 4dd279c7..45ec0667 100644 --- a/src/keymaster.test.js +++ b/src/keymaster.test.js @@ -88,10 +88,10 @@ describe('newWallet', () => { try { keymaster.newWallet(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UPDATE_FAILED); + expect(error.message).toBe(exceptions.UPDATE_FAILED); } }); @@ -110,10 +110,10 @@ describe('newWallet', () => { try { keymaster.newWallet([]); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -219,9 +219,9 @@ describe('createId', () => { try { await keymaster.createId(name); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -272,9 +272,9 @@ describe('removeId', () => { try { keymaster.removeId(name2); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); }); @@ -307,10 +307,10 @@ describe('setCurrentId', () => { try { keymaster.setCurrentId('Alice'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); }); @@ -408,10 +408,10 @@ describe('recoverId', () => { try { await keymaster.recoverId(did); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -446,10 +446,10 @@ describe('testAgent', () => { try { await keymaster.testAgent(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); @@ -458,10 +458,10 @@ describe('testAgent', () => { try { await keymaster.testAgent('mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_DID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); }); @@ -571,7 +571,7 @@ describe('rotateKeys', () => { try { await keymaster.rotateKeys(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { expect(error.message).toBe('Cannot rotate keys'); @@ -605,10 +605,10 @@ describe('addName', () => { try { keymaster.addName('Jack', alice); keymaster.addName('Jack', bob); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -619,10 +619,10 @@ describe('addName', () => { try { keymaster.addName('Alice', alice); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -725,9 +725,9 @@ describe('resolveDID', () => { try { await keymaster.resolveDID('mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_DID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); }); @@ -799,9 +799,9 @@ describe('createAsset', () => { try { const mockAnchor = { name: 'mockAnchor' }; await keymaster.createAsset(mockAnchor); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.NO_CURRENT_ID); } }); @@ -811,9 +811,9 @@ describe('createAsset', () => { try { await keymaster.createId('Bob'); await keymaster.createAsset(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -823,9 +823,9 @@ describe('createAsset', () => { try { await keymaster.createId('Bob'); await keymaster.createAsset(""); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -835,9 +835,9 @@ describe('createAsset', () => { try { await keymaster.createId('Bob'); await keymaster.createAsset([]); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -847,9 +847,9 @@ describe('createAsset', () => { try { await keymaster.createId('Bob'); await keymaster.createAsset({}); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -1149,9 +1149,9 @@ describe('addSignature', () => { try { await keymaster.addSignature(mockJson); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.NO_CURRENT_ID); } }); @@ -1162,9 +1162,9 @@ describe('addSignature', () => { try { await keymaster.addSignature(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -1335,10 +1335,10 @@ describe('issueCredential', () => { try { await keymaster.issueCredential(boundCredential); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -1430,10 +1430,10 @@ describe('revokeCredential', () => { try { await keymaster.revokeCredential(did); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); @@ -1580,26 +1580,26 @@ describe('createChallenge', () => { try { await keymaster.createChallenge([]); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { await keymaster.createChallenge({ credentials: 123 }); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { await keymaster.createChallenge({ mock: [] }); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -2010,10 +2010,10 @@ describe('unpublishCredential', () => { try { await keymaster.unpublishCredential('mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.NO_CURRENT_ID); } }); @@ -2024,10 +2024,10 @@ describe('unpublishCredential', () => { try { await keymaster.unpublishCredential('did:test:mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -2041,10 +2041,10 @@ describe('unpublishCredential', () => { try { await keymaster.unpublishCredential(did); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -2129,10 +2129,10 @@ describe('groupAdd', () => { try { await keymaster.groupAdd(groupDid, 'mockAlias'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_DID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); @@ -2166,10 +2166,10 @@ describe('groupAdd', () => { try { await keymaster.groupAdd('mockAlias', dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_DID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); @@ -2244,42 +2244,42 @@ describe('groupAdd', () => { try { await keymaster.groupAdd(groupDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd(groupDid, 100); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd(groupDid, [1, 2, 3]); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd(groupDid, { name: 'mock' }); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd(groupDid, 'did:mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); @@ -2292,50 +2292,50 @@ describe('groupAdd', () => { try { await keymaster.groupAdd(null, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd(100, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd([1, 2, 3], dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd({ name: 'mock' }, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupAdd(agentDid, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { await keymaster.groupAdd(dataDid, agentDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -2347,10 +2347,10 @@ describe('groupAdd', () => { try { await keymaster.groupAdd(groupDid, groupDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -2367,10 +2367,10 @@ describe('groupAdd', () => { try { await keymaster.groupAdd(group3Did, group1Did); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -2470,42 +2470,42 @@ describe('groupRemove', () => { try { await keymaster.groupRemove(groupDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove(groupDid, 100); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove(groupDid, [1, 2, 3]); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove(groupDid, { name: 'mock' }); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove(groupDid, 'did:mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); @@ -2518,58 +2518,58 @@ describe('groupRemove', () => { try { await keymaster.groupRemove(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove(null, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove(100, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove([1, 2, 3], dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove({ name: 'mock' }, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } try { await keymaster.groupRemove(agentDid, dataDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { await keymaster.groupRemove(dataDid, agentDid); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -2686,10 +2686,10 @@ describe('getGroup', () => { try { await keymaster.getGroup(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); @@ -2699,10 +2699,10 @@ describe('getGroup', () => { try { const agentDID = await keymaster.createId('Bob'); await keymaster.getGroup(agentDID); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -2765,100 +2765,100 @@ describe('createPoll', () => { const poll = JSON.parse(JSON.stringify(template)); poll.type = "wrong type"; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); poll.version = 0; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); delete poll.description; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); delete poll.roster; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); delete poll.options; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); poll.options = ['one option']; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); poll.options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); poll.options = "not a list"; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); delete poll.deadline; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { const poll = JSON.parse(JSON.stringify(template)); poll.deadline = "not a date"; await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } try { @@ -2870,10 +2870,10 @@ describe('createPoll', () => { poll.deadline = lastWeek.toISOString(); await keymaster.createPoll(poll); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -2955,10 +2955,10 @@ describe('votePoll', () => { try { await keymaster.votePoll(pollDid, 5); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); @@ -2975,10 +2975,10 @@ describe('votePoll', () => { try { await keymaster.votePoll(pollDid, 5); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -3019,10 +3019,10 @@ describe('updatePoll', () => { try { await keymaster.updatePoll(pollDid) - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -3163,9 +3163,9 @@ describe('createSchema', () => { try { await keymaster.createSchema({ mock: 'not a schema' }); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -3193,9 +3193,9 @@ describe('getSchema', () => { try { await keymaster.getSchema('bogus'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_DID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); }); @@ -3226,9 +3226,9 @@ describe('setSchema', () => { try { await keymaster.setSchema(did, { mock: 'not a schema' }); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -3265,9 +3265,9 @@ describe('testSchema', () => { try { await keymaster.testSchema(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); }); @@ -3299,9 +3299,9 @@ describe('createTemplate', () => { try { await keymaster.createTemplate(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); }); @@ -3536,9 +3536,9 @@ describe('listCredentials', () => { try { await keymaster.listCredentials('mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); }); @@ -3564,9 +3564,9 @@ describe('getCredential', () => { try { await keymaster.getCredential('mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_DID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); @@ -3576,9 +3576,9 @@ describe('getCredential', () => { try { const agentDID = await keymaster.createId('Rando'); await keymaster.getCredential(agentDID); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_PARAMETER); + expect(error.message).toBe(exceptions.INVALID_PARAMETER); } }); }); @@ -3618,9 +3618,9 @@ describe('removeCredential', () => { try { await keymaster.removeCredential(); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.INVALID_DID); + expect(error.message).toBe(exceptions.INVALID_DID); } }); @@ -3629,9 +3629,9 @@ describe('removeCredential', () => { try { await keymaster.removeCredential('mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_DID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); }); @@ -3707,9 +3707,9 @@ describe('setCurrentId', () => { try { keymaster.setCurrentId('mock'); - throw exceptions.EXPECTED_EXCEPTION;; + throw new Error(exceptions.EXPECTED_EXCEPTION); } catch (error) { - expect(error).toBe(exceptions.UNKNOWN_ID); + expect(error.message).toBe(exceptions.UNKNOWN_ID); } }); });