@interop/did-web-resolver 3.0.0 → 4.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/dist/esm/DidWebResolver.d.ts +147 -0
- package/dist/esm/DidWebResolver.d.ts.map +1 -0
- package/dist/esm/DidWebResolver.js +322 -0
- package/dist/esm/DidWebResolver.js.map +1 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +6 -5
- package/dist/esm/index.js.map +1 -0
- package/dist/src/DidWebResolver.d.ts +146 -0
- package/dist/src/DidWebResolver.js +329 -0
- package/dist/src/DidWebResolver.js.map +1 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.js +12 -0
- package/dist/src/index.js.map +1 -0
- package/dist/test/DidWebResolver.spec.d.ts +1 -0
- package/dist/test/DidWebResolver.spec.js +183 -0
- package/dist/test/DidWebResolver.spec.js.map +1 -0
- package/package.json +47 -59
- package/src/{DidWebResolver.js → DidWebResolver.ts} +53 -36
- package/src/declarations.d.ts +11 -0
- package/src/{index.js → index.ts} +2 -2
- package/build-dist.sh +0 -14
- package/dist/DidWebResolver.js +0 -383
- package/dist/esm/package.json +0 -3
- package/dist/index.js +0 -14
- package/rollup.config.js +0 -15
package/dist/DidWebResolver.js
DELETED
|
@@ -1,383 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var httpClient = require('@digitalcredentials/http-client');
|
|
6
|
-
var didIo = require('@digitalcredentials/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
|
-
var bnid = require('@digitalcredentials/bnid');
|
|
11
|
-
var whatwgUrl = require('whatwg-url');
|
|
12
|
-
|
|
13
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
|
-
|
|
15
|
-
function _interopNamespace(e) {
|
|
16
|
-
if (e && e.__esModule) return e;
|
|
17
|
-
var n = Object.create(null);
|
|
18
|
-
if (e) {
|
|
19
|
-
Object.keys(e).forEach(function (k) {
|
|
20
|
-
if (k !== 'default') {
|
|
21
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
22
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
23
|
-
enumerable: true,
|
|
24
|
-
get: function () { return e[k]; }
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
n["default"] = e;
|
|
30
|
-
return Object.freeze(n);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
var didIo__namespace = /*#__PURE__*/_interopNamespace(didIo);
|
|
34
|
-
var ed25519Context__default = /*#__PURE__*/_interopDefaultLegacy(ed25519Context);
|
|
35
|
-
var x25519Context__default = /*#__PURE__*/_interopDefaultLegacy(x25519Context);
|
|
36
|
-
var didContext__default = /*#__PURE__*/_interopDefaultLegacy(didContext);
|
|
37
|
-
|
|
38
|
-
const { VERIFICATION_RELATIONSHIPS } = didIo__namespace;
|
|
39
|
-
|
|
40
|
-
const DEFAULT_KEY_MAP = {
|
|
41
|
-
capabilityInvocation: 'Ed25519VerificationKey2020',
|
|
42
|
-
authentication: 'Ed25519VerificationKey2020',
|
|
43
|
-
assertionMethod: 'Ed25519VerificationKey2020',
|
|
44
|
-
capabilityDelegation: 'Ed25519VerificationKey2020',
|
|
45
|
-
keyAgreement: 'X25519KeyAgreementKey2020'
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
function didFromUrl ({ url } = {}) {
|
|
49
|
-
if (!url) {
|
|
50
|
-
throw new TypeError('Cannot convert url to did, missing url.')
|
|
51
|
-
}
|
|
52
|
-
if (url.startsWith('http:')) {
|
|
53
|
-
throw new TypeError('did:web does not support non-HTTPS URLs.')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let parsedUrl;
|
|
57
|
-
try {
|
|
58
|
-
parsedUrl = new whatwgUrl.URL(url);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw new TypeError(`Invalid url: "${url}".`)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const { host, pathname } = parsedUrl;
|
|
64
|
-
|
|
65
|
-
let pathComponent = '';
|
|
66
|
-
if (pathname && pathname !== '/' && pathname !== '/.well-known/did.json') {
|
|
67
|
-
pathComponent = pathname.split('/').map(encodeURIComponent).join(':');
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return 'did:web:' + encodeURIComponent(host) + pathComponent
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function urlFromDid ({ did } = {}) {
|
|
74
|
-
if (!did) {
|
|
75
|
-
throw new TypeError('Cannot convert did to url, missing did.')
|
|
76
|
-
}
|
|
77
|
-
if (!did.startsWith('did:web:')) {
|
|
78
|
-
throw new TypeError(`DID Method not supported: "${did}".`)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const [didUrl, hashFragment] = did.split('#');
|
|
82
|
-
// eslint-disable-next-line no-unused-vars
|
|
83
|
-
// const [didResource, query] = didUrl.split('?')
|
|
84
|
-
|
|
85
|
-
// eslint-disable-next-line no-unused-vars
|
|
86
|
-
const [_did, _web, urlNoProtocol, ...pathFragments] = didUrl.split(':');
|
|
87
|
-
|
|
88
|
-
if (urlNoProtocol.includes('/')) {
|
|
89
|
-
throw new TypeError(`Cannot construct url from did: "${did}". domain-name cannot contain a path.`)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
let parsedUrl;
|
|
93
|
-
try {
|
|
94
|
-
// URI-decode the url (in case it contained a port number,
|
|
95
|
-
// for example, `did:web:localhost%3A8080`
|
|
96
|
-
parsedUrl = new whatwgUrl.URL('https://' + decodeURIComponent(urlNoProtocol));
|
|
97
|
-
} catch (error) {
|
|
98
|
-
throw new TypeError(`Cannot construct url from did: "${did}".`)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (pathFragments.length === 0) {
|
|
102
|
-
parsedUrl.pathname = '/.well-known/did.json';
|
|
103
|
-
} else {
|
|
104
|
-
parsedUrl.pathname = pathFragments.map(decodeURIComponent).join('/') + '/did.json';
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (hashFragment) {
|
|
108
|
-
parsedUrl.hash = hashFragment;
|
|
109
|
-
}
|
|
110
|
-
return parsedUrl.toString()
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Initializes the DID Document's keys/proof methods.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* didDocument.id = 'did:ex:123';
|
|
118
|
-
* const {didDocument, keyPairs} = await initKeys({
|
|
119
|
-
* didDocument,
|
|
120
|
-
* cryptoLd,
|
|
121
|
-
* keyMap: {
|
|
122
|
-
* capabilityInvocation: someExistingKey,
|
|
123
|
-
* authentication: 'Ed25519VerificationKey2020',
|
|
124
|
-
* assertionMethod: 'Ed25519VerificationKey2020',
|
|
125
|
-
* keyAgreement: 'X25519KeyAgreementKey2019'
|
|
126
|
-
* }
|
|
127
|
-
* });.
|
|
128
|
-
*
|
|
129
|
-
* @param {object} options - Options hashmap.
|
|
130
|
-
* @param {object} options.didDocument - DID Document.
|
|
131
|
-
* @typedef {object} CryptoLD
|
|
132
|
-
* @param {CryptoLD} [options.cryptoLd] - CryptoLD driver instance,
|
|
133
|
-
* initialized with the key types this DID Document intends to support.
|
|
134
|
-
* @param {object} [options.keyMap] - Map of keys (or key types) by purpose.
|
|
135
|
-
*
|
|
136
|
-
* @returns {Promise<{didDocument: object, keyPairs: Map}>} Resolves with the
|
|
137
|
-
* DID Document initialized with keys, as well as the map of the corresponding
|
|
138
|
-
* key pairs (by key id).
|
|
139
|
-
*/
|
|
140
|
-
async function initKeys ({ didDocument, cryptoLd, keyMap } = {}) {
|
|
141
|
-
const doc = { ...didDocument };
|
|
142
|
-
if (!doc.id) {
|
|
143
|
-
throw new TypeError(
|
|
144
|
-
'DID Document "id" property is required to initialize keys.')
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const keyPairs = new Map();
|
|
148
|
-
|
|
149
|
-
// Set the defaults for the created keys (if needed)
|
|
150
|
-
const options = { controller: doc.id };
|
|
151
|
-
|
|
152
|
-
for (const purpose in keyMap) {
|
|
153
|
-
if (!VERIFICATION_RELATIONSHIPS.has(purpose)) {
|
|
154
|
-
throw new Error(`Unsupported key purpose: "${purpose}".`)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
let key;
|
|
158
|
-
if (typeof keyMap[purpose] === 'string') {
|
|
159
|
-
if (!cryptoLd) {
|
|
160
|
-
throw new Error('Please provide an initialized CryptoLD instance.')
|
|
161
|
-
}
|
|
162
|
-
key = await cryptoLd.generate({ type: keyMap[purpose], ...options });
|
|
163
|
-
} else {
|
|
164
|
-
// An existing key has been provided
|
|
165
|
-
key = keyMap[purpose];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
doc[purpose] = [key.export({ publicKey: true })];
|
|
169
|
-
keyPairs.set(key.id, key);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return { didDocument: doc, keyPairs }
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
class DidWebResolver {
|
|
176
|
-
/**
|
|
177
|
-
* @param cryptoLd {CryptoLD}
|
|
178
|
-
* @param keyMap {object}
|
|
179
|
-
* @param [logger] {object} Logger object (with .log, .error, .warn,
|
|
180
|
-
* etc methods).
|
|
181
|
-
*/
|
|
182
|
-
constructor ({ cryptoLd, keyMap = DEFAULT_KEY_MAP, logger = console } = {}) {
|
|
183
|
-
this.method = 'web'; // did:web:... (used for didIo resolver harness)
|
|
184
|
-
this.cryptoLd = cryptoLd;
|
|
185
|
-
this.keyMap = keyMap;
|
|
186
|
-
this.logger = logger;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Generates a new DID Document and initializes various authentication
|
|
191
|
-
* and authorization proof purpose keys.
|
|
192
|
-
*
|
|
193
|
-
* @example
|
|
194
|
-
* const url = 'https://example.com'
|
|
195
|
-
* const { didDocument, didKeys } = await didWeb.generate({url})
|
|
196
|
-
* didDocument.id
|
|
197
|
-
* // -> 'did:web:example.com'
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* Either an `id` or a `url` is required:
|
|
201
|
-
* @param [id] {string} - A did:web DID. If absent, will be converted from url
|
|
202
|
-
* @param [url] {string}
|
|
203
|
-
*
|
|
204
|
-
* @param [keyMap] {object} A hashmap of key types by purpose.
|
|
205
|
-
*
|
|
206
|
-
* @parma [cryptoLd] {object} CryptoLD instance with support for supported
|
|
207
|
-
* crypto suites installed.
|
|
208
|
-
*
|
|
209
|
-
* @returns {Promise<{didDocument: object, keyPairs: Map,
|
|
210
|
-
* methodFor: Function}>} Resolves with the generated DID Document, along
|
|
211
|
-
* with the corresponding key pairs used to generate it (for storage in a
|
|
212
|
-
* KMS).
|
|
213
|
-
*/
|
|
214
|
-
async generate ({ id, url, seed, keyMap, cryptoLd = this.cryptoLd } = {}) {
|
|
215
|
-
if (!id && !url) {
|
|
216
|
-
throw new TypeError('A "url" or an "id" parameter is required.')
|
|
217
|
-
}
|
|
218
|
-
if (seed && keyMap) {
|
|
219
|
-
throw new TypeError(
|
|
220
|
-
'Either a "seed" or a "keyMap" param must be provided, but not both.'
|
|
221
|
-
)
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const did = id || didFromUrl({ url });
|
|
225
|
-
|
|
226
|
-
if (seed) {
|
|
227
|
-
const keyPair = await _keyPairFromSecretSeed({
|
|
228
|
-
seed, controller: did, cryptoLd
|
|
229
|
-
});
|
|
230
|
-
keyMap = { assertionMethod: keyPair };
|
|
231
|
-
} else {
|
|
232
|
-
keyMap = keyMap || this.keyMap;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// Compose the DID Document
|
|
236
|
-
let didDocument = {
|
|
237
|
-
'@context': [
|
|
238
|
-
didContext__default["default"].constants.DID_CONTEXT_URL,
|
|
239
|
-
ed25519Context__default["default"].constants.CONTEXT_URL,
|
|
240
|
-
x25519Context__default["default"].constants.CONTEXT_URL
|
|
241
|
-
],
|
|
242
|
-
id: did
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
const result = await initKeys({ didDocument, cryptoLd, keyMap });
|
|
246
|
-
const keyPairs = result.keyPairs;
|
|
247
|
-
didDocument = result.didDocument;
|
|
248
|
-
|
|
249
|
-
// Convenience function that returns the public/private key pair instance
|
|
250
|
-
// for a given purpose (authentication, assertionMethod, keyAgreement, etc).
|
|
251
|
-
const methodFor = ({ purpose }) => {
|
|
252
|
-
const { id: methodId } = didIo__namespace.findVerificationMethod({
|
|
253
|
-
doc: didDocument, purpose
|
|
254
|
-
});
|
|
255
|
-
return keyPairs.get(methodId)
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
return { didDocument, keyPairs, methodFor }
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Fetches a DID Document for a given DID.
|
|
263
|
-
*
|
|
264
|
-
* @example
|
|
265
|
-
* // In Node.js tests, use an agent to avoid self-signed certificate errors
|
|
266
|
-
* const agent = new https.agent({rejectUnauthorized: false});
|
|
267
|
-
*
|
|
268
|
-
* @param {string} [did] For example, 'did:web:example.com'
|
|
269
|
-
* @param {string} [url]
|
|
270
|
-
* @param {https.Agent} [agent] Optional agent used to customize network
|
|
271
|
-
* behavior in Node.js (such as `rejectUnauthorized: false`).
|
|
272
|
-
* @param {object} [logger] Logger object (with .log, .error, .warn,
|
|
273
|
-
* etc methods).
|
|
274
|
-
*
|
|
275
|
-
* @throws {Error}
|
|
276
|
-
*
|
|
277
|
-
* @returns {Promise<object>} Plain parsed JSON object of the DID Document.
|
|
278
|
-
*/
|
|
279
|
-
async get ({ did, url, agent, logger = this.logger }) {
|
|
280
|
-
const didUrl = url || urlFromDid({ did });
|
|
281
|
-
if (!didUrl) {
|
|
282
|
-
throw new TypeError('A DID or a URL is required.')
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const [urlAuthority, keyIdFragment] = didUrl.split('#');
|
|
286
|
-
|
|
287
|
-
let didDocument;
|
|
288
|
-
try {
|
|
289
|
-
logger.info(`Fetching "${urlAuthority}" via http client.`);
|
|
290
|
-
const result = await httpClient.httpClient.get(urlAuthority, { agent });
|
|
291
|
-
didDocument = result.data;
|
|
292
|
-
} catch (e) {
|
|
293
|
-
// status is HTTP status code
|
|
294
|
-
// data is JSON error from the server if available
|
|
295
|
-
const { data, status } = e;
|
|
296
|
-
logger.error(`Http ${status} error:`, data);
|
|
297
|
-
throw e
|
|
298
|
-
}
|
|
299
|
-
if (didDocument && keyIdFragment) {
|
|
300
|
-
// resolve an individual key
|
|
301
|
-
// Keys are expected to have format: <did:web:...>#<keyIdFragment>
|
|
302
|
-
const didAuthority = didFromUrl({ url: urlAuthority });
|
|
303
|
-
const methodId = `${didAuthority}#${keyIdFragment}`;
|
|
304
|
-
|
|
305
|
-
const key = didIo__namespace.findVerificationMethod({ doc: didDocument, methodId });
|
|
306
|
-
if (!key) {
|
|
307
|
-
throw new Error(`Key id ${methodId} not found.`)
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const keyPair = await this.cryptoLd.from(key);
|
|
311
|
-
|
|
312
|
-
return keyPair.export({ publicKey: true, includeContext: true })
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return didDocument
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Returns the public key (verification method) object for a given DID
|
|
320
|
-
* Document and purpose. Useful in conjunction with a `.get()` call.
|
|
321
|
-
*
|
|
322
|
-
* @example
|
|
323
|
-
* const didDocument = await didKeyDriver.get({did});
|
|
324
|
-
* const authKeyData = didDriver.publicMethodFor({
|
|
325
|
-
* didDocument, purpose: 'authentication'
|
|
326
|
-
* });
|
|
327
|
-
* // You can then create a suite instance object to verify signatures etc.
|
|
328
|
-
* const authPublicKey = await cryptoLd.from(authKeyData);
|
|
329
|
-
* const {verify} = authPublicKey.verifier();
|
|
330
|
-
*
|
|
331
|
-
* @param {object} options - Options hashmap.
|
|
332
|
-
* @param {object} options.didDocument - DID Document (retrieved via a
|
|
333
|
-
* `.get()` or from some other source).
|
|
334
|
-
* @param {string} options.purpose - Verification method purpose, such as
|
|
335
|
-
* 'authentication', 'assertionMethod', 'keyAgreement' and so on.
|
|
336
|
-
*
|
|
337
|
-
* @returns {object} Returns the public key object (obtained from the DID
|
|
338
|
-
* Document), without a `@context`.
|
|
339
|
-
*/
|
|
340
|
-
publicMethodFor ({ didDocument, purpose } = {}) {
|
|
341
|
-
if (!didDocument) {
|
|
342
|
-
throw new TypeError('The "didDocument" parameter is required.')
|
|
343
|
-
}
|
|
344
|
-
if (!purpose) {
|
|
345
|
-
throw new TypeError('The "purpose" parameter is required.')
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
const method = didIo__namespace.findVerificationMethod({ doc: didDocument, purpose });
|
|
349
|
-
if (!method) {
|
|
350
|
-
throw new Error(`No verification method found for purpose "${purpose}"`)
|
|
351
|
-
}
|
|
352
|
-
return method
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* @param options {object}
|
|
358
|
-
* @param options.seed {string|Uint8Array}
|
|
359
|
-
* @param controller {string}
|
|
360
|
-
* @param cryptoLd {object}
|
|
361
|
-
*
|
|
362
|
-
* @return {Promise<LDKeyPair>}
|
|
363
|
-
*/
|
|
364
|
-
async function _keyPairFromSecretSeed ({ seed, controller, cryptoLd } = {}) {
|
|
365
|
-
let seedBytes;
|
|
366
|
-
if (typeof seed === 'string') {
|
|
367
|
-
// Currently only supports base58 multibase / identity multihash encoding.
|
|
368
|
-
if (!seed.startsWith('z1A')) {
|
|
369
|
-
throw new TypeError('"seed" parameter must be a multibase/multihash encoded string, or a Uint8Array.')
|
|
370
|
-
}
|
|
371
|
-
seedBytes = bnid.decodeSecretKeySeed({ secretKeySeed: seed });
|
|
372
|
-
} else {
|
|
373
|
-
seedBytes = new Uint8Array(seed);
|
|
374
|
-
}
|
|
375
|
-
return cryptoLd.generate({
|
|
376
|
-
controller, seed: seedBytes, type: 'Ed25519VerificationKey2020'
|
|
377
|
-
})
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
exports.DidWebResolver = DidWebResolver;
|
|
381
|
-
exports.didFromUrl = didFromUrl;
|
|
382
|
-
exports.initKeys = initKeys;
|
|
383
|
-
exports.urlFromDid = urlFromDid;
|
package/dist/esm/package.json
DELETED
package/dist/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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/rollup.config.js
DELETED