From 9f15d81e139c5a6af66636084192a12727eee1c4 Mon Sep 17 00:00:00 2001 From: Mircea Nistor Date: Wed, 10 Aug 2022 15:22:58 +0200 Subject: [PATCH] feat(did-resolver): simplify DIDResolverPlugin constructor fixes #976 --- __tests__/localAgent.test.ts | 29 ++++----- packages/did-resolver/package.json | 10 ++- .../src/__tests__/integration.test.ts | 63 ++++++------------- packages/did-resolver/src/resolver.ts | 43 +++++++------ packages/did-resolver/tsconfig.json | 9 ++- 5 files changed, 69 insertions(+), 85 deletions(-) diff --git a/__tests__/localAgent.test.ts b/__tests__/localAgent.test.ts index 2ebe90a24..50fa71022 100644 --- a/__tests__/localAgent.test.ts +++ b/__tests__/localAgent.test.ts @@ -60,7 +60,6 @@ import { BrokenDiscoveryProvider, FakeDidProvider, FakeDidResolver } from '../pa import { DataSource } from 'typeorm' import { createGanacheProvider } from './utils/ganache-provider' import { createEthersProvider } from './utils/ethers-provider' -import { Resolver } from 'did-resolver' import { getResolver as ethrDidResolver } from 'ethr-did-resolver' import { getResolver as webDidResolver } from 'web-did-resolver' import { contexts as credential_contexts } from '@transmute/credentials-context' @@ -194,22 +193,20 @@ const setup = async (options?: IAgentOptions): Promise => { }, }), new DIDResolverPlugin({ - resolver: new Resolver({ - ...ethrDidResolver({ - infuraProjectId, - networks: [ - { - name: 'ganache', - chainId: 1337, - provider, - registry, - }, - ], - }), - ...webDidResolver(), - ...getDidKeyResolver(), - ...new FakeDidResolver(() => agent).getDidFakeResolver(), + ...ethrDidResolver({ + infuraProjectId, + networks: [ + { + name: 'ganache', + chainId: 1337, + provider, + registry, + }, + ], }), + ...webDidResolver(), + ...getDidKeyResolver(), + ...new FakeDidResolver(() => agent).getDidFakeResolver(), }), new DataStore(dbConnection), new DataStoreORM(dbConnection), diff --git a/packages/did-resolver/package.json b/packages/did-resolver/package.json index c1d833437..b8abf2009 100644 --- a/packages/did-resolver/package.json +++ b/packages/did-resolver/package.json @@ -10,12 +10,13 @@ }, "dependencies": { "@veramo/core": "^3.1.4", + "@veramo/utils": "^3.1.4", "cross-fetch": "^3.1.4", - "debug": "^4.3.3" + "debug": "^4.3.3", + "did-resolver": "^3.2.2" }, "devDependencies": { "@types/debug": "4.1.7", - "did-resolver": "3.2.2", "ethr-did-resolver": "6.0.2", "typescript": "4.7.3", "web-did-resolver": "2.0.19" @@ -32,10 +33,7 @@ "repository": "git@github.com:uport-project/veramo.git", "author": "Simonas Karuzas ", "contributors": [ - { - "name": "Mircea Nistor", - "email": "mircea.nistor@mesh.xyz" - } + "Mircea Nistor { beforeAll(() => {}) @@ -89,45 +92,19 @@ describe('@veramo/did-resolver', () => { }) }) - //// Uniresolver is too unstable - // it('should resolve did:key using uniresolver', async () => { - // expect.assertions(1) - // const { didDocument } = await resolverPlugin.resolveDid({ - // didUrl: 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // }) - // expect(didDocument).toEqual({ - // '@context': ['https://w3id.org/did/v0.11'], - // id: 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // publicKey: [ - // { - // id: 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6#z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // type: 'Ed25519VerificationKey2018', - // controller: 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // publicKeyBase58: '2QTnR7atrFu3Y7S6Xmmr4hTsMaL1KDh6Mpe9MgnJugbi', - // }, - // ], - // authentication: [ - // 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6#z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // ], - // assertionMethod: [ - // 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6#z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // ], - // capabilityDelegation: [ - // 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6#z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // ], - // capabilityInvocation: [ - // 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6#z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // ], - // keyAgreement: [ - // { - // id: 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6#z6LSbgq3GejX88eiAYWmZ9EiddS3GaXodvm8MJJyEH7bqXgz', - // type: 'X25519KeyAgreementKey2019', - // controller: 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6', - // publicKeyBase58: '1eskLvf2fvy5A912VimK3DZRRzgwKayUKbHjpU589vE', - // }, - // ], - // }) - // }) + it('should resolve web DID using direct constructor', async () => { + expect.assertions(1) + const result = await resolverPluginDirect.resolveDid({ didUrl: 'did:web:did.actor:alice' }) + expect(result?.didDocument?.id).toEqual('did:web:did.actor:alice') + }) + + it('should resolve ethr-did with RPC URL using direct constructor', async () => { + expect.assertions(1) + const result = await resolverPluginDirect.resolveDid({ + didUrl: 'did:ethr:goerli:0xE6Fe788d8ca214A080b0f6aC7F48480b2AEfa9a6', + }) + expect(result?.didDocument?.id).toEqual('did:ethr:goerli:0xE6Fe788d8ca214A080b0f6aC7F48480b2AEfa9a6') + }) it('should fail predictably when unsupported method is resolved', async () => { expect.assertions(1) diff --git a/packages/did-resolver/src/resolver.ts b/packages/did-resolver/src/resolver.ts index 45fd3feab..14540f770 100644 --- a/packages/did-resolver/src/resolver.ts +++ b/packages/did-resolver/src/resolver.ts @@ -1,23 +1,20 @@ import { DIDDocumentSection, IAgentPlugin, IResolver, schema } from '@veramo/core' +import { isDefined } from '@veramo/utils' import { DIDDocument, - DIDResolutionResult, DIDResolutionOptions, - VerificationMethod, - ServiceEndpoint, + DIDResolutionResult, + DIDResolver, parse as parseDID, Resolvable, + Resolver, + ServiceEndpoint, + VerificationMethod, } from 'did-resolver' - -export { DIDDocument } import Debug from 'debug' const debug = Debug('veramo:resolver') -interface Options { - resolver: Resolvable -} - /** * A Veramo Plugin that enables users to resolve DID documents. * @@ -31,9 +28,17 @@ export class DIDResolverPlugin implements IAgentPlugin { readonly schema = schema.IResolver private didResolver: Resolvable - constructor(options: Options) { - if (!options.resolver) throw Error('Missing resolver') - this.didResolver = options.resolver + constructor(options: { resolver?: Resolvable } | { [didMethod: string]: DIDResolver }) { + const { resolver, ...resolverMap } = options + if (isDefined(resolver)) { + this.didResolver = resolver as Resolvable + } else if (Object.keys(resolverMap).length > 0) { + this.didResolver = new Resolver(resolverMap as Record) + } else { + throw Error( + 'invalid_config: The DIDResolverPlugin must be initialized with a Resolvable or a map of methods to DIDResolver implementations', + ) + } this.methods = { resolveDid: this.resolveDid.bind(this), @@ -43,9 +48,9 @@ export class DIDResolverPlugin implements IAgentPlugin { /** {@inheritDoc @veramo/core#IResolver.resolveDid} */ async resolveDid({ - didUrl, - options, - }: { + didUrl, + options, + }: { didUrl: string options?: DIDResolutionOptions }): Promise { @@ -72,10 +77,10 @@ export class DIDResolverPlugin implements IAgentPlugin { /** {@inheritDoc @veramo/core#IResolver.getDIDComponentById} */ async getDIDComponentById({ - didDocument, - didUrl, - section, - }: { + didDocument, + didUrl, + section, + }: { didDocument: DIDDocument didUrl: string section?: DIDDocumentSection diff --git a/packages/did-resolver/tsconfig.json b/packages/did-resolver/tsconfig.json index eb113f37a..8f4d75834 100644 --- a/packages/did-resolver/tsconfig.json +++ b/packages/did-resolver/tsconfig.json @@ -5,5 +5,12 @@ "outDir": "build", "declarationDir": "build" }, - "references": [{ "path": "../core" }] + "references": [ + { + "path": "../core" + }, + { + "path": "../utils" + } + ] }