diff --git a/packages/agent/src/agent-did-resolver-cache.ts b/packages/agent/src/agent-did-resolver-cache.ts index 8fb9377e3..1c36c48f0 100644 --- a/packages/agent/src/agent-did-resolver-cache.ts +++ b/packages/agent/src/agent-did-resolver-cache.ts @@ -1,5 +1,6 @@ import { DidResolutionResult, DidResolverCache, DidResolverCacheLevel, DidResolverCacheLevelParams } from '@web5/dids'; import { Web5PlatformAgent } from './types/agent.js'; +import { logger } from '@web5/common'; /** diff --git a/packages/agent/src/local-key-manager.ts b/packages/agent/src/local-key-manager.ts index ee94e373f..083fd7517 100644 --- a/packages/agent/src/local-key-manager.ts +++ b/packages/agent/src/local-key-manager.ts @@ -423,17 +423,13 @@ export class LocalKeyManager implements AgentKeyManager { // Compute the key URI for the key. const keyUri = await this.getKeyUri({ key: privateKey }); - const existingKey = await this._keyStore.get({ id: keyUri, agent: this.agent, useCache: true }); - if (!existingKey) { - // Store the key if it does not already exist in the key store. - await this._keyStore.set({ - id : keyUri, - data : privateKey, - agent : this.agent, - preventDuplicates : true, - useCache : true - }); - } + await this._keyStore.set({ + id : keyUri, + data : privateKey, + agent : this.agent, + preventDuplicates : true, + useCache : true + }); return keyUri; } diff --git a/packages/agent/tests/local-key-manager.spec.ts b/packages/agent/tests/local-key-manager.spec.ts index d95c8da89..6b53232a3 100644 --- a/packages/agent/tests/local-key-manager.spec.ts +++ b/packages/agent/tests/local-key-manager.spec.ts @@ -120,32 +120,6 @@ describe('LocalKeyManager', () => { expect(importedKey).to.exist; expect(importedKey).to.deep.equal(key); }); - - it('does not attempt to import a key that is already in the key store', async () => { - // scenario: a key already exists within your key store, you attempt to import it again - // the method should not throw an error, but should also not attempt to store the key again - - const storeSetSpy = sinon.spy(testHarness.agent.keyManager['_keyStore'], 'set'); - - // generate a key within the key manager - const keyUri = await testHarness.agent.keyManager.generateKey({ algorithm: 'secp256k1' }); - expect(storeSetSpy.callCount).to.equal(1); - - // export the key - const exportedKey = await testHarness.agent.keyManager.exportKey({ keyUri }); - - // reset the spy count - storeSetSpy.resetHistory(); - - // attempt to import the same key, will not fail - await testHarness.agent.keyManager.importKey({ key: exportedKey }); - expect(storeSetSpy.callCount).to.equal(0); // - - // sanity import a key generated outside the key manager - const key = await Ed25519.generateKey(); - await testHarness.agent.keyManager.importKey({ key }); - expect(storeSetSpy.callCount).to.equal(1); - }); }); describe('exportKey()', () => { diff --git a/packages/dids/src/bearer-did.ts b/packages/dids/src/bearer-did.ts index 120f94186..cdecee834 100644 --- a/packages/dids/src/bearer-did.ts +++ b/packages/dids/src/bearer-did.ts @@ -240,6 +240,7 @@ export class BearerDid { keyManager?: CryptoApi & KeyImporterExporter; portableDid: PortableDid; }): Promise { + // Get all verification methods from the given DID document, including embedded methods. const verificationMethods = getVerificationMethods({ didDocument: portableDid.document }); @@ -250,7 +251,13 @@ export class BearerDid { // If given, import the private key material into the key manager. for (let key of portableDid.privateKeys ?? []) { - await keyManager.importKey({ key }); + + // confirm th key does not already exist before importing it to avoid failures from the key manager + const keyUri = await keyManager.getKeyUri({ key }); + const keyExists = await keyManager.getPublicKey({ keyUri }).then(() => true).catch(() => false); + if (!keyExists) { + await keyManager.importKey({ key }); + } } // Validate that the key material for every verification method in the DID document is present diff --git a/packages/dids/tests/bearer-did.spec.ts b/packages/dids/tests/bearer-did.spec.ts index db0790bb8..7657b7504 100644 --- a/packages/dids/tests/bearer-did.spec.ts +++ b/packages/dids/tests/bearer-did.spec.ts @@ -447,5 +447,23 @@ describe('BearerDid', () => { expect(error.message).to.include('Key not found'); } }); + + it('does not attempt to import a key that is already in the key manager', async () => { + + // create a key manager + const keyManager = new LocalKeyManager(); + + // Import one of the private keys into the key manager + const privateKey = portableDid.privateKeys![0]; + await keyManager.importKey({ key: privateKey }); + + // spy on the importKey method + const importKeySpy = sinon.spy(keyManager, 'importKey'); + + // attempt to import the BearerDid with the key manager + const did = await BearerDid.import({ portableDid, keyManager }); + expect(did.uri).to.equal(portableDid.uri); + expect(importKeySpy.calledOnce).to.be.false; + }); }); }); \ No newline at end of file