Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(did-resolver): simplify DIDResolverPlugin constructor #986

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -194,22 +193,20 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
},
}),
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),
Expand Down
10 changes: 4 additions & 6 deletions packages/did-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,10 +33,7 @@
"repository": "git@github.com:uport-project/veramo.git",
"author": "Simonas Karuzas <simonas.karuzas@mesh.xyz>",
"contributors": [
{
"name": "Mircea Nistor",
"email": "mircea.nistor@mesh.xyz"
}
"Mircea Nistor <mircea.nistor@mesh.xyz"
],
"license": "Apache-2.0",
"keywords": []
Expand Down
63 changes: 20 additions & 43 deletions packages/did-resolver/src/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ jest.setTimeout(60000)

const providerConfig = {
networks: [
{ name: 'rinkeby', rpcUrl: 'https://rinkeby.infura.io/v3/6b734e0b04454df8a6ce234023c04f26' },
{ name: 'rinkeby', rpcUrl: 'https://rinkeby.infura.io/v3/3586660d179141e3801c3895de1c2eba' },
{ name: 'goerli', rpcUrl: 'https://goerli.infura.io/v3/3586660d179141e3801c3895de1c2eba' },
{ name: 'development', rpcUrl: 'http://localhost:7545' },
// FIXME: add this example
// { name: 'test', provider: TBD_add_example_of_custom_provider_usage },
],
}

/** This creates a resolver that supports the [ethr, web, key, elem] DID methods */
let resolver = new Resolver({
let resolverMap = {
// resolve did:ethr using the embedded ethr-did-resolver
...getEthrResolver(providerConfig),
// resolve did:web using the embedded web-did-resolver
...getWebDidResolver(),
// resolve some other DID methods using the centralized `uniresolver.io` service
...getUniversalResolverFor(['key', 'elem']),
})
}

/** This creates a resolver that supports the [ethr, web, key, elem] DID methods */
let resolver = new Resolver(resolverMap)
let resolverPlugin: DIDResolverPlugin = new DIDResolverPlugin({ resolver })
let resolverPluginDirect: DIDResolverPlugin = new DIDResolverPlugin(resolverMap)

describe('@veramo/did-resolver', () => {
beforeAll(() => {})
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 24 additions & 19 deletions packages/did-resolver/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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<string, DIDResolver>)
} 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),
Expand All @@ -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<DIDResolutionResult> {
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion packages/did-resolver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
"outDir": "build",
"declarationDir": "build"
},
"references": [{ "path": "../core" }]
"references": [
{
"path": "../core"
},
{
"path": "../utils"
}
]
}