@interop/did-web-resolver 0.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +65 -22
- package/build-dist.sh +14 -0
- package/dist/DidWebResolver.js +336 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/package.json +3 -0
- package/dist/index.js +14 -0
- package/package.json +48 -23
- package/rollup.config.js +15 -0
- package/src/DidWebResolver.js +228 -31
- package/src/index.js +7 -2
- package/.idea/codeStyles/Project.xml +0 -79
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/dbnavigator.xml +0 -456
- package/.idea/did-web-driver.iml +0 -12
- package/.idea/misc.xml +0 -10
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.idea/workspace.xml +0 -176
- package/.travis.yml +0 -10
- package/src/main.js +0 -5
- package/test/karma.conf.js +0 -40
- package/test/unit/DidWebResolver.spec.js +0 -152
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# did-web-driver ChangeLog
|
|
2
2
|
|
|
3
|
+
## 2.0.0 - 2022-01-01
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
- Use rollup for build-time transpile instead of esm.
|
|
7
|
+
- Use "no ESM" branches for all other deps.
|
|
8
|
+
- `.get()` now also resolves keys (to match other did-io drivers).
|
|
9
|
+
- **BREAKING**: Update ed25519 and X25519 dependencies to latest. You will have
|
|
10
|
+
to re-generate your `did:web` DID documents for this version, as the
|
|
11
|
+
key serialization formats have changed.
|
|
12
|
+
|
|
13
|
+
## 1.1.0 - 2021-04-25
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
- Add `didWebDriver.publicMethodFor()`.
|
|
17
|
+
|
|
18
|
+
## 1.0.1 - 2021-04-25
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
- Fix handling of hash fragments by `urlFromDid()`.
|
|
22
|
+
- Add logger to constructor.
|
|
23
|
+
|
|
24
|
+
## 1.0.0 - 2021-04-24
|
|
25
|
+
|
|
26
|
+
### Changed
|
|
27
|
+
- **BREAKING** Update to latest DID Core context
|
|
28
|
+
- **BREAKING** Update to use crypto-ld v5 API, latest crypto suites
|
|
29
|
+
- Add support for X25519KeyAgreementKey suite
|
|
30
|
+
|
|
31
|
+
## 0.2.0 - 2020-08-01
|
|
32
|
+
|
|
33
|
+
### Changed
|
|
34
|
+
- **BREAKING**: Update to use crypto-ld v4 API
|
|
35
|
+
|
|
3
36
|
## 0.0.1
|
|
4
37
|
|
|
5
38
|
### Added
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
# `did:web` Resolver
|
|
1
|
+
# `did:web` Resolver _(@interop/did-web-resolver)_
|
|
2
2
|
|
|
3
|
-
[](https://github.com/interop-alliance/did-web-resolver/actions?query=workflow%3A%22Node.js+CI%22)
|
|
4
|
+
[](https://npm.im/@interop/did-web-resolver)
|
|
5
5
|
|
|
6
6
|
> A did:web method Decentralized Identifier (DID) resolver for the did-io library.
|
|
7
7
|
|
|
@@ -33,36 +33,79 @@ Other implementations:
|
|
|
33
33
|
|
|
34
34
|
## Usage
|
|
35
35
|
|
|
36
|
+
### Initializing
|
|
37
|
+
|
|
36
38
|
```js
|
|
39
|
+
import { Ed25519VerificationKey2020 }
|
|
40
|
+
from '@digitalbazaar/ed25519-verification-key-2020'
|
|
41
|
+
import { X25519KeyAgreementKey2020 }
|
|
42
|
+
from '@digitalbazaar/x25519-key-agreement-key-2020'
|
|
37
43
|
import { CryptoLD } from 'crypto-ld'
|
|
38
|
-
|
|
44
|
+
|
|
45
|
+
import * as didWeb from '@interop/did-web-resolver'
|
|
39
46
|
|
|
40
47
|
const cryptoLd = new CryptoLD()
|
|
41
|
-
cryptoLd.use(
|
|
42
|
-
|
|
43
|
-
import { DidWebResolver } from '@interop/did-web-resolver'
|
|
44
|
-
const keyMap = { // default
|
|
45
|
-
capabilityInvocation: 'ed25519',
|
|
46
|
-
authentication: 'ed25519',
|
|
47
|
-
assertionMethod: 'ed25519',
|
|
48
|
-
capabilityDelegation: 'ed25519',
|
|
49
|
-
// keyAgreement: 'x25519' // <- not yet supported
|
|
50
|
-
}
|
|
48
|
+
cryptoLd.use(Ed25519VerificationKey2020)
|
|
49
|
+
cryptoLd.use(X25519KeyAgreementKey2020)
|
|
51
50
|
|
|
52
|
-
const
|
|
51
|
+
const didWebDriver = didWeb.driver({ cryptoLd })
|
|
53
52
|
|
|
54
|
-
// Optionally use it with
|
|
55
|
-
import {
|
|
56
|
-
const
|
|
53
|
+
// Optionally use it with the CachedResolver from did-io
|
|
54
|
+
import {CachedResolver} from '@digitalbazaar/did-io';
|
|
55
|
+
const resolver = new CachedResolver()
|
|
56
|
+
resolver.use(didWebDriver)
|
|
57
|
+
```
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
### Generating a new DID
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const { didDocument, keyPairs, methodFor } = await didWebDriver.generate()
|
|
63
|
+
|
|
64
|
+
{
|
|
65
|
+
'@context': [
|
|
66
|
+
'https://www.w3.org/ns/did/v1',
|
|
67
|
+
'https://w3id.org/security/suites/ed25519-2020/v1',
|
|
68
|
+
'https://w3id.org/security/suites/x25519-2020/v1'
|
|
69
|
+
],
|
|
70
|
+
id: 'did:web:example.com',
|
|
71
|
+
capabilityInvocation: [{
|
|
72
|
+
id: 'did:web:example.com#z6MkqUiWi2o5V5oDEVzqszpkDhzeJ2o9Z4zVyTWeASqgrgti',
|
|
73
|
+
type: 'Ed25519VerificationKey2020',
|
|
74
|
+
controller: 'did:web:example.com',
|
|
75
|
+
publicKeyMultibase: 'zC2TU7nYe9YJk81A9CRruNcSeUTXJ9Bk9HSbiLAsfwU7L'
|
|
76
|
+
}],
|
|
77
|
+
authentication: [{
|
|
78
|
+
id: 'did:web:example.com#z6MksjNYAxjiTrhPFx9Ljk3SVowEtFXhFqLdsMKJHV4KrcDT',
|
|
79
|
+
type: 'Ed25519VerificationKey2020',
|
|
80
|
+
controller: 'did:web:example.com',
|
|
81
|
+
publicKeyMultibase: 'zEH7VaiVH8KCv9TJe4B5beiPF4gFqqx6HBLQNTD6JwPS5'
|
|
82
|
+
}],
|
|
83
|
+
assertionMethod: [{
|
|
84
|
+
id: 'did:web:example.com#z6MkiyYa5mG4moiHrmXQea8bNvdEWRWi3KuouHqoiknGf7xV',
|
|
85
|
+
type: 'Ed25519VerificationKey2020',
|
|
86
|
+
controller: 'did:web:example.com',
|
|
87
|
+
publicKeyMultibase: 'z5XHXVX1dSGDpkGghy1AkXq5EgrErdSfTDGvstUpFjuB7'
|
|
88
|
+
}],
|
|
89
|
+
capabilityDelegation: [{
|
|
90
|
+
id: 'did:web:example.com#z6MknmeMZEXLhS6g2p6YPHkQG4PkNsJev652CqnsArPm3dZa',
|
|
91
|
+
type: 'Ed25519VerificationKey2020',
|
|
92
|
+
controller: 'did:web:example.com',
|
|
93
|
+
publicKeyMultibase: 'z9KPJxzGuMtcCvKFqhinZQxqkZJ2oWCpfWpswLaRk8QnC'
|
|
94
|
+
}],
|
|
95
|
+
keyAgreement: [{
|
|
96
|
+
id: 'did:web:example.com#z6LSg3dWQzpQgRpaVzNXkcosnheyhXJUQkNQQgXyDapFSCFZ',
|
|
97
|
+
type: 'X25519KeyAgreementKey2020',
|
|
98
|
+
controller: 'did:web:example.com',
|
|
99
|
+
publicKeyMultibase: 'z5NTLth1Yay6qQbzmDyHvU7SVrNmMi9CFXhpHj8AiipUo'
|
|
100
|
+
}]
|
|
101
|
+
}
|
|
59
102
|
```
|
|
60
103
|
|
|
61
104
|
## Install
|
|
62
105
|
|
|
63
106
|
```bash
|
|
64
|
-
git clone https://github.com/interop-alliance/did-web-
|
|
65
|
-
cd did-web-
|
|
107
|
+
git clone https://github.com/interop-alliance/did-web-resolver.git
|
|
108
|
+
cd did-web-resolver
|
|
66
109
|
npm install
|
|
67
110
|
```
|
|
68
111
|
|
|
@@ -76,4 +119,4 @@ PRs accepted.
|
|
|
76
119
|
|
|
77
120
|
## License
|
|
78
121
|
|
|
79
|
-
[The MIT License](LICENSE.md) © Interop Alliance and Dmitri Zagidulin
|
|
122
|
+
[The MIT License](LICENSE.md) ©2020-2021 Interop Alliance and Dmitri Zagidulin
|
package/build-dist.sh
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
mkdir ./dist/esm
|
|
2
|
+
cat >dist/esm/index.js <<!EOF
|
|
3
|
+
import cjsModule from '../index.js';
|
|
4
|
+
export const driver = cjsModule.driver;
|
|
5
|
+
export const DidWebResolver = cjsModule.DidWebResolver;
|
|
6
|
+
export const didFromUrl = cjsModule.didFromUrl;
|
|
7
|
+
export const urlFromDid = cjsModule.urlFromDid;
|
|
8
|
+
!EOF
|
|
9
|
+
|
|
10
|
+
cat >dist/esm/package.json <<!EOF
|
|
11
|
+
{
|
|
12
|
+
"type": "module"
|
|
13
|
+
}
|
|
14
|
+
!EOF
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var httpClient = require('@digitalbazaar/http-client');
|
|
6
|
+
var didIo = require('@digitalbazaar/did-io');
|
|
7
|
+
var ed25519Context = require('ed25519-signature-2020-context');
|
|
8
|
+
var x25519Context = require('x25519-key-agreement-2020-context');
|
|
9
|
+
var didContext = require('did-context');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
function _interopNamespace(e) {
|
|
14
|
+
if (e && e.__esModule) return e;
|
|
15
|
+
var n = Object.create(null);
|
|
16
|
+
if (e) {
|
|
17
|
+
Object.keys(e).forEach(function (k) {
|
|
18
|
+
if (k !== 'default') {
|
|
19
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return e[k]; }
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
n["default"] = e;
|
|
28
|
+
return Object.freeze(n);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var didIo__namespace = /*#__PURE__*/_interopNamespace(didIo);
|
|
32
|
+
var ed25519Context__default = /*#__PURE__*/_interopDefaultLegacy(ed25519Context);
|
|
33
|
+
var x25519Context__default = /*#__PURE__*/_interopDefaultLegacy(x25519Context);
|
|
34
|
+
var didContext__default = /*#__PURE__*/_interopDefaultLegacy(didContext);
|
|
35
|
+
|
|
36
|
+
const { VERIFICATION_RELATIONSHIPS } = didIo__namespace;
|
|
37
|
+
|
|
38
|
+
const DEFAULT_KEY_MAP = {
|
|
39
|
+
capabilityInvocation: 'Ed25519VerificationKey2020',
|
|
40
|
+
authentication: 'Ed25519VerificationKey2020',
|
|
41
|
+
assertionMethod: 'Ed25519VerificationKey2020',
|
|
42
|
+
capabilityDelegation: 'Ed25519VerificationKey2020',
|
|
43
|
+
keyAgreement: 'X25519KeyAgreementKey2020'
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function didFromUrl ({ url } = {}) {
|
|
47
|
+
if (!url) {
|
|
48
|
+
throw new TypeError('Cannot convert url to did, missing url.')
|
|
49
|
+
}
|
|
50
|
+
if (url.startsWith('http:')) {
|
|
51
|
+
throw new TypeError('did:web does not support non-HTTPS URLs.')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let parsedUrl;
|
|
55
|
+
try {
|
|
56
|
+
parsedUrl = new URL(url);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw new TypeError(`Invalid url: "${url}".`)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const { host, pathname } = parsedUrl;
|
|
62
|
+
|
|
63
|
+
let pathComponent = '';
|
|
64
|
+
if (pathname && pathname !== '/' && pathname !== '/.well-known/did.json') {
|
|
65
|
+
pathComponent = pathname.split('/').map(encodeURIComponent).join(':');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return 'did:web:' + encodeURIComponent(host) + pathComponent
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function urlFromDid ({ did } = {}) {
|
|
72
|
+
if (!did) {
|
|
73
|
+
throw new TypeError('Cannot convert did to url, missing did.')
|
|
74
|
+
}
|
|
75
|
+
if (!did.startsWith('did:web:')) {
|
|
76
|
+
throw new TypeError(`DID Method not supported: "${did}".`)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const [didUrl, hashFragment] = did.split('#');
|
|
80
|
+
// eslint-disable-next-line no-unused-vars
|
|
81
|
+
// const [didResource, query] = didUrl.split('?')
|
|
82
|
+
|
|
83
|
+
// eslint-disable-next-line no-unused-vars
|
|
84
|
+
const [_did, _web, urlNoProtocol] = didUrl.split(':');
|
|
85
|
+
|
|
86
|
+
let parsedUrl;
|
|
87
|
+
try {
|
|
88
|
+
// URI-decode the url (in case it contained a port number,
|
|
89
|
+
// for example, `did:web:localhost%3A8080`
|
|
90
|
+
parsedUrl = new URL('https://' + decodeURIComponent(urlNoProtocol));
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw new TypeError(`Cannot construct url from did: "${did}".`)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!parsedUrl.pathname || parsedUrl.pathname === '/') {
|
|
96
|
+
parsedUrl.pathname = '/.well-known/did.json';
|
|
97
|
+
} else {
|
|
98
|
+
const pathFragments = parsedUrl.pathname.split('/');
|
|
99
|
+
parsedUrl.pathname = pathFragments.map(decodeURIComponent).join('/');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (hashFragment) {
|
|
103
|
+
parsedUrl.hash = hashFragment;
|
|
104
|
+
}
|
|
105
|
+
return parsedUrl.toString()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Initializes the DID Document's keys/proof methods.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* didDocument.id = 'did:ex:123';
|
|
113
|
+
* const {didDocument, keyPairs} = await initKeys({
|
|
114
|
+
* didDocument,
|
|
115
|
+
* cryptoLd,
|
|
116
|
+
* keyMap: {
|
|
117
|
+
* capabilityInvocation: someExistingKey,
|
|
118
|
+
* authentication: 'Ed25519VerificationKey2020',
|
|
119
|
+
* assertionMethod: 'Ed25519VerificationKey2020',
|
|
120
|
+
* keyAgreement: 'X25519KeyAgreementKey2019'
|
|
121
|
+
* }
|
|
122
|
+
* });.
|
|
123
|
+
*
|
|
124
|
+
* @param {object} options - Options hashmap.
|
|
125
|
+
* @param {object} options.didDocument - DID Document.
|
|
126
|
+
* @typedef {object} CryptoLD
|
|
127
|
+
* @param {CryptoLD} [options.cryptoLd] - CryptoLD driver instance,
|
|
128
|
+
* initialized with the key types this DID Document intends to support.
|
|
129
|
+
* @param {object} [options.keyMap] - Map of keys (or key types) by purpose.
|
|
130
|
+
*
|
|
131
|
+
* @returns {Promise<{didDocument: object, keyPairs: Map}>} Resolves with the
|
|
132
|
+
* DID Document initialized with keys, as well as the map of the corresponding
|
|
133
|
+
* key pairs (by key id).
|
|
134
|
+
*/
|
|
135
|
+
async function initKeys ({ didDocument, cryptoLd, keyMap = {} } = {}) {
|
|
136
|
+
const doc = { ...didDocument };
|
|
137
|
+
if (!doc.id) {
|
|
138
|
+
throw new TypeError(
|
|
139
|
+
'DID Document "id" property is required to initialize keys.')
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const keyPairs = new Map();
|
|
143
|
+
|
|
144
|
+
// Set the defaults for the created keys (if needed)
|
|
145
|
+
const options = { controller: doc.id };
|
|
146
|
+
|
|
147
|
+
for (const purpose in keyMap) {
|
|
148
|
+
if (!VERIFICATION_RELATIONSHIPS.has(purpose)) {
|
|
149
|
+
throw new Error(`Unsupported key purpose: "${purpose}".`)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let key;
|
|
153
|
+
if (typeof keyMap[purpose] === 'string') {
|
|
154
|
+
if (!cryptoLd) {
|
|
155
|
+
throw new Error('Please provide an initialized CryptoLD instance.')
|
|
156
|
+
}
|
|
157
|
+
key = await cryptoLd.generate({ type: keyMap[purpose], ...options });
|
|
158
|
+
} else {
|
|
159
|
+
// An existing key has been provided
|
|
160
|
+
key = keyMap[purpose];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
doc[purpose] = [key.export({ publicKey: true })];
|
|
164
|
+
keyPairs.set(key.id, key);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return { didDocument: doc, keyPairs }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
class DidWebResolver {
|
|
171
|
+
/**
|
|
172
|
+
* @param cryptoLd {CryptoLD}
|
|
173
|
+
* @param keyMap {object}
|
|
174
|
+
* @param [logger] {object} Logger object (with .log, .error, .warn,
|
|
175
|
+
* etc methods).
|
|
176
|
+
*/
|
|
177
|
+
constructor ({ cryptoLd, keyMap = DEFAULT_KEY_MAP, logger = console } = {}) {
|
|
178
|
+
this.method = 'web'; // did:web:... (used for didIo resolver harness)
|
|
179
|
+
this.cryptoLd = cryptoLd;
|
|
180
|
+
this.keyMap = keyMap;
|
|
181
|
+
this.logger = logger;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Generates a new DID Document and initializes various authentication
|
|
186
|
+
* and authorization proof purpose keys.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* const url = 'https://example.com'
|
|
190
|
+
* const { didDocument, didKeys } = await didWeb.generate({url})
|
|
191
|
+
* didDocument.id
|
|
192
|
+
* // -> 'did:web:example.com'
|
|
193
|
+
*
|
|
194
|
+
*
|
|
195
|
+
* Either an `id` or a `url` is required:
|
|
196
|
+
* @param [id] {string} - A did:web DID. If absent, will be converted from url
|
|
197
|
+
* @param [url] {string}
|
|
198
|
+
*
|
|
199
|
+
* @param [keyMap=DEFAULT_KEY_MAP] {object} A hashmap of key types by purpose.
|
|
200
|
+
*
|
|
201
|
+
* @parma [cryptoLd] {object} CryptoLD instance with support for supported
|
|
202
|
+
* crypto suites installed.
|
|
203
|
+
*
|
|
204
|
+
* @returns {Promise<{didDocument: object, keyPairs: Map,
|
|
205
|
+
* methodFor: Function}>} Resolves with the generated DID Document, along
|
|
206
|
+
* with the corresponding key pairs used to generate it (for storage in a
|
|
207
|
+
* KMS).
|
|
208
|
+
*/
|
|
209
|
+
async generate ({ id, url, keyMap = this.keyMap, cryptoLd = this.cryptoLd } = {}) {
|
|
210
|
+
const did = id || didFromUrl({ url });
|
|
211
|
+
|
|
212
|
+
// Compose the DID Document
|
|
213
|
+
let didDocument = {
|
|
214
|
+
'@context': [
|
|
215
|
+
didContext__default["default"].constants.DID_CONTEXT_URL,
|
|
216
|
+
ed25519Context__default["default"].constants.CONTEXT_URL,
|
|
217
|
+
x25519Context__default["default"].constants.CONTEXT_URL
|
|
218
|
+
],
|
|
219
|
+
id: did
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const result = await initKeys({ didDocument, cryptoLd, keyMap });
|
|
223
|
+
const keyPairs = result.keyPairs;
|
|
224
|
+
didDocument = result.didDocument;
|
|
225
|
+
|
|
226
|
+
// Convenience function that returns the public/private key pair instance
|
|
227
|
+
// for a given purpose (authentication, assertionMethod, keyAgreement, etc).
|
|
228
|
+
const methodFor = ({ purpose }) => {
|
|
229
|
+
const { id: methodId } = didIo__namespace.findVerificationMethod({
|
|
230
|
+
doc: didDocument, purpose
|
|
231
|
+
});
|
|
232
|
+
return keyPairs.get(methodId)
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
return { didDocument, keyPairs, methodFor }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Fetches a DID Document for a given DID.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* // In Node.js tests, use an agent to avoid self-signed certificate errors
|
|
243
|
+
* const agent = new https.agent({rejectUnauthorized: false});
|
|
244
|
+
*
|
|
245
|
+
* @param {string} [did] For example, 'did:web:example.com'
|
|
246
|
+
* @param {string} [url]
|
|
247
|
+
* @param {https.Agent} [agent] Optional agent used to customize network
|
|
248
|
+
* behavior in Node.js (such as `rejectUnauthorized: false`).
|
|
249
|
+
* @param {object} [logger] Logger object (with .log, .error, .warn,
|
|
250
|
+
* etc methods).
|
|
251
|
+
*
|
|
252
|
+
* @throws {Error}
|
|
253
|
+
*
|
|
254
|
+
* @returns {Promise<object>} Plain parsed JSON object of the DID Document.
|
|
255
|
+
*/
|
|
256
|
+
async get ({ did, url, agent, logger = this.logger }) {
|
|
257
|
+
const didUrl = url || urlFromDid({ did });
|
|
258
|
+
if (!didUrl) {
|
|
259
|
+
throw new TypeError('A DID or a URL is required.')
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const [urlAuthority, keyIdFragment] = didUrl.split('#');
|
|
263
|
+
|
|
264
|
+
let didDocument;
|
|
265
|
+
try {
|
|
266
|
+
logger.info(`Fetching "${urlAuthority}" via http client.`);
|
|
267
|
+
const result = await httpClient.httpClient.get(urlAuthority, { agent });
|
|
268
|
+
didDocument = result.data;
|
|
269
|
+
} catch (e) {
|
|
270
|
+
// status is HTTP status code
|
|
271
|
+
// data is JSON error from the server if available
|
|
272
|
+
const { data, status } = e;
|
|
273
|
+
logger.error(`Http ${status} error:`, data);
|
|
274
|
+
throw e
|
|
275
|
+
}
|
|
276
|
+
if (didDocument && keyIdFragment) {
|
|
277
|
+
// resolve an individual key
|
|
278
|
+
// Keys are expected to have format: <did:web:...>#<keyIdFragment>
|
|
279
|
+
const didAuthority = didFromUrl({ url: urlAuthority });
|
|
280
|
+
const methodId = `${didAuthority}#${keyIdFragment}`;
|
|
281
|
+
|
|
282
|
+
const key = didIo__namespace.findVerificationMethod({ doc: didDocument, methodId });
|
|
283
|
+
if (!key) {
|
|
284
|
+
throw new Error(`Key id ${methodId} not found.`)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const keyPair = await this.cryptoLd.from(key);
|
|
288
|
+
|
|
289
|
+
return keyPair.export({ publicKey: true, includeContext: true })
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return didDocument
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Returns the public key (verification method) object for a given DID
|
|
297
|
+
* Document and purpose. Useful in conjunction with a `.get()` call.
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* const didDocument = await didKeyDriver.get({did});
|
|
301
|
+
* const authKeyData = didDriver.publicMethodFor({
|
|
302
|
+
* didDocument, purpose: 'authentication'
|
|
303
|
+
* });
|
|
304
|
+
* // You can then create a suite instance object to verify signatures etc.
|
|
305
|
+
* const authPublicKey = await cryptoLd.from(authKeyData);
|
|
306
|
+
* const {verify} = authPublicKey.verifier();
|
|
307
|
+
*
|
|
308
|
+
* @param {object} options - Options hashmap.
|
|
309
|
+
* @param {object} options.didDocument - DID Document (retrieved via a
|
|
310
|
+
* `.get()` or from some other source).
|
|
311
|
+
* @param {string} options.purpose - Verification method purpose, such as
|
|
312
|
+
* 'authentication', 'assertionMethod', 'keyAgreement' and so on.
|
|
313
|
+
*
|
|
314
|
+
* @returns {object} Returns the public key object (obtained from the DID
|
|
315
|
+
* Document), without a `@context`.
|
|
316
|
+
*/
|
|
317
|
+
publicMethodFor ({ didDocument, purpose } = {}) {
|
|
318
|
+
if (!didDocument) {
|
|
319
|
+
throw new TypeError('The "didDocument" parameter is required.')
|
|
320
|
+
}
|
|
321
|
+
if (!purpose) {
|
|
322
|
+
throw new TypeError('The "purpose" parameter is required.')
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const method = didIo__namespace.findVerificationMethod({ doc: didDocument, purpose });
|
|
326
|
+
if (!method) {
|
|
327
|
+
throw new Error(`No verification method found for purpose "${purpose}"`)
|
|
328
|
+
}
|
|
329
|
+
return method
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
exports.DidWebResolver = DidWebResolver;
|
|
334
|
+
exports.didFromUrl = didFromUrl;
|
|
335
|
+
exports.initKeys = initKeys;
|
|
336
|
+
exports.urlFromDid = urlFromDid;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var DidWebResolver = require('./DidWebResolver.js');
|
|
6
|
+
|
|
7
|
+
const driver = options => {
|
|
8
|
+
return new DidWebResolver.DidWebResolver(options)
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
exports.DidWebResolver = DidWebResolver.DidWebResolver;
|
|
12
|
+
exports.didFromUrl = DidWebResolver.didFromUrl;
|
|
13
|
+
exports.urlFromDid = DidWebResolver.urlFromDid;
|
|
14
|
+
exports.driver = driver;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interop/did-web-resolver",
|
|
3
3
|
"description": "A did:web method Decentralized Identifier (DID) resolver for the did-io library.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Dmitri Zagidulin",
|
|
7
7
|
"url": "https://github.com/dmitrizagidulin/"
|
|
@@ -14,45 +14,70 @@
|
|
|
14
14
|
"homepage": "https://github.com/interop-alliance/did-web-driver",
|
|
15
15
|
"bugs": "https://github.com/interop-alliance/did-web-driver/issues",
|
|
16
16
|
"scripts": {
|
|
17
|
+
"rollup": "rollup -c rollup.config.js",
|
|
18
|
+
"build": "npm run clear && npm run rollup && ./build-dist.sh",
|
|
19
|
+
"clear": "rimraf dist/ && mkdir dist",
|
|
20
|
+
"prepare": "npm run build",
|
|
21
|
+
"rebuild": "npm run clear && npm run build",
|
|
17
22
|
"test": "npm run standard && npm run test-node",
|
|
18
23
|
"test-node": "cross-env NODE_ENV=test mocha -r esm --preserve-symlinks -t 10000 test/**/*.spec.js",
|
|
19
24
|
"test-karma": "karma start test/karma.conf.js",
|
|
20
25
|
"nyc": "cross-env NODE_ENV=test nyc npm run test-node",
|
|
21
26
|
"standard": "standard --fix"
|
|
22
27
|
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src",
|
|
31
|
+
"rollup.config.js",
|
|
32
|
+
"build-dist.sh",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"main": "dist/index.js",
|
|
37
|
+
"module": "dist/esm/index.js",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"require": "./dist/index.js",
|
|
41
|
+
"import": "./dist/esm/index.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
23
44
|
"dependencies": {
|
|
24
|
-
"did-
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
45
|
+
"@digitalbazaar/did-io": "digitalbazaar/did-io#noesm",
|
|
46
|
+
"@digitalbazaar/http-client": "digitalbazaar/http-client#noesm",
|
|
47
|
+
"did-context": "digitalbazaar/did-context#nofs",
|
|
48
|
+
"ed25519-signature-2020-context": "^1.1.0",
|
|
49
|
+
"x25519-key-agreement-2020-context": "digitalbazaar/x25519-key-agreement-2020-context#nofs"
|
|
28
50
|
},
|
|
29
51
|
"devDependencies": {
|
|
30
|
-
"@babel/core": "^7.
|
|
31
|
-
"@babel/plugin-transform-modules-commonjs": "^7.
|
|
32
|
-
"@babel/plugin-transform-runtime": "^7.
|
|
33
|
-
"@babel/preset-env": "^7.
|
|
34
|
-
"@babel/runtime": "^7.
|
|
35
|
-
"@digitalbazaar/ed25519-verification-key-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
52
|
+
"@babel/core": "^7.16.7",
|
|
53
|
+
"@babel/plugin-transform-modules-commonjs": "^7.16.7",
|
|
54
|
+
"@babel/plugin-transform-runtime": "^7.16.7",
|
|
55
|
+
"@babel/preset-env": "^7.16.7",
|
|
56
|
+
"@babel/runtime": "^7.16.7",
|
|
57
|
+
"@digitalbazaar/ed25519-verification-key-2020": "^3.2.0",
|
|
58
|
+
"@digitalbazaar/x25519-key-agreement-key-2020": "^2.0.0",
|
|
59
|
+
"babel-loader": "^8.2.3",
|
|
60
|
+
"chai": "^4.3.4",
|
|
61
|
+
"cross-env": "^7.0.3",
|
|
62
|
+
"crypto-ld": "^6.0.0",
|
|
63
|
+
"esm": "^3.2.25",
|
|
40
64
|
"dirty-chai": "^2.0.1",
|
|
41
|
-
"karma": "^
|
|
65
|
+
"karma": "^6.3.9",
|
|
42
66
|
"karma-babel-preprocessor": "^8.0.1",
|
|
43
67
|
"karma-chai": "^0.1.0",
|
|
44
68
|
"karma-chrome-launcher": "^3.1.0",
|
|
45
69
|
"karma-mocha": "^2.0.1",
|
|
46
70
|
"karma-mocha-reporter": "^2.2.5",
|
|
47
|
-
"karma-sourcemap-loader": "^0.3.
|
|
48
|
-
"karma-webpack": "^
|
|
49
|
-
"mocha": "^8.
|
|
71
|
+
"karma-sourcemap-loader": "^0.3.8",
|
|
72
|
+
"karma-webpack": "^5.0.0",
|
|
73
|
+
"mocha": "^8.4.0",
|
|
50
74
|
"nyc": "^15.1.0",
|
|
51
|
-
"sinon": "^
|
|
52
|
-
"standard": "^
|
|
53
|
-
"
|
|
75
|
+
"sinon": "^12.0.1",
|
|
76
|
+
"standard": "^16.0.4",
|
|
77
|
+
"rimraf": "^3.0.2",
|
|
78
|
+
"rollup": "^2.62.0",
|
|
79
|
+
"webpack": "^5.65.0"
|
|
54
80
|
},
|
|
55
|
-
"main": "src/main.js",
|
|
56
81
|
"nyc": {
|
|
57
82
|
"reporter": [
|
|
58
83
|
"html",
|
package/rollup.config.js
ADDED