Skip to content

Commit

Permalink
Throw error if publicMethodFor is not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrizagidulin committed Apr 9, 2021
1 parent 3f2366f commit 0d8a56e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/DidKeyDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export class DidKeyDriver {
throw new TypeError('The "purpose" parameter is required.');
}

return didIo.findVerificationMethod({doc: didDocument, purpose});
const method = didIo.findVerificationMethod({doc: didDocument, purpose});
if(!method) {
throw new Error(`No verification method found for purpose "${purpose}"`);
}
return method;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"module": "lib/main.js",
"dependencies": {
"@digitalbazaar/did-io": "^1.0.0",
"@digitalbazaar/ed25519-verification-key-2020": "^2.0.0",
"@digitalbazaar/ed25519-verification-key-2020": "^2.1.1",
"@digitalbazaar/x25519-key-agreement-key-2020": "^1.0.0",
"did-context": "^3.0.0",
"ed25519-signature-2020-context": "^1.0.1",
Expand Down
20 changes: 14 additions & 6 deletions test/driver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,23 @@ describe('did:key method driver', () => {
'zB12NYF8RrR3h41TDCTJojY59usg3mbtbjnFs7Eud1Y6u');
});

it('should return undefined if key is not found for purpose', async () => {
it('should throw error if key is not found for purpose', async () => {
const did = 'did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH';
// First, get the did document
const didDocument = await didKeyDriver.get({did});
// Then publicMethodFor can be used to fetch key data
const result = didKeyDriver.publicMethodFor({
didDocument, purpose: 'invalidPurpose'
});
expect(result).to.be.undefined;

let error;
try {
didKeyDriver.publicMethodFor({
didDocument, purpose: 'invalidPurpose'
});
} catch(e) {
error = e;
}

expect(error).to.be.exist;
expect(error.message).to
.match(/No verification method found for purpose/);
});
});

Expand Down

0 comments on commit 0d8a56e

Please sign in to comment.