diff --git a/packages/daf-resolver/package.json b/packages/daf-resolver/package.json index ebe1e10da..d6fb641b1 100644 --- a/packages/daf-resolver/package.json +++ b/packages/daf-resolver/package.json @@ -9,13 +9,16 @@ "extract-api": "yarn daf extract-api" }, "dependencies": { + "cross-fetch": "^3.0.6", "daf-core": "^7.0.0-beta.57", "debug": "^4.1.1" }, "devDependencies": { "@types/debug": "^4.1.5", "did-resolver": "^2.1.2", - "typescript": "^4.0.3" + "ethr-did-resolver": "^3.0.2", + "typescript": "^4.0.3", + "web-did-resolver": "^1.3.5" }, "files": [ "build/**/*", diff --git a/packages/daf-resolver/src/__tests__/integration.test.ts b/packages/daf-resolver/src/__tests__/integration.test.ts new file mode 100644 index 000000000..fa1c6278b --- /dev/null +++ b/packages/daf-resolver/src/__tests__/integration.test.ts @@ -0,0 +1,134 @@ +import { DafResolver } from '../resolver' +import { Resolver } from 'did-resolver' +import { getResolver as getEthrResolver } from 'ethr-did-resolver' +import { getResolver as getWebDidResolver } from 'web-did-resolver' +import { UniversalResolver } from '../universal-resolver' + +const providerConfig = { + networks: [ + { name: 'rinkeby', rpcUrl: 'https://rinkeby.infura.io/v3/6b734e0b04454df8a6ce234023c04f26' }, + { name: 'development', rpcUrl: 'http://localhost:7545' }, + //FIXME: add this example + // { name: 'test', provider: TBD_add_example_of_custom_provider_usage }, + ], +} + +const uniResolver = new UniversalResolver({ url: 'https://dev.uniresolver.io/1.0/identifiers/' }) + +/** This creates a resolver that supports the [ethr, web, key, elem] DID methods */ +let resolver: Resolver = new Resolver({ + //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 + key: uniResolver, + elem: uniResolver, +}) + +let resolverPlugin: DafResolver = new DafResolver({ resolver }) + +describe('daf-resolver', () => { + beforeAll(() => {}) + + it('should resolve web DID', async () => { + expect.assertions(1) + await expect(resolverPlugin.resolveDid({ didUrl: 'did:web:did.actor:alice' })).resolves.toMatchObject({ + '@context': 'https://w3id.org/did/v0.11', + id: 'did:web:did.actor:alice', + publicKey: [ + { + id: 'did:web:did.actor:alice#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN', + controller: 'did:web:did.actor:alice', + type: 'Ed25519VerificationKey2018', + publicKeyBase58: 'DK7uJiq9PnPnj7AmNZqVBFoLuwTjT1hFPrk6LSjZ2JRz', + }, + ], + authentication: ['did:web:did.actor:alice#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN'], + assertionMethod: ['did:web:did.actor:alice#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN'], + capabilityDelegation: ['did:web:did.actor:alice#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN'], + capabilityInvocation: ['did:web:did.actor:alice#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN'], + keyAgreement: [ + { + id: 'did:web:did.actor:alice#zC8GybikEfyNaausDA4mkT4egP7SNLx2T1d1kujLQbcP6h', + type: 'X25519KeyAgreementKey2019', + controller: 'did:web:did.actor:alice', + publicKeyBase58: 'CaSHXEvLKS6SfN9aBfkVGBpp15jSnaHazqHgLHp8KZ3Y', + }, + ], + }) + }) + + it('should resolve ethr-did with RPC URL', async () => { + expect.assertions(1) + await expect( + resolverPlugin.resolveDid({ didUrl: 'did:ethr:rinkeby:0xE6Fe788d8ca214A080b0f6aC7F48480b2AEfa9a6' }), + ).resolves.toMatchObject({ + '@context': 'https://w3id.org/did/v1', + id: 'did:ethr:rinkeby:0xE6Fe788d8ca214A080b0f6aC7F48480b2AEfa9a6', + publicKey: [ + { + id: 'did:ethr:rinkeby:0xE6Fe788d8ca214A080b0f6aC7F48480b2AEfa9a6#controller', + type: 'Secp256k1VerificationKey2018', + controller: 'did:ethr:rinkeby:0xE6Fe788d8ca214A080b0f6aC7F48480b2AEfa9a6', + ethereumAddress: '0xe6fe788d8ca214a080b0f6ac7f48480b2aefa9a6', + }, + ], + authentication: [ + { + type: 'Secp256k1SignatureAuthentication2018', + publicKey: 'did:ethr:rinkeby:0xE6Fe788d8ca214A080b0f6aC7F48480b2AEfa9a6#controller', + }, + ], + }) + }) + + it('should resolve did:key using uniresolver', async () => { + expect.assertions(1) + await expect( + resolverPlugin.resolveDid({ didUrl: 'did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6' }), + ).resolves.toMatchObject({ + '@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 fail predictably when unsupported method is resolved', async () => { + expect.assertions(1) + await expect(resolverPlugin.resolveDid({ didUrl: 'did:unsupported:lorem.ipsum' })).rejects.toThrowError( + "Unsupported DID method: 'unsupported'", + ) + }) + + it.todo('should resolve ethr-did with custom web3 provider') +}) diff --git a/packages/daf-resolver/src/__tests__/resolver.test.ts b/packages/daf-resolver/src/__tests__/resolver.test.ts index 9e50ab8cd..c495266e3 100644 --- a/packages/daf-resolver/src/__tests__/resolver.test.ts +++ b/packages/daf-resolver/src/__tests__/resolver.test.ts @@ -6,17 +6,13 @@ describe('daf-resolver', () => { expect(() => { new DafResolver({ //@ts-ignore - resolver: undefined + resolver: undefined, }) }).toThrow() }) -it.todo('should resolve example did') -it.todo('should fail predictably when unsupported method is resolved') -it.todo('should resolve ethr-did with RPC URL') -it.todo('should resolve ethr-did with custom web3 provider') + it('should have resolve method', () => { const resolver = new DafResolver({ resolver: new Resolver() }) expect(resolver).toHaveProperty('resolveDid') }) - }) diff --git a/packages/daf-resolver/src/universal-resolver.ts b/packages/daf-resolver/src/universal-resolver.ts index b4bdd3a07..12b297a6f 100644 --- a/packages/daf-resolver/src/universal-resolver.ts +++ b/packages/daf-resolver/src/universal-resolver.ts @@ -1,4 +1,5 @@ -import { DIDDocument } from 'did-resolver' +import { DIDDocument, ParsedDID, Resolver } from 'did-resolver' +import fetch from 'cross-fetch' interface Options { url: string diff --git a/yarn.lock b/yarn.lock index 51a161237..173df749f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5288,17 +5288,7 @@ concat-stream@^1.5.0, concat-stream@^1.5.1, concat-stream@^1.6.2: readable-stream "^2.2.2" typedarray "^0.0.6" -concat-stream@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-2.0.0.tgz#414cf5af790a48c60ab9be4527d56d5e41133cb1" - integrity sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A== - dependencies: - buffer-from "^1.0.0" - inherits "^2.0.3" - readable-stream "^3.0.2" - typedarray "^0.0.6" - -"concat-stream@github:hugomrdias/concat-stream#feat/smaller": +concat-stream@^2.0.0, "concat-stream@github:hugomrdias/concat-stream#feat/smaller": version "2.0.0" resolved "https://codeload.github.com/hugomrdias/concat-stream/tar.gz/057bc7b5d6d8df26c8cf00a3f151b6721a0a8034" dependencies: @@ -5798,7 +5788,7 @@ debug@^3.1.0, debug@^3.2.6: dependencies: ms "^2.1.1" -debuglog@*, debuglog@^1.0.1: +debuglog@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492" integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI= @@ -8867,7 +8857,7 @@ import-local@^3.0.2: pkg-dir "^4.2.0" resolve-cwd "^3.0.0" -imurmurhash@*, imurmurhash@^0.1.4: +imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" integrity sha1-khi5srkoojixPcT7a21XbyMUU+o= @@ -11152,11 +11142,6 @@ lockfile@^1.0.4: dependencies: signal-exit "^3.0.2" -lodash._baseindexof@*: - version "3.1.0" - resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c" - integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw= - lodash._baseuniq@~4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8" @@ -11165,33 +11150,11 @@ lodash._baseuniq@~4.6.0: lodash._createset "~4.0.0" lodash._root "~3.0.0" -lodash._bindcallback@*: - version "3.0.1" - resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e" - integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4= - -lodash._cacheindexof@*: - version "3.0.2" - resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92" - integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI= - -lodash._createcache@*: - version "3.1.2" - resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093" - integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM= - dependencies: - lodash._getnative "^3.0.0" - lodash._createset@~4.0.0: version "4.0.3" resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26" integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY= -lodash._getnative@*, lodash._getnative@^3.0.0: - version "3.9.1" - resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" - integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U= - lodash._reinterpolate@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d" @@ -11272,11 +11235,6 @@ lodash.partition@^4.6.0: resolved "https://registry.yarnpkg.com/lodash.partition/-/lodash.partition-4.6.0.tgz#a38e46b73469e0420b0da1212e66d414be364ba4" integrity sha1-o45GtzRp4EILDaEhLmbUFL42S6Q= -lodash.restparam@*: - version "3.6.1" - resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" - integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU= - lodash.set@^4.3.2: version "4.3.2" resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"