Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should add/remove a public key node from the DID Doc', async () => {
await didDoc.importKeys(exampleKeys);
const authSuite = didDoc.doc[constants.SUITES.authentication][0];
const authKey = authSuite.publicKey[0];
didDoc.removeKey(authKey);
// Check to make sure key is removed
expect(authSuite.publicKey).to.eql([]);
expect(didDoc.keys[keyId]).to.not.exist();
// Now re-add the key
const suiteId = `${did}#auth-suite-1`;
const key = await LDKeyPair.from(exampleKeys[keyId]);
await didDoc.addKey({suiteId, key});
expect(authSuite.publicKey).to.eql([key.publicNode({owner: did})]);
expect(didDoc.keys[keyId]).to.eql(key);
});
});
module.exports = async options => {
const {
did, private: privateKeyBase58, public: publicKeyBase58,
purpose: proofPurpose
} = options;
logger._debug(options, 'ed25519-key-add', {did});
const privateDidDocument = await helpers.getSynchronizedDid(options);
// create the key
// TODO: implement passphrase
const keyPair = await LDKeyPair.from({
controller: did, privateKeyBase58, publicKeyBase58,
type: 'Ed25519VerificationKey2018'
});
keyPair.id = privateDidDocument.generateKeyId({did, keyPair});
if(keyPair.id in privateDidDocument.keys) {
throw new Error('The key already exists.');
}
// activate a JSON patch observer which will be resolved in _send.
privateDidDocument.observe();
privateDidDocument.addKey({key: keyPair, proofPurpose});
logger._log(options, 'DID local update successful.');
await _send(
async importKeys(data = {}, options = {}) {
for(const keyData of Object.values(data)) {
const key = await LDKeyPair.from(keyData,
{injector: this.injector, ...options});
this.keys[key.id] = key;
}
}