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