Skip to content

Commit

Permalink
docs: update readme with v3.0 API (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
oed authored Mar 2, 2021
1 parent 3daa229 commit 807652f
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ This is intended to support the proposed [Decentralized Identifiers](https://w3c

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

## Breaking changes (0.1.0)

This removes the global resolver. We've learnt that this ended up causing many problems. Instead you should now instantiate a Resolver object and use that to resolve did's within your consuming app/library.

### Changes required for developers of DID resolvers

You now no longer register your DID resolvers using the global `registerMethod()` function. Instead export the resolver function and show how to configure it in a local resolver object.

## 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:
```js
{
{
ethr: resolve,
web: resolve
}
Expand All @@ -49,6 +41,29 @@ const resolver = new Resolver({
const resolver = new Resolver(ethrResolver)
```

### Using legacy DID Method resolvers
DID Method resolvers created before version `3.0.0` of this library can be passed as legacy resolvers.
```js
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.
Expand All @@ -72,7 +87,9 @@ The built in Cache can be enabled by passing in a `true` value to the constructo
const resolver = new DIDResolver({
ethr,
web
}, true)
}, {
cache: true
})
```

Here is an example using `js-cache` which has not been tested.
Expand All @@ -93,14 +110,16 @@ const customCache : DIDCache = (parsed, resolve) => {
const resolver = new DIDResolver({
ethr,
web
}, customCache)
}, {
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)` function. e.g. `{ ethr: resolve }`.
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](https://github.com/decentralized-identity/did-resolver/blob/develop/src/resolver.ts#L51)

Expand All @@ -109,15 +128,20 @@ export function getResolver() {
async function resolve(
did: string,
parsed: ParsedDID,
didResolver: Resolver
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 didDoc
// Return the DIDResolutionResult object
return {
didResolutionMetadata: { contentType: 'application/did+ld+json' },
didDocument: didDoc
didDocumentMetadata: { ... }
}
}

return { myMethod: resolve }
Expand Down

0 comments on commit 807652f

Please sign in to comment.