From d37c772b0eb3ce65a1e0a5f99b97acf641515d6b Mon Sep 17 00:00:00 2001 From: Niels Klomp Date: Thu, 20 Jun 2024 02:29:21 +0200 Subject: [PATCH] feat: Add support to find keys by thumbprint, and not have to resolve to DID resolution in all cases --- packages/did-utils/src/did-functions.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/did-utils/src/did-functions.ts b/packages/did-utils/src/did-functions.ts index 4faa38b1..c3934334 100644 --- a/packages/did-utils/src/did-functions.ts +++ b/packages/did-utils/src/did-functions.ts @@ -323,14 +323,18 @@ export async function getKey( if (!identifier) { return Promise.reject(new Error(`No identifier provided to getKey method!`)) } - const keys = await mapIdentifierKeysToDocWithJwkSupport(identifier, verificationMethodSection, context) - if (!keys || keys.length === 0) { - throw new Error(`No keys found for verificationMethodSection: ${verificationMethodSection} and did ${identifier.did}`) + let identifierKey = keyId ? identifier.keys.find((key: IKey) => key.kid === keyId || key?.meta?.jwkThumbprint === keyId) : undefined + if (!identifierKey) { + const keys = await mapIdentifierKeysToDocWithJwkSupport(identifier, verificationMethodSection, context) + if (!keys || keys.length === 0) { + throw new Error(`No keys found for verificationMethodSection: ${verificationMethodSection} and did ${identifier.did}`) + } + identifierKey = keyId ? keys.find((key: _ExtendedIKey) => key.meta.verificationMethod.id === keyId) : keys[0] } - - const identifierKey = keyId ? keys.find((key: _ExtendedIKey) => key.kid === keyId || key.meta.verificationMethod.id === keyId) : keys[0] if (!identifierKey) { - throw new Error(`No matching verificationMethodSection key found for keyId: ${keyId}`) + throw new Error( + `No matching verificationMethodSection key found for keyId: ${keyId} and vmSection: ${verificationMethodSection} for id ${identifier.did}` + ) } return identifierKey