diff --git a/packages/did-resolver/src/__tests__/resolver.test.ts b/packages/did-resolver/src/__tests__/resolver.test.ts index f299d833b..99e548de6 100644 --- a/packages/did-resolver/src/__tests__/resolver.test.ts +++ b/packages/did-resolver/src/__tests__/resolver.test.ts @@ -1,5 +1,5 @@ import { DIDResolverPlugin } from '../resolver.js' -import { Resolver } from 'did-resolver' +import { Resolver, VerificationMethod } from 'did-resolver' describe('@veramo/did-resolver', () => { it('should throw error when misconfigured', () => { @@ -15,4 +15,90 @@ describe('@veramo/did-resolver', () => { const resolver = new DIDResolverPlugin({ resolver: new Resolver() }) expect(resolver).toHaveProperty('resolveDid') }) + + const didDoc1 = { + '@context':["https://www.w3.org/ns/did/v1","https://w3id.org/security/multikey/v1", + // line below causes TS issue but isn't used in getDIDComponentById + // {"@base":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ"} + ], + "id":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ", + "verificationMethod":[ + {"id":"#key-2","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff"}, + {"id":"#key-1","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA"} + ], + "keyAgreement":["#key-1"], + "authentication":["#key-2"], + "assertionMethod":["#key-2"], + "service":[{"id":"1","description":"a DIDComm endpoint","serviceEndpoint":"did:web:dev-didcomm-mediator.herokuapp.com","type":"DIDCommMessaging"}] + } + + it('should correctly get component by key suffix with key in verificationMethod as suffix only', async () => { + const resolver = new DIDResolverPlugin({ resolver: new Resolver() }) + const result = await resolver.getDIDComponentById({ didDocument: didDoc1, section: 'keyAgreement', didUrl: '#key-1'}) + expect((result.valueOf()as VerificationMethod).id).toEqual("did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1") + }) + + const didDoc2 = { + "@context":["https://www.w3.org/ns/did/v1","https://w3id.org/security/multikey/v1", + // {"@base":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ"} + ], + "id":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ", + "verificationMethod":[ + {"id":"#key-2","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff"}, + {"id":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA"} + ], + "keyAgreement":["#key-1"], + "authentication":["#key-2"], + "assertionMethod":["#key-2"], + "service":[{"id":"1","description":"a DIDComm endpoint","serviceEndpoint":"did:web:dev-didcomm-mediator.herokuapp.com","type":"DIDCommMessaging"}] + } + + it('should correctly get component by key suffix with key in verificationMethod as DID + suffix', async () => { + const resolver = new DIDResolverPlugin({ resolver: new Resolver() }) + const result = await resolver.getDIDComponentById({ didDocument: didDoc2, section: 'keyAgreement', didUrl: '#key-1'}) + expect((result.valueOf()as VerificationMethod).id).toEqual("did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1") + }) + + const didDoc3 = { + "@context":["https://www.w3.org/ns/did/v1","https://w3id.org/security/multikey/v1", + // {"@base":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ"} + ], + "id":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ", + "verificationMethod":[ + {"id":"#key-2","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff"}, + {"id":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA"} + ], + "keyAgreement":["#key-1"], + "authentication":["#key-2"], + "assertionMethod":["#key-2"], + "service":[{"id":"1","description":"a DIDComm endpoint","serviceEndpoint":"did:web:dev-didcomm-mediator.herokuapp.com","type":"DIDCommMessaging"}] + } + + it('should correctly get component by DID + key suffix with key in verificationMethod as DID + suffix', async () => { + const resolver = new DIDResolverPlugin({ resolver: new Resolver() }) + const result = await resolver.getDIDComponentById({ didDocument: didDoc3, section: 'keyAgreement', didUrl: 'did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1'}) + expect((result.valueOf()as VerificationMethod).id).toEqual("did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1") + }) + + + const didDoc4 = { + "@context":["https://www.w3.org/ns/did/v1","https://w3id.org/security/multikey/v1", + // {"@base":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ"} + ], + "id":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ", + "verificationMethod":[ + {"id":"#key-2","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff"}, + {"id":"#key-1","type":"Multikey","controller":"did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ","publicKeyMultibase":"z6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA"} + ], + "keyAgreement":["#key-1"], + "authentication":["#key-2"], + "assertionMethod":["#key-2"], + "service":[{"id":"1","description":"a DIDComm endpoint","serviceEndpoint":"did:web:dev-didcomm-mediator.herokuapp.com","type":"DIDCommMessaging"}] + } + + it('should correctly get component by DID + key suffix with key in verificationMethod as suffix only', async () => { + const resolver = new DIDResolverPlugin({ resolver: new Resolver() }) + const result = await resolver.getDIDComponentById({ didDocument: didDoc4, section: 'keyAgreement', didUrl: 'did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1'}) + expect((result.valueOf()as VerificationMethod).id).toEqual("did:peer:2.Ez6LSiNkZ4hS9ivnKXcEf1GGfXxqcWSCCXaFC775JZjVfwkoA.Vz6Mkr6TpZKXqdt4Kx28GSrLt4VysTWof6Npm4HNaGMcVPTff.SeyJpZCI6IjEiLCJ0IjoiZG0iLCJzIjoiZGlkOndlYjpkZXYtZGlkY29tbS1tZWRpYXRvci5oZXJva3VhcHAuY29tIiwiZGVzY3JpcHRpb24iOiJhIERJRENvbW0gZW5kcG9pbnQifQ#key-1") + }) }) diff --git a/packages/did-resolver/src/resolver.ts b/packages/did-resolver/src/resolver.ts index d0e13e63b..d93db5b82 100644 --- a/packages/did-resolver/src/resolver.ts +++ b/packages/did-resolver/src/resolver.ts @@ -100,7 +100,7 @@ export class DIDResolverPlugin implements IAgentPlugin { } }) if (typeof result === 'string') { - result = mainSections.find((item) => item.id === didUrl || `${did}${item.id}` === didUrl) + result = mainSections.find((item) => item.id === didUrl || `${did}${item.id}` === didUrl || item.id === `${did}${didUrl}`); } if (!result) {