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