Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

did-resolver

decentralized-identity457kApache-2.04.1.0TypeScript support: included

Resolve DID documents

readme

npm npm codecov

Typescript DID Resolver

This library is intended as a simple common interface for javascript applications to resolve DID documents from Decentralized Identifiers (DIDs).

This is intended to support the proposed Decentralized Identifiers spec from the W3C Credentials Community Group.

The library does not implement any specific DID method, but allows DID method implementors to release npm packages that applications can add.

Configure Resolver object

You are now required to preconfigure a resolver during instantiation. The Resolver constructor expects a registry of methods mapped to a resolver function. For example:

{
  ethr: resolve,
  web: resolve
}

Each method resolver should expose a function called getResolver which will return an object containing one of these key/value pairs. Then you can flatten them into one object to pass into the Resolver constructor.

import { Resolver } from 'did-resolver'
import ethr from 'ethr-did-resolver'
import web from 'web-did-resolver'
import sov from 'sov-did-resolver'

//returns an object of { methodName: resolveFunction}
ethrResolver = ethr.getResolver()
webResolver = web.getResolver()

//If you are using multiple methods you need to flatten them into one object
const resolver = new Resolver({
  ...ethrResolver,
  ...webResolver,
})

//If you are using one method you can simply pass the result of getResolver( into the constructor
const resolver = new Resolver(ethrResolver)

Using legacy DID Method resolvers

DID Method resolvers created before version 3.0.0 of this library can be used as legacy resolvers.

import { Resolver } from 'did-resolver'
import web from 'web-did-resolver'
import sov from 'sov-did-resolver'

//returns an object of { methodName: resolveFunction}
webResolver = web.getResolver()
sovResolver = sov.getResolver()

//If you are using multiple methods you need to flatten them into one object
const resolver = new Resolver({}, {
  legacyResolvers: {
    ...webResolver,
    ...sovResolver,
  }
})

//If you are using one method you can simply pass the result of getResolver( into the constructor
const resolver = new Resolver(ethrResolver)

Resolving a DID document

The resolver presents a simple resolve() function that returns a ES6 Promise returning the DID document.

resolver.resolve('did:ethr:0xF3beAC30C498D9E26865F34fCAa57dBB935b0D74/some/path#fragment=123').then(doc => console.log)

// You can also use ES7 async/await syntax
const doc = await resolver.resolve('did:ethr:0xF3beAC30C498D9E26865F34fCAa57dBB935b0D74/some/path#fragment=123')

Caching

Resolving DID Documents can be expensive. It is in most cases best to cache DID documents. Caching has to be specifically enabled using the cache parameter

The built-in cache uses a Map, but does not have an automatic TTL, so entries don't expire. This is fine in most web, mobile and serverless contexts. If you run a long-running process you may want to use an existing configurable caching system.

The built-in Cache can be enabled by passing in a true value to the constructor:

const resolver = new DIDResolver({
  ethr,
  web
}, {
  cache: true
})

Here is an example using js-cache which has not been tested.

var cache = require('js-cache')
const customCache : DIDCache = (parsed, resolve) => {
  // DID spec requires to not cache if no-cache param is set
  if (parsed.params && parsed.params['no-cache'] === 'true') return await resolve()
  const cached = cache.get(parsed.didUrl)
  if (cached !== undefined) return cached
  const doc = await resolve()
  cache.set(parsed, doc, 60000)
  return doc
}

const resolver = new DIDResolver({
  ethr,
  web
}, {
  cache: customCache
})

Implementing a DID method

Each DID method will have its own methods for looking up an identifier on its respective blockchain or other decentralized storage mechanism.

To avoid misconfiguration, method implementers should export a getResolver() function which returns an object mapping the method name to a resolve(did: string, parsed: ParsedDID, didResolver: DIDResolver, options: DIDResolutionOptions) function. e.g. { ethr: resolve }.

The resolve function should accept a did string, and an object of type ParsedDID

export function getResolver() {
  async function resolve(
    did: string,
    parsed: ParsedDID,
    didResolver: Resolver,
    options: DIDResolutionOptions
  ): Promise<DIDDocument> {
    console.log(parsed)
    // {method: 'mymethod', id: 'abcdefg', did: 'did:mymethod:abcdefg/some/path#fragment=123', path: '/some/path', fragment: 'fragment=123'}
    const didDoc = ...// lookup doc
    // If you need to lookup another did as part of resolving this did document, the primary DIDResolver object is passed in as well
    const parentDID = await didResolver.resolve(...)
    // Return the DIDResolutionResult object
    return {
      didResolutionMetadata: { contentType: 'application/did+ld+json' },
      didDocument: didDoc
      didDocumentMetadata: { ... }
    }
  }

  return { myMethod: resolve }
}

The MyMethod getResolver() result could then be passed into the DIDResolver constructor. Note that it should be flattened if used with other methods as well.

import { DIDResolver } from 'did-resolver'
import MyMethod from 'mymethod-did-resolver'

const myResolver = MyMethod.getResolver()
const resolver = new DIDResolver(myResolver)

changelog

4.1.0 (2023-03-08)

Features

  • add type definitions for ConditionalProof2022 (#143) (c4f62d3)

4.0.1 (2022-10-17)

Bug Fixes

4.0.0 (2022-08-02)

Bug Fixes

  • types: update Service interface to match latest DID spec (#136) (4ead68e), closes #135

BREAKING CHANGES

  • types: the ServiceEndpoint type has been renamed to Service and a new type ServiceEndpoint was created to fit the DID spec

3.2.2 (2022-06-05)

Bug Fixes

  • build: fix typo in package exports (832ea58)

3.2.1 (2022-06-05)

Bug Fixes

  • ci: groom the build scripts and dependencies (#133) (5c71b08)

3.2.0 (2022-03-29)

Features

3.1.5 (2021-12-09)

Bug Fixes

3.1.4 (2021-12-04)

Bug Fixes

  • add optional [@context](https://github.com/context) to result data type (#108) (39a3301)

3.1.3 (2021-10-26)

Bug Fixes

3.1.2 (2021-09-29)

Bug Fixes

  • editorial fix to trigger a new release (52098b4)

3.1.1 (2021-09-27)

Bug Fixes

  • docs: editorial fix, include recent changes (db504fd)

3.1.0 (2021-03-26)

Features

  • export the Resolvable type definition (ddc0ec2)

3.0.2 (2021-03-12)

Bug Fixes

  • allow the use of % char in method specific ID (#86) (3a21466)

3.0.1 (2021-03-02)

Bug Fixes

  • export interfaces needed by resolvers (#83) (89e8c9c)

3.0.0 (2021-03-02)

Features

  • update resolve method signature to match did core spec (#82) (313e685), closes #79

BREAKING CHANGES

  • the Resolver and DIDResolver data types have been upgraded. To use a legacy resolver that simply returns a DIDDocument, please register it under the options.legacyResolvers constructor param

2.2.0 (2021-02-26)

Features

  • add type definition for JWK formatted public keys (#80) (f9b9c8d)

2.1.2 (2020-12-09)

Bug Fixes

  • types: fix data type for "authentication" array in DIDDocument (#60) (d017cb3)
  • extend DIDDocument.[@context](https://github.com/context) data type to include arrays (#70) (8781b69), closes #68

2.1.1 (2020-08-19)

Bug Fixes

2.1.0 (2020-08-14)

Features

  • rename PublicKey attribute 'owner' to 'controller' (#55) (850e5a5), closes #54

2.0.1 (2020-06-12)

Bug Fixes

  • ci: fine-tune release to include yarn.lock when committing the version bump (#46) (07f2f1d)

2.0.0 (2020-06-10)

Bug Fixes

  • return non-nullable DIDDocument on Resolver.resolve() (#21) (2753e3e)

BREAKING CHANGES

  • The Resolver.resolve() method throws an error (rejects the promise) if the DID document returned by the respective method is null. Also, null documents are not cached.

1.1.0 (2019-11-08)

Bug Fixes

  • typo in type declaration in resolver.ts (0b20750)

Features