Skip to content

Commit

Permalink
feat: refactor to resolve peer-did
Browse files Browse the repository at this point in the history
  • Loading branch information
nickreynolds committed Mar 15, 2023
1 parent 904e779 commit e04640c
Show file tree
Hide file tree
Showing 10 changed files with 9,196 additions and 436 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
.DS_Store

**/.idea/
yarn-error.log
223 changes: 0 additions & 223 deletions CHANGELOG.md

This file was deleted.

56 changes: 7 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,32 @@
[![npm](https://img.shields.io/npm/dt/web-did-resolver.svg)](https://www.npmjs.com/package/web-did-resolver)
[![npm](https://img.shields.io/npm/v/web-did-resolver.svg)](https://www.npmjs.com/package/web-did-resolver)
[![codecov](https://codecov.io/gh/decentralized-identity/web-did-resolver/branch/develop/graph/badge.svg)](https://codecov.io/gh/decentralized-identity/web-did-resolver)

# Web DID Resolver
# Peer DID Resolver

This library is intended to represent domains accessed through https as
[Decentralized Identifiers](https://w3c.github.io/did-core/#identifier)
and retrieve an associated [DID Document](https://w3c.github.io/did-core/#did-document-properties)

It supports the proposed [`did:web` method spec](https://w3c-ccg.github.io/did-method-web/) from
the [W3C Credentials Community Group](https://w3c-ccg.github.io).

It supports the proposed [`did:peer` method spec](https://identity.foundation/peer-did-method-spec/)
It requires the `did-resolver` library, which is the primary interface for resolving DIDs.

## DID method

To encode a DID for an HTTPS domain, simply prepend `did:web:` to domain name.

eg: `https://example.com -> did:web:example.com`

## DID Document

The DID resolver takes the domain and forms a [well-known URI](https://tools.ietf.org/html/rfc5785)
to access the DID Document.

For a did `did:web:example.com`, the resolver will attempt to access the document at
`https://example.com/.well-known/did.json`

A minimal DID Document might contain the following information:

```json
{
"@context": "https://w3id.org/did/v1",
"id": "did:web:example.com",
"publicKey": [
{
"id": "did:web:example.com#owner",
"type": "Secp256k1VerificationKey2018",
"controller": "did:web:example.com",
"publicKeyHex": "04ab0102bcae6c7c3a90b01a3879d9518081bc06123038488db9cb109b082a77d97ea3373e3dfde0eccd9adbdce11d0302ea5c098dbb0b310234c8689501749274"
}
],
"assertionMethod": [ "did:web:example.com#owner" ],
"authentication": [ "did:web:example.com#owner" ]
}
```

Note: this example uses the `Secp256k1VerificationKey2018` type and an `publicKeyHex` as a publicKey entry, signaling
that this DID is claiming to control the private key associated with that publicKey.

## Resolving a DID document

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

```js
import { Resolver } from 'did-resolver'
import { getResolver } from 'web-did-resolver'
import { getResolver } from 'peer-did-resolver'

const webResolver = getResolver()
const peerResolver = getResolver()

const didResolver = new Resolver({
...webResolver
...peerResolver
//...you can flatten multiple resolver methods into the Resolver
})

didResolver.resolve('did:web:uport.me').then(doc => console.log(doc))
didResolver.resolve('did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDEiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MSJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfQ').then(doc => console.log(doc))

// You can also use ES7 async/await syntax
;(async () => {
const doc = await didResolver.resolve('did:web:uport.me')
const doc = await didResolver.resolve('did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDEiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MSJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfQ')
console.log(doc)
})();
```
9 changes: 6 additions & 3 deletions example/react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"private": true,
"dependencies": {
"did-resolver": "4.0.1",
"peer-did-resolver": "../../",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-scripts": "5.0.1",
"web-did-resolver": "../../"
"react-scripts": "5.0.1"
},
"scripts": {
"start": "SKIP_PREFLIGHT_CHECK=true react-scripts start",
"start": "cross-env SKIP_PREFLIGHT_CHECK=true react-scripts start",
"build": "react-scripts build"
},
"eslintConfig": {
Expand All @@ -30,5 +30,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"cross-env": "^7.0.3"
}
}
4 changes: 2 additions & 2 deletions example/react-app/src/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import './App.css'
import { useState } from 'react'
import { Resolver } from 'did-resolver'
import { getResolver } from 'web-did-resolver'
import { getResolver } from 'peer-did-resolver'

const webDidResolver = getResolver()
const didResolver = new Resolver(webDidResolver)
function App() {
const [did, setDid] = useState('did:web:identity.foundation')
const [did, setDid] = useState('did:peer:2.Ez6LSpSrLxbAhg2SHwKk7kwpsH7DM7QjFS5iK6qP87eViohud.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludDEiLCJyIjpbImRpZDpleGFtcGxlOnNvbWVtZWRpYXRvciNzb21la2V5MSJdLCJhIjpbImRpZGNvbW0vdjIiLCJkaWRjb21tL2FpcDI7ZW52PXJmYzU4NyJdfQ')
const [resolved, setResolved] = useState()
return (
<div className="App">
Expand Down
Loading

0 comments on commit e04640c

Please sign in to comment.