From aabddb436b8b4dd78378da4704ba00147d074cdb Mon Sep 17 00:00:00 2001 From: Mircea Nistor Date: Thu, 26 Aug 2021 16:01:20 +0200 Subject: [PATCH] fix(did-resolver): always include didResolutionMetadata in result (#682) fixes #681 --- __tests__/shared/resolveDid.ts | 12 +++++++++--- packages/did-comm/src/didcomm.ts | 10 +--------- packages/did-comm/src/utils.ts | 6 +++--- packages/did-resolver/src/resolver.ts | 15 ++++++++++++++- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/__tests__/shared/resolveDid.ts b/__tests__/shared/resolveDid.ts index 14de75b0e..c1975240d 100644 --- a/__tests__/shared/resolveDid.ts +++ b/__tests__/shared/resolveDid.ts @@ -23,16 +23,18 @@ export default (testContext: { expect(didDoc?.id).toEqual(didUrl) }) - it('should resolve did:key', async () => { + it('should resolve did:key github #681', async () => { const didUrl = 'did:key:z6Mkk7yqnGF3YwTrLpqrW6PGsKci7dNqh1CjnvMbzrMerSeL' const result = await agent.resolveDid({ didUrl }) const didDoc = result.didDocument expect(didDoc?.id).toEqual(didUrl) + expect(result).toHaveProperty('didDocumentMetadata') + expect(result).toHaveProperty('didResolutionMetadata') }) it('should resolve imported fake did', async () => { const did = 'did:fake:myfakedid' - const importedFakeDID = await agent.didManagerImport({ + await agent.didManagerImport({ did, keys: [ { @@ -56,7 +58,7 @@ export default (testContext: { }) const resolved = await agent.resolveDid({ didUrl: did }) expect(resolved.didDocument).toEqual({ - id: 'did:fake:myfakedid', + id: did, service: [ { id: 'did:fake:myfakedid#fake-service-1', @@ -77,6 +79,8 @@ export default (testContext: { authentication: ['did:fake:myfakedid#fake-key-1'], assertionMethod: ['did:fake:myfakedid#fake-key-1'], }) + expect(resolved).toHaveProperty('didDocumentMetadata') + expect(resolved).toHaveProperty('didResolutionMetadata') }) it('should resolve created fake did', async () => { @@ -103,6 +107,7 @@ export default (testContext: { }) it('should throw error when resolving garbage', async () => { + expect.assertions(3) //@ts-ignore await expect(agent.resolveDid()).resolves.toEqual({ didDocument: null, @@ -135,6 +140,7 @@ export default (testContext: { afterAll(testContext.tearDown) it('should throw validation error', async () => { + expect.assertions(3) //@ts-ignore await expect(agent.resolveDid()).rejects.toHaveProperty('name', 'ValidationError') //@ts-ignore diff --git a/packages/did-comm/src/didcomm.ts b/packages/did-comm/src/didcomm.ts index 24b631eeb..ff1125b1a 100644 --- a/packages/did-comm/src/didcomm.ts +++ b/packages/did-comm/src/didcomm.ts @@ -468,15 +468,7 @@ export class DIDComm implements IAgentPlugin { throw new Error(`not_supported: return routes not supported yet`) } - const result = await context.agent.resolveDid({ didUrl: `${recipientDidUrl}` }) - const err = result.didResolutionMetadata.error - const msg = result.didResolutionMetadata.message - const didDoc = result.didDocument - if (!didDoc || err) { - throw new Error( - `resolver_error: could not resolve DID document for '${recipientDidUrl}': ${err} ${msg}`, - ) - } + const didDoc = await resolveDidOrThrow(recipientDidUrl, context) const services = didDoc.service?.filter( (service: any) => service.type === 'DIDCommMessaging', diff --git a/packages/did-comm/src/utils.ts b/packages/did-comm/src/utils.ts index 1e419debe..eb0105dd0 100644 --- a/packages/did-comm/src/utils.ts +++ b/packages/did-comm/src/utils.ts @@ -193,11 +193,11 @@ export async function mapIdentifierKeysToDoc( return extendedKeys } -export async function resolveDidOrThrow(didUrl: string, context: IAgentContext) { +export async function resolveDidOrThrow(didUrl: string, context: IAgentContext): Promise { // TODO: add caching const docResult = await context.agent.resolveDid({ didUrl: didUrl }) - const err = docResult.didResolutionMetadata.error - const msg = docResult.didResolutionMetadata.message + const err = docResult?.didResolutionMetadata?.error + const msg = docResult?.didResolutionMetadata?.message const didDocument = docResult.didDocument if (!isDefined(didDocument) || err) { throw new Error(`not_found: could not resolve DID document for '${didUrl}': ${err} ${msg}`) diff --git a/packages/did-resolver/src/resolver.ts b/packages/did-resolver/src/resolver.ts index 0cf691084..5fe443e9f 100644 --- a/packages/did-resolver/src/resolver.ts +++ b/packages/did-resolver/src/resolver.ts @@ -44,7 +44,20 @@ export class DIDResolverPlugin implements IAgentPlugin { accept: 'application/did+ld+json', ...options, } - return this.didResolver.resolve(didUrl, resolverOptions) + + // ensure the required fields are present, even if the resolver is not compliant + const cannedResponse: DIDResolutionResult = { + didDocumentMetadata: {}, + didResolutionMetadata: {}, + didDocument: null, + } + + const resolution = await this.didResolver.resolve(didUrl, resolverOptions) + + return { + ...cannedResponse, + ...resolution, + } } /** {@inheritDoc @veramo/core#IResolver.getDIDComponentById} */