@digitalbazaar/vc 2.1.0 → 3.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/lib/index.js CHANGED
@@ -1,8 +1,620 @@
1
1
  /**
2
- * A library for working with verifiable credentials - vc library.
2
+ * A JavaScript implementation of Verifiable Credentials.
3
3
  *
4
+ * @author Dave Longley
4
5
  * @author David I. Lehn
5
6
  *
6
- * Copyright 2017-2021 Digital Bazaar, Inc.
7
+ * @license BSD 3-Clause License
8
+ * Copyright (c) 2017-2022 Digital Bazaar, Inc.
9
+ * All rights reserved.
10
+ *
11
+ * Redistribution and use in source and binary forms, with or without
12
+ * modification, are permitted provided that the following conditions are met:
13
+ *
14
+ * Redistributions of source code must retain the above copyright notice,
15
+ * this list of conditions and the following disclaimer.
16
+ *
17
+ * Redistributions in binary form must reproduce the above copyright
18
+ * notice, this list of conditions and the following disclaimer in the
19
+ * documentation and/or other materials provided with the distribution.
20
+ *
21
+ * Neither the name of the Digital Bazaar, Inc. nor the names of its
22
+ * contributors may be used to endorse or promote products derived from
23
+ * this software without specific prior written permission.
24
+ *
25
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
+ * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
+ */
37
+ import jsonld from 'jsonld';
38
+ import jsigs from 'jsonld-signatures';
39
+ import {CredentialIssuancePurpose} from './CredentialIssuancePurpose.js';
40
+ import {documentLoader as _documentLoader} from './documentLoader.js';
41
+ export const defaultDocumentLoader =
42
+ jsigs.extendContextLoader(_documentLoader);
43
+ import * as credentialsContext from 'credentials-context';
44
+
45
+ const {AuthenticationProofPurpose} = jsigs.purposes;
46
+ const {constants: {CREDENTIALS_CONTEXT_V1_URL}} = credentialsContext;
47
+
48
+ export {CredentialIssuancePurpose};
49
+
50
+ // Z and T can be lowercase
51
+ // RFC3339 regex
52
+ export const dateRegex = new RegExp('^(\\d{4})-(0[1-9]|1[0-2])-' +
53
+ '(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):' +
54
+ '([0-5][0-9]):([0-5][0-9]|60)' +
55
+ '(\\.[0-9]+)?(Z|(\\+|-)([01][0-9]|2[0-3]):' +
56
+ '([0-5][0-9]))$', 'i');
57
+
58
+ /**
59
+ * @typedef {object} LinkedDataSignature
60
+ */
61
+
62
+ /**
63
+ * @typedef {object} Presentation
64
+ */
65
+
66
+ /**
67
+ * @typedef {object} ProofPurpose
68
+ */
69
+
70
+ /**
71
+ * @typedef {object} VerifiableCredential
72
+ */
73
+
74
+ /**
75
+ * @typedef {object} VerifiablePresentation
76
+ */
77
+
78
+ /**
79
+ * @typedef {object} VerifyPresentationResult
80
+ * @property {boolean} verified - True if verified, false if not.
81
+ * @property {object} presentationResult
82
+ * @property {Array} credentialResults
83
+ * @property {object} error
84
+ */
85
+
86
+ /**
87
+ * @typedef {object} VerifyCredentialResult
88
+ * @property {boolean} verified - True if verified, false if not.
89
+ * @property {object} statusResult
90
+ * @property {Array} results
91
+ * @property {object} error
92
+ */
93
+
94
+ /**
95
+ * Issues a verifiable credential (by taking a base credential document,
96
+ * and adding a digital signature to it).
97
+ *
98
+ * @param {object} [options={}] - The options to use.
99
+ *
100
+ * @param {object} options.credential - Base credential document.
101
+ * @param {LinkedDataSignature} options.suite - Signature suite (with private
102
+ * key material), passed in to sign().
103
+ *
104
+ * @param {ProofPurpose} [options.purpose] - A ProofPurpose. If not specified,
105
+ * a default purpose will be created.
106
+ *
107
+ * Other optional params passed to `sign()`:
108
+ * @param {object} [options.documentLoader] - A document loader.
109
+ * @param {object} [options.expansionMap] - An expansion map.
110
+ *
111
+ * @throws {Error} If missing required properties.
112
+ *
113
+ * @returns {Promise<VerifiableCredential>} Resolves on completion.
114
+ */
115
+ export async function issue({
116
+ credential, suite, expansionMap,
117
+ purpose = new CredentialIssuancePurpose(),
118
+ documentLoader = defaultDocumentLoader
119
+ } = {}) {
120
+ // check to make sure the `suite` has required params
121
+ // Note: verificationMethod defaults to publicKey.id, in suite constructor
122
+ if(!suite) {
123
+ throw new TypeError('"suite" parameter is required for issuing.');
124
+ }
125
+ if(!suite.verificationMethod) {
126
+ throw new TypeError('"suite.verificationMethod" property is required.');
127
+ }
128
+
129
+ if(!credential) {
130
+ throw new TypeError('"credential" parameter is required for issuing.');
131
+ }
132
+
133
+ // Set the issuance date to now(), if missing
134
+ if(!credential.issuanceDate) {
135
+ const now = (new Date()).toJSON();
136
+ credential.issuanceDate = `${now.substr(0, now.length - 5)}Z`;
137
+ }
138
+
139
+ // run common credential checks
140
+ _checkCredential(credential);
141
+
142
+ return jsigs.sign(credential, {purpose, documentLoader, suite, expansionMap});
143
+ }
144
+
145
+ /**
146
+ * Verifies a verifiable presentation:
147
+ * - Checks that the presentation is well-formed
148
+ * - Checks the proofs (for example, checks digital signatures against the
149
+ * provided public keys).
150
+ *
151
+ * @param {object} [options={}] - The options to use.
152
+ *
153
+ * @param {VerifiablePresentation} options.presentation - Verifiable
154
+ * presentation, signed or unsigned, that may contain within it a
155
+ * verifiable credential.
156
+ *
157
+ * @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - One or
158
+ * more signature suites that are supported by the caller's use case. This is
159
+ * an explicit design decision -- the calling code must specify which
160
+ * signature types (ed25519, RSA, etc) are allowed.
161
+ * Although it is expected that the secure resolution/fetching of the public
162
+ * key material (to verify against) is to be handled by the documentLoader,
163
+ * the suite param can optionally include the key directly.
164
+ *
165
+ * @param {boolean} [options.unsignedPresentation=false] - By default, this
166
+ * function assumes that a presentation is signed (and will return an error if
167
+ * a `proof` section is missing). Set this to `true` if you're using an
168
+ * unsigned presentation.
169
+ *
170
+ * Either pass in a proof purpose,
171
+ * @param {AuthenticationProofPurpose} [options.presentationPurpose] - Optional
172
+ * proof purpose (a default one will be created if not passed in).
173
+ *
174
+ * or a default purpose will be created with params:
175
+ * @param {string} [options.challenge] - Required if purpose is not passed in.
176
+ * @param {string} [options.controller] - A controller.
177
+ * @param {string} [options.domain] - A domain.
178
+ *
179
+ * @param {Function} [options.documentLoader] - A document loader.
180
+ * @param {Function} [options.checkStatus] - Optional function for checking
181
+ * credential status if `credentialStatus` is present on the credential.
182
+ *
183
+ * @returns {Promise<VerifyPresentationResult>} The verification result.
184
+ */
185
+ export async function verify(options = {}) {
186
+ const {presentation} = options;
187
+ try {
188
+ if(!presentation) {
189
+ throw new TypeError(
190
+ 'A "presentation" property is required for verifying.');
191
+ }
192
+ return _verifyPresentation(options);
193
+ } catch(error) {
194
+ return {
195
+ verified: false,
196
+ results: [{presentation, verified: false, error}],
197
+ error
198
+ };
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Verifies a verifiable credential:
204
+ * - Checks that the credential is well-formed
205
+ * - Checks the proofs (for example, checks digital signatures against the
206
+ * provided public keys).
207
+ *
208
+ * @param {object} [options={}] - The options.
209
+ *
210
+ * @param {object} options.credential - Verifiable credential.
211
+ *
212
+ * @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - One or
213
+ * more signature suites that are supported by the caller's use case. This is
214
+ * an explicit design decision -- the calling code must specify which
215
+ * signature types (ed25519, RSA, etc) are allowed.
216
+ * Although it is expected that the secure resolution/fetching of the public
217
+ * key material (to verify against) is to be handled by the documentLoader,
218
+ * the suite param can optionally include the key directly.
219
+ *
220
+ * @param {CredentialIssuancePurpose} [options.purpose] - Optional
221
+ * proof purpose (a default one will be created if not passed in).
222
+ * @param {Function} [options.documentLoader] - A document loader.
223
+ * @param {Function} [options.checkStatus] - Optional function for checking
224
+ * credential status if `credentialStatus` is present on the credential.
225
+ *
226
+ * @returns {Promise<VerifyCredentialResult>} The verification result.
227
+ */
228
+ export async function verifyCredential(options = {}) {
229
+ const {credential} = options;
230
+ try {
231
+ if(!credential) {
232
+ throw new TypeError(
233
+ 'A "credential" property is required for verifying.');
234
+ }
235
+ return await _verifyCredential(options);
236
+ } catch(error) {
237
+ return {
238
+ verified: false,
239
+ results: [{credential, verified: false, error}],
240
+ error
241
+ };
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Verifies a verifiable credential.
247
+ *
248
+ * @private
249
+ * @param {object} [options={}] - The options.
250
+ *
251
+ * @param {object} options.credential - Verifiable credential.
252
+ * @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - See the
253
+ * definition in the `verify()` docstring, for this param.
254
+ *
255
+ * @throws {Error} If required parameters are missing (in `_checkCredential`).
256
+ *
257
+ * @param {CredentialIssuancePurpose} [options.purpose] - A purpose.
258
+ * @param {Function} [options.documentLoader] - A document loader.
259
+ * @param {Function} [options.checkStatus] - Optional function for checking
260
+ * credential status if `credentialStatus` is present on the credential.
261
+ *
262
+ * @returns {Promise<VerifyCredentialResult>} The verification result.
263
+ */
264
+ async function _verifyCredential(options = {}) {
265
+ const {credential, checkStatus} = options;
266
+
267
+ // run common credential checks
268
+ _checkCredential(credential);
269
+
270
+ // if credential status is provided, a `checkStatus` function must be given
271
+ if(credential.credentialStatus && typeof options.checkStatus !== 'function') {
272
+ throw new TypeError(
273
+ 'A "checkStatus" function must be given to verify credentials with ' +
274
+ '"credentialStatus".');
275
+ }
276
+
277
+ const documentLoader = options.documentLoader || defaultDocumentLoader;
278
+
279
+ const {controller} = options;
280
+ const purpose = options.purpose || new CredentialIssuancePurpose({
281
+ controller
282
+ });
283
+
284
+ const result = await jsigs.verify(
285
+ credential, {purpose, documentLoader, ...options});
286
+
287
+ // if verification has already failed, skip status check
288
+ if(!result.verified) {
289
+ return result;
290
+ }
291
+
292
+ if(credential.credentialStatus) {
293
+ result.statusResult = await checkStatus(options);
294
+ if(!result.statusResult.verified) {
295
+ result.verified = false;
296
+ }
297
+ }
298
+
299
+ return result;
300
+ }
301
+
302
+ /**
303
+ * Creates an unsigned presentation from a given verifiable credential.
304
+ *
305
+ * @param {object} options - Options to use.
306
+ * @param {object|Array<object>} [options.verifiableCredential] - One or more
307
+ * verifiable credential.
308
+ * @param {string} [options.id] - Optional VP id.
309
+ * @param {string} [options.holder] - Optional presentation holder url.
310
+ *
311
+ * @throws {TypeError} If verifiableCredential param is missing.
312
+ * @throws {Error} If the credential (or the presentation params) are missing
313
+ * required properties.
314
+ *
315
+ * @returns {Presentation} The credential wrapped inside of a
316
+ * VerifiablePresentation.
317
+ */
318
+ export function createPresentation({verifiableCredential, id, holder} = {}) {
319
+ const presentation = {
320
+ '@context': [CREDENTIALS_CONTEXT_V1_URL],
321
+ type: ['VerifiablePresentation']
322
+ };
323
+ if(verifiableCredential) {
324
+ const credentials = [].concat(verifiableCredential);
325
+ // ensure all credentials are valid
326
+ for(const credential of credentials) {
327
+ _checkCredential(credential);
328
+ }
329
+ presentation.verifiableCredential = credentials;
330
+ }
331
+ if(id) {
332
+ presentation.id = id;
333
+ }
334
+ if(holder) {
335
+ presentation.holder = holder;
336
+ }
337
+
338
+ _checkPresentation(presentation);
339
+
340
+ return presentation;
341
+ }
342
+
343
+ /**
344
+ * Signs a given presentation.
345
+ *
346
+ * @param {object} [options={}] - Options to use.
347
+ *
348
+ * Required:
349
+ * @param {Presentation} options.presentation - A presentation.
350
+ * @param {LinkedDataSignature} options.suite - passed in to sign()
351
+ *
352
+ * Either pass in a ProofPurpose, or a default one will be created with params:
353
+ * @param {ProofPurpose} [options.purpose] - A ProofPurpose. If not specified,
354
+ * a default purpose will be created with the domain and challenge options.
355
+ *
356
+ * @param {string} [options.domain] - A domain.
357
+ * @param {string} options.challenge - A required challenge.
358
+ *
359
+ * @param {Function} [options.documentLoader] - A document loader.
360
+ *
361
+ * @returns {Promise<{VerifiablePresentation}>} A VerifiablePresentation with
362
+ * a proof.
363
+ */
364
+ export async function signPresentation(options = {}) {
365
+ const {presentation, domain, challenge} = options;
366
+ const purpose = options.purpose || new AuthenticationProofPurpose({
367
+ domain,
368
+ challenge
369
+ });
370
+
371
+ const documentLoader = options.documentLoader || defaultDocumentLoader;
372
+
373
+ return jsigs.sign(presentation, {purpose, documentLoader, ...options});
374
+ }
375
+
376
+ /**
377
+ * Verifies that the VerifiablePresentation is well formed, and checks the
378
+ * proof signature if it's present. Also verifies all the VerifiableCredentials
379
+ * that are present in the presentation, if any.
380
+ *
381
+ * @param {object} [options={}] - The options.
382
+ * @param {VerifiablePresentation} options.presentation - A
383
+ * VerifiablePresentation.
384
+ *
385
+ * @param {LinkedDataSignature|LinkedDataSignature[]} options.suite - See the
386
+ * definition in the `verify()` docstring, for this param.
387
+ *
388
+ * @param {boolean} [options.unsignedPresentation=false] - By default, this
389
+ * function assumes that a presentation is signed (and will return an error if
390
+ * a `proof` section is missing). Set this to `true` if you're using an
391
+ * unsigned presentation.
392
+ *
393
+ * Either pass in a proof purpose,
394
+ * @param {AuthenticationProofPurpose} [options.presentationPurpose] - A
395
+ * ProofPurpose. If not specified, a default purpose will be created with
396
+ * the challenge, controller, and domain options.
397
+ *
398
+ * @param {string} [options.challenge] - A challenge. Required if purpose is
399
+ * not passed in.
400
+ * @param {string} [options.controller] - A controller. Required if purpose is
401
+ * not passed in.
402
+ * @param {string} [options.domain] - A domain. Required if purpose is not
403
+ * passed in.
404
+ *
405
+ * @param {Function} [options.documentLoader] - A document loader.
406
+ * @param {Function} [options.checkStatus] - Optional function for checking
407
+ * credential status if `credentialStatus` is present on the credential.
408
+ *
409
+ * @throws {Error} If presentation is missing required params.
410
+ *
411
+ * @returns {Promise<VerifyPresentationResult>} The verification result.
412
+ */
413
+ async function _verifyPresentation(options = {}) {
414
+ const {presentation, unsignedPresentation} = options;
415
+
416
+ _checkPresentation(presentation);
417
+
418
+ const documentLoader = options.documentLoader || defaultDocumentLoader;
419
+
420
+ // FIXME: verify presentation first, then each individual credential
421
+ // only if that proof is verified
422
+
423
+ // if verifiableCredentials are present, verify them, individually
424
+ let credentialResults;
425
+ let verified = true;
426
+ const credentials = jsonld.getValues(presentation, 'verifiableCredential');
427
+ if(credentials.length > 0) {
428
+ // verify every credential in `verifiableCredential`
429
+ credentialResults = await Promise.all(credentials.map(credential => {
430
+ return verifyCredential({credential, documentLoader, ...options});
431
+ }));
432
+
433
+ for(const [i, credentialResult] of credentialResults.entries()) {
434
+ credentialResult.credentialId = credentials[i].id;
435
+ }
436
+
437
+ const allCredentialsVerified = credentialResults.every(r => r.verified);
438
+ if(!allCredentialsVerified) {
439
+ verified = false;
440
+ }
441
+ }
442
+
443
+ if(unsignedPresentation) {
444
+ // No need to verify the proof section of this presentation
445
+ return {verified, results: [presentation], credentialResults};
446
+ }
447
+
448
+ const {controller, domain, challenge} = options;
449
+ if(!options.presentationPurpose && !challenge) {
450
+ throw new Error(
451
+ 'A "challenge" param is required for AuthenticationProofPurpose.');
452
+ }
453
+
454
+ const purpose = options.presentationPurpose ||
455
+ new AuthenticationProofPurpose({controller, domain, challenge});
456
+
457
+ const presentationResult = await jsigs.verify(
458
+ presentation, {purpose, documentLoader, ...options});
459
+
460
+ return {
461
+ presentationResult,
462
+ verified: verified && presentationResult.verified,
463
+ credentialResults,
464
+ error: presentationResult.error
465
+ };
466
+ }
467
+
468
+ /**
469
+ * @param {string|object} obj - Either an object with an id property
470
+ * or a string that is an id.
471
+ * @returns {string|undefined} Either an id or undefined.
472
+ * @private
473
+ *
474
+ */
475
+ function _getId(obj) {
476
+ if(typeof obj === 'string') {
477
+ return obj;
478
+ }
479
+
480
+ if(!('id' in obj)) {
481
+ return;
482
+ }
483
+
484
+ return obj.id;
485
+ }
486
+
487
+ // export for testing
488
+ /**
489
+ * @param {object} presentation - An object that could be a presentation.
490
+ * @throws {Error}
491
+ * @private
492
+ */
493
+ export function _checkPresentation(presentation) {
494
+ // normalize to an array to allow the common case of context being a string
495
+ const context = Array.isArray(presentation['@context']) ?
496
+ presentation['@context'] : [presentation['@context']];
497
+
498
+ // ensure first context is 'https://www.w3.org/2018/credentials/v1'
499
+ if(context[0] !== CREDENTIALS_CONTEXT_V1_URL) {
500
+ throw new Error(
501
+ `"${CREDENTIALS_CONTEXT_V1_URL}" needs to be first in the ` +
502
+ 'list of contexts.');
503
+ }
504
+
505
+ const types = jsonld.getValues(presentation, 'type');
506
+
507
+ // check type presence
508
+ if(!types.includes('VerifiablePresentation')) {
509
+ throw new Error('"type" must include "VerifiablePresentation".');
510
+ }
511
+ }
512
+
513
+ // export for testing
514
+ /**
515
+ * @param {object} credential - An object that could be a VerifiableCredential.
516
+ * @throws {Error}
517
+ * @private
7
518
  */
8
- module.exports = require('./vc.js');
519
+ export function _checkCredential(credential) {
520
+ // ensure first context is 'https://www.w3.org/2018/credentials/v1'
521
+ if(credential['@context'][0] !== CREDENTIALS_CONTEXT_V1_URL) {
522
+ throw new Error(
523
+ `"${CREDENTIALS_CONTEXT_V1_URL}" needs to be first in the ` +
524
+ 'list of contexts.');
525
+ }
526
+
527
+ // check type presence and cardinality
528
+ if(!credential.type) {
529
+ throw new Error('"type" property is required.');
530
+ }
531
+
532
+ if(!jsonld.getValues(credential, 'type').includes('VerifiableCredential')) {
533
+ throw new Error('"type" must include `VerifiableCredential`.');
534
+ }
535
+
536
+ if(!credential.credentialSubject) {
537
+ throw new Error('"credentialSubject" property is required.');
538
+ }
539
+
540
+ // If credentialSubject.id is present and is not a URI, reject it
541
+ if(credential.credentialSubject.id) {
542
+ _validateUriId({
543
+ id: credential.credentialSubject.id, propertyName: 'credentialSubject.id'
544
+ });
545
+ }
546
+
547
+ if(!credential.issuer) {
548
+ throw new Error('"issuer" property is required.');
549
+ }
550
+
551
+ // check issuanceDate cardinality
552
+ if(jsonld.getValues(credential, 'issuanceDate').length > 1) {
553
+ throw new Error('"issuanceDate" property can only have one value.');
554
+ }
555
+
556
+ // check issued is a date
557
+ if(!credential.issuanceDate) {
558
+ throw new Error('"issuanceDate" property is required.');
559
+ }
560
+
561
+ if('issuanceDate' in credential) {
562
+ if(!dateRegex.test(credential.issuanceDate)) {
563
+ throw new Error(
564
+ `"issuanceDate" must be a valid date: ${credential.issuanceDate}`);
565
+ }
566
+ }
567
+
568
+ // check issuer cardinality
569
+ if(jsonld.getValues(credential, 'issuer').length > 1) {
570
+ throw new Error('"issuer" property can only have one value.');
571
+ }
572
+
573
+ // check issuer is a URL
574
+ if('issuer' in credential) {
575
+ const issuer = _getId(credential.issuer);
576
+ if(!issuer) {
577
+ throw new Error(`"issuer" id is required.`);
578
+ }
579
+ _validateUriId({id: issuer, propertyName: 'issuer'});
580
+ }
581
+
582
+ if('credentialStatus' in credential) {
583
+ if(!credential.credentialStatus.id) {
584
+ throw new Error('"credentialStatus" must include an id.');
585
+ }
586
+ if(!credential.credentialStatus.type) {
587
+ throw new Error('"credentialStatus" must include a type.');
588
+ }
589
+ }
590
+
591
+ // check evidences are URLs
592
+ jsonld.getValues(credential, 'evidence').forEach(evidence => {
593
+ const evidenceId = _getId(evidence);
594
+ if(evidenceId) {
595
+ _validateUriId({id: evidenceId, propertyName: 'evidence'});
596
+ }
597
+ });
598
+
599
+ // check expires is a date
600
+ if('expirationDate' in credential &&
601
+ !dateRegex.test(credential.expirationDate)) {
602
+ throw new Error(
603
+ `"expirationDate" must be a valid date: ${credential.expirationDate}`);
604
+ }
605
+ }
606
+
607
+ function _validateUriId({id, propertyName}) {
608
+ let parsed;
609
+ try {
610
+ parsed = new URL(id);
611
+ } catch(e) {
612
+ const error = new TypeError(`"${propertyName}" must be a URI: "${id}".`);
613
+ error.cause = e;
614
+ throw error;
615
+ }
616
+
617
+ if(!parsed.protocol) {
618
+ throw new TypeError(`"${propertyName}" must be a URI: "${id}".`);
619
+ }
620
+ }