Skip to content

Commit

Permalink
do not fail if duplicate key, just do not update
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Oct 10, 2024
1 parent f7920ef commit 57b1594
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
15 changes: 8 additions & 7 deletions packages/agent/src/did-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,23 @@ export class AgentDidApi<TKeyManager extends AgentKeyManager = AgentKeyManager>
throw new Error('AgentDidApi: No changes detected, update aborted');
}

const bearerDid = new BearerDid({
uri : portableDid.uri,
document : portableDid.document,
metadata : portableDid.metadata,
keyManager : this.agent.keyManager
});
// If private keys are present in the PortableDid, import the key material into the Agent's key
// manager. Validate that the key material for every verification method in the DID document is
// present in the key manager. If no keys are present, this will fail.
// NOTE: We currently do not delete the previous keys from the document.
// TODO: Add support for deleting the keys no longer present in the document.
const bearerDid = await BearerDid.import({ keyManager: this.agent.keyManager, portableDid });

// Only the DID URI, document, and metadata are stored in the Agent's DID store.
const { uri, document, metadata } = bearerDid;
const portableDidWithoutKeys: PortableDid = { uri, document, metadata };

// pre-populate the resolution cache with the document and metadata
await this.cache.set(uri, { didDocument: document, didResolutionMetadata: { }, didDocumentMetadata: metadata });

await this._store.set({
id : uri,
data : { uri, document, metadata }, // portable did without the keys
data : portableDidWithoutKeys,
agent : this.agent,
tenant : tenant ?? uri,
updateExisting : true,
Expand Down
19 changes: 11 additions & 8 deletions packages/agent/src/local-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,17 @@ export class LocalKeyManager implements AgentKeyManager {
// Compute the key URI for the key.
const keyUri = await this.getKeyUri({ key: privateKey });

// Store the key in the key store.
await this._keyStore.set({
id : keyUri,
data : privateKey,
agent : this.agent,
preventDuplicates : true,
useCache : true
});
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
});
}

return keyUri;
}
Expand Down

0 comments on commit 57b1594

Please sign in to comment.