-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-verify.spec.js
956 lines (877 loc) · 30.4 KB
/
10-verify.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
/*!
* Copyright (c) 2019-2024 Digital Bazaar, Inc. All rights reserved.
*/
import chai from 'chai';
const should = chai.should();
import * as bbs2023Cryptosuite from '@digitalbazaar/bbs-2023-cryptosuite';
import * as Bls12381Multikey from '@digitalbazaar/bls12-381-multikey';
import * as EcdsaMultikey from '@digitalbazaar/ecdsa-multikey';
import * as ecdsaSd2023Cryptosuite from
'@digitalbazaar/ecdsa-sd-2023-cryptosuite';
import * as vc from '../lib/index.js';
import {
CONTEXT_V1 as odrlCtx,
CONTEXT_URL_V1 as odrlCtxUrl
} from '@digitalbazaar/odrl-context';
import {
CONTEXT_V1 as vcExamplesV1Ctx,
CONTEXT_URL_V1 as vcExamplesV1CtxUrl
} from '@digitalbazaar/credentials-examples-context';
import {assertionController} from './mocks/assertionController.js';
import {CredentialIssuancePurpose} from '../lib/CredentialIssuancePurpose.js';
import dataIntegrityContext from '@digitalbazaar/data-integrity-context';
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {Ed25519Signature2018} from '@digitalbazaar/ed25519-signature-2018';
import {
Ed25519VerificationKey2018
} from '@digitalbazaar/ed25519-verification-key-2018';
import {invalidContexts} from './contexts/index.js';
import jsigs from 'jsonld-signatures';
import jsonld from 'jsonld';
import {credential as mockCredential} from './mocks/credential.js';
import {mock as mockData} from './mocks/mock.data.js';
import multikeyContext from '@digitalbazaar/multikey-context';
import {MultiLoader} from './MultiLoader.js';
import {v4 as uuid} from 'uuid';
import {VeresOneDriver} from 'did-veres-one';
const remoteDocuments = new Map();
remoteDocuments.set(vcExamplesV1CtxUrl, vcExamplesV1Ctx);
remoteDocuments.set(odrlCtxUrl, odrlCtx);
remoteDocuments.set(
dataIntegrityContext.constants.CONTEXT_URL,
dataIntegrityContext.contexts.get(
dataIntegrityContext.constants.CONTEXT_URL));
remoteDocuments.set(
multikeyContext.constants.CONTEXT_URL,
multikeyContext.contexts.get(
multikeyContext.constants.CONTEXT_URL));
for(const key in invalidContexts) {
const {url, value} = invalidContexts[key];
remoteDocuments.set(url, value);
}
const {extendContextLoader} = jsigs;
const {defaultDocumentLoader} = vc;
const testContextLoader = extendContextLoader(async url => {
const remoteDocument = remoteDocuments.get(url);
if(remoteDocument) {
return {
contextUrl: null,
document: jsonld.clone(remoteDocument),
documentUrl: url
};
}
return defaultDocumentLoader(url);
});
// documents are added to this documentLoader incrementally
const testLoader = new MultiLoader({
documentLoader: [
// CREDENTIALS_CONTEXT_URL
testContextLoader
]
});
const documentLoader = testLoader.documentLoader.bind(testLoader);
// do ed25519 setup...
let suite;
let keyPair;
before(async () => {
// set up the Ed25519 key pair that will be signing and verifying
keyPair = await Ed25519VerificationKey2018.generate({
id: 'https://example.edu/issuers/keys/1',
controller: 'https://example.edu/issuers/565049'
});
// add the key to the controller doc (authorizes its use for assertion)
assertionController.assertionMethod.push(keyPair.id);
// also add the key for authentication (VP) purposes
// FIXME: this shortcut to reuse the same key and sign VPs as issuer can
// confuse developers trying to learn from the test suite and it should
// be changed
assertionController.authentication.push(keyPair.id);
// register the controller document and the key document with documentLoader
remoteDocuments.set(
'https://example.edu/issuers/565049', assertionController);
remoteDocuments.set(
'https://example.edu/issuers/keys/1',
await keyPair.export({publicKey: true}));
// set up the signature suite, using the generated key
suite = new Ed25519Signature2018({
verificationMethod: 'https://example.edu/issuers/keys/1',
key: keyPair
});
});
// do ecdsa setup...
let ecdsaKeyPair;
before(async () => {
// set up the ECDSA key pair that will be signing and verifying
ecdsaKeyPair = await EcdsaMultikey.generate({
curve: 'P-256',
id: 'https://example.edu/issuers/keys/2',
controller: 'https://example.edu/issuers/565049'
});
// add the key to the controller doc (authorizes its use for assertion)
assertionController.assertionMethod.push(ecdsaKeyPair.id);
// register the key document with documentLoader
remoteDocuments.set(
'https://example.edu/issuers/keys/2',
await ecdsaKeyPair.export({publicKey: true}));
});
// do BBS setup...
let bbsKeyPair;
before(async () => {
// set up the BBS key pair that will be signing and verifying
bbsKeyPair = await Bls12381Multikey.generateBbsKeyPair({
algorithm: 'BBS-BLS12-381-SHA-256',
id: 'https://example.edu/issuers/keys/3',
controller: 'https://example.edu/issuers/565049'
});
// add the key to the controller doc (authorizes its use for assertion)
assertionController.assertionMethod.push(bbsKeyPair.id);
// register the key document with documentLoader
remoteDocuments.set(
'https://example.edu/issuers/keys/3',
await bbsKeyPair.export({publicKey: true}));
});
describe('vc.issue()', () => {
it('should issue a verifiable credential with proof', async () => {
const credential = jsonld.clone(mockCredential);
const verifiableCredential = await vc.issue({
credential,
suite,
documentLoader
});
verifiableCredential.should.exist;
verifiableCredential.should.be.an('object');
verifiableCredential.should.have.property('proof');
verifiableCredential.proof.should.be.an('object');
});
it('should issue an expired verifiable credential', async () => {
const keyPair = await Ed25519VerificationKey2018.generate();
const fp = Ed25519VerificationKey2018
.fingerprintFromPublicKey({publicKeyBase58: keyPair.publicKeyBase58});
keyPair.id = `did:key:${fp}#${fp}`;
const credential = jsonld.clone(mockCredential);
credential.id = `urn:uuid:${uuid()}`;
credential.issuer = `did:key:${fp}`;
credential.expirationDate = '2020-05-31T19:21:25Z';
const verifiableCredential = await vc.issue({
credential,
suite: new Ed25519Signature2018({
key: keyPair
}),
// set `now` to expiration date, allowing the credential to be issued
// without failing the expired check
now: (new Date('2020-05-31T19:21:25Z')),
documentLoader
});
verifiableCredential.should.exist;
verifiableCredential.should.be.an('object');
verifiableCredential.should.have.property('proof');
verifiableCredential.proof.should.be.an('object');
});
it('should throw an error on missing verificationMethod', async () => {
const suite = new Ed25519Signature2018({
// Note no key id or verificationMethod passed to suite
key: await Ed25519VerificationKey2018.generate()
});
let error;
try {
await vc.issue({
credential: mockCredential,
suite
});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "verificationMethod" property missing');
error.should.be.instanceof(TypeError);
error.message.should
.contain('"suite.verificationMethod" property is required.');
});
it('should add "issuanceDate" to verifiable credentials', async () => {
const credential = jsonld.clone(mockCredential);
delete credential.issuanceDate;
const now = new Date();
const expectedIssuanceDate = `${now.toISOString().slice(0, -5)}Z`;
const verifiableCredential = await vc.issue({
credential,
suite,
documentLoader,
now
});
verifiableCredential.should.exist;
verifiableCredential.should.be.an('object');
verifiableCredential.should.have.property('proof');
verifiableCredential.proof.should.be.an('object');
verifiableCredential.should.have.property(
'issuanceDate',
expectedIssuanceDate
);
});
});
describe('vc.createPresentation()', () => {
it('should create an unsigned presentation', () => {
const presentation = vc.createPresentation({
verifiableCredential: mockCredential,
id: 'test:ebc6f1c2',
holder: 'did:ex:holder123'
});
presentation.type.should.eql(['VerifiablePresentation']);
presentation.should.have.property('verifiableCredential');
presentation.should.have.property('id', 'test:ebc6f1c2');
presentation.should.have.property('holder', 'did:ex:holder123');
presentation.should.not.have.property('proof');
});
});
describe('vc.signPresentation()', () => {
it('should create a signed VP', async () => {
const presentation = vc.createPresentation({
verifiableCredential: mockCredential,
id: 'test:ebc6f1c2',
holder: 'did:ex:holder123'
});
const vp = await vc.signPresentation({
presentation,
suite, // from before() block
challenge: '12ec21',
documentLoader
});
vp.should.have.property('proof');
vp.proof.should.have.property('type', 'Ed25519Signature2018');
vp.proof.should.have.property('proofPurpose', 'authentication');
vp.proof.should.have.property('verificationMethod',
'https://example.edu/issuers/keys/1');
vp.proof.should.have.property('challenge', '12ec21');
vp.proof.should.have.property('created');
vp.proof.should.have.property('jws');
});
});
describe('verify API (credentials)', () => {
it('should verify a vc', async () => {
const verifiableCredential = await vc.issue({
credential: mockCredential,
suite,
documentLoader
});
const result = await vc.verifyCredential({
credential: verifiableCredential,
controller: assertionController,
suite,
documentLoader
});
if(result.error) {
throw result.error;
}
result.verified.should.be.true;
});
it('should verify an ECDSA-SD derived vc', async () => {
const {
createDiscloseCryptosuite,
createSignCryptosuite,
createVerifyCryptosuite
} = ecdsaSd2023Cryptosuite;
const proofId = `urn:uuid:${uuid()}`;
// setup ecdsa-sd-2023 suite for signing selective disclosure VCs
const ecdsaSdSignSuite = new DataIntegrityProof({
signer: ecdsaKeyPair.signer(), cryptosuite: createSignCryptosuite({
mandatoryPointers: [
'/issuanceDate',
'/issuer'
]
})
});
ecdsaSdSignSuite.proof = {id: proofId};
// setup ecdsa-sd-2023 suite for deriving selective disclosure VCs
const ecdsaSdDeriveSuite = new DataIntegrityProof({
cryptosuite: createDiscloseCryptosuite({
proofId,
selectivePointers: [
'/credentialSubject'
]
})
});
// setup ecdsa-sd-2023 suite for verifying selective disclosure VCs
const ecdsaSdVerifySuite = new DataIntegrityProof({
cryptosuite: createVerifyCryptosuite()
});
const verifiableCredential = await vc.issue({
credential: {...mockCredential},
suite: ecdsaSdSignSuite,
documentLoader
});
const derivedCredential = await vc.derive({
verifiableCredential,
suite: ecdsaSdDeriveSuite,
documentLoader
});
const result = await vc.verifyCredential({
credential: derivedCredential,
controller: assertionController,
suite: ecdsaSdVerifySuite,
documentLoader
});
if(result.error) {
throw result.error;
}
result.verified.should.be.true;
});
it('should verify a BBS derived vc', async () => {
const {
createDiscloseCryptosuite,
createSignCryptosuite,
createVerifyCryptosuite
} = bbs2023Cryptosuite;
// setup bbs-2023 suite for signing unlinkable VCs
const bbsSignSuite = new DataIntegrityProof({
signer: bbsKeyPair.signer(), cryptosuite: createSignCryptosuite({
mandatoryPointers: [
'/issuanceDate',
'/issuer'
]
})
});
// setup bbs-2023 suite for deriving unlinkable VC proofs
const bbsDeriveSuite = new DataIntegrityProof({
cryptosuite: createDiscloseCryptosuite({
selectivePointers: [
'/credentialSubject'
]
})
});
// setup bbs-2023 suite for verifying unlinkable VC proofs
const bbsVerifySuite = new DataIntegrityProof({
cryptosuite: createVerifyCryptosuite()
});
const credential = {...mockCredential};
delete credential.id;
delete credential.credentialSubject.id;
credential.issuanceDate = '2010-01-01T01:00:00Z';
const verifiableCredential = await vc.issue({
credential,
suite: bbsSignSuite,
documentLoader
});
const derivedCredential = await vc.derive({
verifiableCredential,
suite: bbsDeriveSuite,
documentLoader
});
const result = await vc.verifyCredential({
credential: derivedCredential,
controller: assertionController,
suite: bbsVerifySuite,
documentLoader
});
if(result.error) {
throw result.error;
}
result.verified.should.be.true;
});
it('should verify a vc with a positive status check', async () => {
const credential = jsonld.clone(mockCredential);
credential['@context'].push({
'@context': {
id: '@id',
type: '@type',
TestStatusList: {
'@id': 'https://example.edu/TestStatusList',
'@type': '@id'
}
}
});
credential.credentialStatus = {
id: 'https://example.edu/status/24',
type: 'TestStatusList'
};
const verifiableCredential = await vc.issue({
credential,
suite,
documentLoader
});
const result = await vc.verifyCredential({
credential: verifiableCredential,
controller: assertionController,
suite,
documentLoader,
checkStatus: async () => ({verified: true})
});
if(result.error) {
throw result.error;
}
result.verified.should.be.true;
});
describe('negative test', async () => {
it('fails to verify if a context resolves to null', async () => {
const {credential, suite} = await _generateCredential();
credential['@context'].push(invalidContexts.nullDoc.url);
const results = await vc.verifyCredential({
suite,
credential,
documentLoader
});
results.verified.should.be.a('boolean');
results.verified.should.be.false;
});
it('fails to verify if a context contains an invalid id', async () => {
const {credential, suite} = await _generateCredential();
credential['@context'].push(invalidContexts.invalidId.url);
const results = await vc.verifyCredential({
suite,
credential,
documentLoader
});
results.verified.should.be.a('boolean');
results.verified.should.be.false;
});
it('fails to verify if a context has a null version', async () => {
const {credential, suite} = await _generateCredential();
credential['@context'].push(invalidContexts.nullVersion.url);
const results = await vc.verifyCredential({
suite,
credential,
documentLoader
});
results.verified.should.be.a('boolean');
results.verified.should.be.false;
});
it('fails to verify if a context has a null @id', async () => {
const {credential, suite} = await _generateCredential();
credential['@context'].push(invalidContexts.nullId.url);
const results = await vc.verifyCredential({
suite,
credential,
documentLoader
});
results.verified.should.be.a('boolean');
results.verified.should.be.false;
});
it('fails to verify if a context has a null @type', async () => {
const {credential, suite} = await _generateCredential();
credential['@context'].push(invalidContexts.nullType.url);
const results = await vc.verifyCredential({
suite,
credential,
documentLoader
});
results.verified.should.be.a('boolean');
results.verified.should.be.false;
});
it('fails to verify if a context links to a missing doc', async () => {
const {credential, suite} = await _generateCredential();
credential['@context'].push('https://fsad.digitalbazaar.com');
const results = await vc.verifyCredential({
suite,
credential,
documentLoader
});
results.verified.should.be.a('boolean');
results.verified.should.be.false;
});
it('fails to verify if a context has an invalid url', async () => {
const {credential, suite} = await _generateCredential();
credential['@context'].push('htps://fsad.digitalbazaar.');
const results = await vc.verifyCredential({
suite,
credential,
documentLoader
});
results.verified.should.be.a('boolean');
results.verified.should.be.false;
});
it('should fail to verify a vc with a negative status check', async () => {
const credential = jsonld.clone(mockCredential);
credential['@context'].push({
'@context': {
id: '@id',
type: '@type',
TestStatusList: {
'@id': 'https://example.edu/TestStatusList',
'@type': '@id'
}
}
});
credential.credentialStatus = {
id: 'https://example.edu/status/24',
type: 'TestStatusList'
};
const verifiableCredential = await vc.issue({
credential,
suite,
documentLoader
});
const result = await vc.verifyCredential({
credential: verifiableCredential,
controller: assertionController,
suite,
documentLoader,
checkStatus: async () => ({verified: false})
});
if(result.error) {
throw result.error;
}
result.verified.should.be.false;
});
it('should not run "checkStatus" on a vc without a ' +
'"credentialStatus" property', async () => {
const credential = jsonld.clone(mockCredential);
const verifiableCredential = await vc.issue({
credential,
suite,
documentLoader
});
const result = await vc.verifyCredential({
credential: verifiableCredential,
controller: assertionController,
suite,
documentLoader,
// ensure any checkStatus call will fail verification
checkStatus: async () => ({verified: false})
});
if(result.error) {
throw result.error;
}
result.verified.should.be.true;
});
it('should fail to verify a changed ECDSA-SD derived vc', async () => {
const {
createDiscloseCryptosuite,
createSignCryptosuite,
createVerifyCryptosuite
} = ecdsaSd2023Cryptosuite;
const proofId = `urn:uuid:${uuid()}`;
// setup ecdsa-sd-2023 suite for signing selective disclosure VCs
const ecdsaSdSignSuite = new DataIntegrityProof({
signer: ecdsaKeyPair.signer(), cryptosuite: createSignCryptosuite({
mandatoryPointers: [
'/issuanceDate',
'/issuer'
]
})
});
ecdsaSdSignSuite.proof = {id: proofId};
// setup ecdsa-sd-2023 suite for deriving selective disclosure VCs
const ecdsaSdDeriveSuite = new DataIntegrityProof({
cryptosuite: createDiscloseCryptosuite({
proofId,
selectivePointers: [
'/credentialSubject'
]
})
});
// setup ecdsa-sd-2023 suite for verifying selective disclosure VCs
const ecdsaSdVerifySuite = new DataIntegrityProof({
cryptosuite: createVerifyCryptosuite()
});
const verifiableCredential = await vc.issue({
credential: {...mockCredential},
suite: ecdsaSdSignSuite,
documentLoader
});
const derivedCredential = await vc.derive({
verifiableCredential,
suite: ecdsaSdDeriveSuite,
documentLoader
});
derivedCredential.credentialSubject.id = `urn:uuid:${uuid()}`;
const result = await vc.verifyCredential({
credential: derivedCredential,
controller: assertionController,
suite: ecdsaSdVerifySuite,
documentLoader
});
result.verified.should.be.a('boolean');
result.verified.should.be.false;
});
it('should fail to verify a changed BBS derived vc', async () => {
const {
createDiscloseCryptosuite,
createSignCryptosuite,
createVerifyCryptosuite
} = bbs2023Cryptosuite;
// setup bbs-2023 suite for signing unlinkable VCs
const bbsSignSuite = new DataIntegrityProof({
signer: bbsKeyPair.signer(), cryptosuite: createSignCryptosuite({
mandatoryPointers: [
'/issuanceDate',
'/issuer'
]
})
});
// setup bbs-2023 suite for deriving unlinkable VC proofs
const bbsDeriveSuite = new DataIntegrityProof({
cryptosuite: createDiscloseCryptosuite({
selectivePointers: [
'/credentialSubject'
]
})
});
// setup bbs-2023 suite for verifying unlinkable VC proofs
const bbsVerifySuite = new DataIntegrityProof({
cryptosuite: createVerifyCryptosuite()
});
const verifiableCredential = await vc.issue({
credential: {...mockCredential},
suite: bbsSignSuite,
documentLoader
});
const derivedCredential = await vc.derive({
verifiableCredential,
suite: bbsDeriveSuite,
documentLoader
});
derivedCredential.credentialSubject.id = `urn:uuid:${uuid()}`;
const result = await vc.verifyCredential({
credential: derivedCredential,
controller: assertionController,
suite: bbsVerifySuite,
documentLoader
});
result.verified.should.be.a('boolean');
result.verified.should.be.false;
});
});
});
describe('verify API (presentations)', () => {
it('verifies a valid signed presentation', async () => {
const challenge = uuid();
const {presentation, suite, documentLoader} =
await _generatePresentation({challenge});
const result = await vc.verify({
challenge,
suite,
documentLoader,
presentation
});
if(result.error) {
const firstError = [].concat(result.error)[0];
throw firstError;
}
result.verified.should.be.a('boolean');
result.verified.should.be.true;
});
it('verifies an unsigned presentation', async () => {
const {presentation, suite: vcSuite, documentLoader} =
await _generatePresentation({unsigned: true});
const result = await vc.verify({
documentLoader,
presentation,
suite: vcSuite,
unsignedPresentation: true
});
if(result.error) {
const firstError = [].concat(result.error)[0];
throw firstError;
}
result.verified.should.be.a('boolean');
result.verified.should.be.true;
});
});
describe('test for multiple credentials', async () => {
const credentialsCount = [5, 25, 50, 100];
for(const count of credentialsCount) {
it(`cause error when credentials are tampered [${count}]`, async () => {
const challenge = uuid();
const {presentation, suite: vcSuite, documentLoader} =
await _generatePresentation({challenge, credentialsCount: count});
// tampering with the first two credentials id
presentation.verifiableCredential[0].id = 'test:some_fake_id';
presentation.verifiableCredential[1].id = 'test:some_other_fake_id';
const result = await vc.verify({
documentLoader,
presentation,
suite: vcSuite,
unsignedPresentation: true
});
const credentialResults = result.credentialResults;
const credentialOne = result.credentialResults[0];
const credentialTwo = result.credentialResults[1];
const firstErrorMsg = result.credentialResults[0].error.errors[0].message;
result.verified.should.be.a('boolean');
result.verified.should.be.false;
credentialOne.verified.should.be.a('boolean');
credentialOne.verified.should.be.false;
credentialOne.credentialId.should.be.a('string');
credentialOne.credentialId.should.equal('test:some_fake_id');
credentialTwo.verified.should.be.a('boolean');
credentialTwo.verified.should.be.false;
credentialTwo.credentialId.should.be.a('string');
credentialTwo.credentialId.should.equal('test:some_other_fake_id');
for(let i = 2; i < credentialResults.length; ++i) {
const credential = credentialResults[i];
credential.verified.should.be.a('boolean');
credential.verified.should.be.true;
should.exist(credential.credentialId);
credential.credentialId.should.be.a('string');
}
firstErrorMsg.should.contain('Invalid signature.');
});
it('should not cause error when credentials are correct', async () => {
const challenge = uuid();
const {presentation, suite: vcSuite, documentLoader} =
await _generatePresentation({challenge, credentialsCount: count});
const result = await vc.verify({
documentLoader,
presentation,
suite: vcSuite,
unsignedPresentation: true
});
const credentialResults = result.credentialResults;
result.verified.should.be.a('boolean');
result.verified.should.be.true;
for(const credential of credentialResults) {
credential.verified.should.be.a('boolean');
credential.verified.should.be.true;
should.exist(credential.credentialId);
credential.credentialId.should.be.a('string');
}
});
}
});
describe('_checkCredential', () => {
it('should reject a credentialSubject.id that is not a URI', () => {
const credential = jsonld.clone(mockData.credentials.alpha);
credential.issuer = 'http://example.edu/credentials/58473';
credential.credentialSubject.id = '12345';
let error;
try {
vc._checkCredential({credential});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "credentialSubject.id" is not a URI');
error.should.be.instanceof(TypeError);
error.message.should
.contain('"credentialSubject.id" must be a URI');
});
it('should reject an issuer that is not a URI', () => {
const credential = jsonld.clone(mockData.credentials.alpha);
credential.issuer = '12345';
let error;
try {
vc._checkCredential({credential});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "credentialSubject.id" is not a URI');
error.should.be.instanceof(TypeError);
error.message.should
.contain('"issuer" must be a URI');
});
it('should reject an evidence id that is not a URI', () => {
const credential = jsonld.clone(mockData.credentials.alpha);
credential.issuer = 'did:example:12345';
credential.evidence = '12345';
let error;
try {
vc._checkCredential({credential});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "evidence" is not a URI');
error.should.be.instanceof(TypeError);
error.message.should
.contain('"evidence" must be a URI');
});
it('should reject if "expirationDate" has passed', () => {
const credential = jsonld.clone(mockData.credentials.alpha);
credential.issuer = 'did:example:12345';
// set expirationDate to an expired date.
credential.expirationDate = '2020-05-31T19:21:25Z';
let error;
try {
vc._checkCredential({credential});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "expirationDate" has passed');
error.message.should
.contain('Credential has expired.');
});
it('should reject if "now" is before "issuanceDate"', () => {
const credential = jsonld.clone(mockData.credentials.alpha);
credential.issuer = 'did:example:12345';
credential.issuanceDate = '2022-10-31T19:21:25Z';
const now = '2022-06-30T19:21:25Z';
let error;
try {
vc._checkCredential({credential, now});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "now" is before "issuanceDate"');
error.message.should.contain(
'The current date time (2022-06-30T19:21:25.000Z) is before the ' +
'"issuanceDate" (2022-10-31T19:21:25.000Z).');
});
});
async function _generateCredential() {
const mockCredential = jsonld.clone(mockData.credentials.alpha);
const {didDocument, documentLoader} = await _loadDid();
mockCredential.issuer = didDocument.didDocument.id;
mockCredential.id = `http://example.edu/credentials/${uuid()}`;
testLoader.addLoader(documentLoader);
const assertionKey = didDocument.methodFor({purpose: 'assertionMethod'});
const suite = new Ed25519Signature2018({key: assertionKey});
const credential = await jsigs.sign(mockCredential, {
compactProof: false,
documentLoader: testLoader.documentLoader.bind(testLoader),
suite,
purpose: new CredentialIssuancePurpose()
});
return {credential, documentLoader, suite};
}
async function _generatePresentation({
challenge, unsigned = false, credentialsCount = 1
}) {
const {didDocument, documentLoader: didLoader} = await _loadDid();
testLoader.addLoader(didLoader);
const credentials = [];
// generate multiple credentials
for(let i = 0; i < credentialsCount; i++) {
const {credential} = await _generateCredential();
credentials.push(credential);
}
const {documentLoader: dlc, suite: vcSuite} = await _generateCredential();
testLoader.addLoader(dlc);
const presentation = vc.createPresentation(
{verifiableCredential: credentials});
if(unsigned) {
return {presentation, suite: vcSuite,
documentLoader: testLoader.documentLoader.bind(testLoader)};
}
const authenticationKey = didDocument.methodFor({purpose: 'authentication'});
const vpSuite = new Ed25519Signature2018({key: authenticationKey});
const vp = await vc.signPresentation({
presentation, suite: vpSuite, challenge,
documentLoader: testLoader.documentLoader.bind(testLoader)
});
return {presentation: vp, suite: [vcSuite, vpSuite],
documentLoader: testLoader.documentLoader.bind(testLoader)};
}
async function _loadDid() {
const driver = new VeresOneDriver({mode: 'test'});
const didDocument = await driver.generate({
seed: new Uint8Array(32)
});
const documentLoader = url => {
let document;
if(url.includes('#')) {
// FIXME: code path not yet tested
throw new Error('FIXME');
document = didDocument.keyPairs.get(url);
} else if(url === didDocument.didDocument.id) {
document = didDocument.didDocument;
}
if(document) {
return {contextUrl: null, document, documentUrl: url};
}
throw new Error(`"${url}" not authorized to be resolved.`);
};
return {
didDocument, documentLoader
};
}