From c2c97cd0c38a58ff254797370039e1ced02fb1e9 Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Mon, 21 Oct 2024 13:13:09 -0400 Subject: [PATCH] add setter and getter for the identity metadata name --- packages/agent/src/identity-api.ts | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/agent/src/identity-api.ts b/packages/agent/src/identity-api.ts index f35bab22a..8e34fbcc2 100644 --- a/packages/agent/src/identity-api.ts +++ b/packages/agent/src/identity-api.ts @@ -270,6 +270,40 @@ export class AgentIdentityApi { + const identity = await this.get({ didUri }); + if (!identity) { + throw new Error(`AgentIdentityApi: Failed to retrieve metadata name due to Identity not found: ${didUri}`); + } + + return identity.metadata.name; + } + + public async setMetadataName({ didUri, name }: { didUri: string; name: string }): Promise { + if (!name) { + throw new Error('AgentIdentityApi: Failed to set metadata name due to missing name value.'); + } + + const identity = await this.get({ didUri }); + if (!identity) { + throw new Error(`AgentIdentityApi: Failed to set metadata name due to Identity not found: ${didUri}`); + } + + if (identity.metadata.name === name) { + throw new Error('AgentIdentityApi: Metadata name is already set to the provided value.'); + } + + // Update the name in the Identity's metadata and store it + await this._store.set({ + id : identity.did.uri, + data : { ...identity.metadata, name }, + agent : this.agent, + tenant : identity.metadata.tenant, + updateExisting : true, + useCache : true + }); + } + /** * Returns the connected Identity, if one is available. *