Skip to content

Commit

Permalink
fix(daf-resolver): import cross-fetch in universal-resolver.ts (#303)
Browse files Browse the repository at this point in the history
This fixes #302

Also add integration tests for the daf-resolver package
  • Loading branch information
mirceanis authored Dec 16, 2020
1 parent 62be3ee commit 57be4ea
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 53 deletions.
5 changes: 4 additions & 1 deletion packages/daf-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*",
Expand Down
134 changes: 134 additions & 0 deletions packages/daf-resolver/src/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
8 changes: 2 additions & 6 deletions packages/daf-resolver/src/__tests__/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

})
3 changes: 2 additions & 1 deletion packages/daf-resolver/src/universal-resolver.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
48 changes: 3 additions & 45 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 57be4ea

Please sign in to comment.