-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
175 lines (172 loc) · 6.67 KB
/
index.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
/*!
* Copyright (c) 2022-2023 Digital Bazaar, Inc.
*/
import {checkKeyType} from './assertions.js';
import {createRequire} from 'node:module';
import {generateTestData} from './vc-generator/index.js';
import {runDataIntegrityProofFormatTests} from './suites/create.js';
import {runDataIntegrityProofVerifyTests} from './suites/verify.js';
const require = createRequire(import.meta.url);
export const validVc = require('./validVc.json');
/**
* Validates the structure of the "proof" property on a digital document.
*
* @param {object} options - Options to use.
* @param {object} [options.credential = validVc] - A credential for
* the issuance tests.
* @param {string} options.cryptosuiteName - A cryptosuite name.
* @param {Map<string,object>} options.implemented - The vendors being tested.
* @param {Array<string>} [options.expectedProofTypes] - An option to specify
* the expected proof types. The default value is set to
* ['DataIntegrityProof'].
* @param {boolean} [options.isEcdsaTests] - A boolean option to specify
* if it is used in ecdsa test suite or not. The default value
* is set to false.
* @param {string} [options.testDescription] - An option to define
* the test description. The default value is set to
* `Data Integrity (issuer)`.
* @param {object} [options.optionalTests] - Toggle for running
* optional tests.
*
* @returns {object} Returns the test suite being run.
*/
export function checkDataIntegrityProofFormat({
implemented, expectedProofTypes = ['DataIntegrityProof'],
cryptosuiteName, isEcdsaTests = false, credential = validVc,
testDescription = 'Data Integrity (issuer)',
optionalTests = {
dates: false,
contextInjection: false,
domain: false,
proofChain: false
}
} = {}) {
return describe(testDescription, function() {
// this will tell the report
// to make an interop matrix with this suite
this.matrix = true;
this.report = true;
this.rowLabel = 'Test Name';
this.columnLabel = 'Issuer';
this.implemented = [];
for(const [vendorName, {endpoints}] of implemented) {
if(!endpoints) {
throw new Error(`Expected ${vendorName} to have endpoints.`);
}
for(const endpoint of endpoints) {
if(isEcdsaTests) {
const {supportedEcdsaKeyTypes} = endpoint.settings;
for(const supportedEcdsaKeyType of supportedEcdsaKeyTypes) {
const keyType = checkKeyType(supportedEcdsaKeyType);
this.implemented.push(`${vendorName}: ${keyType}`);
runDataIntegrityProofFormatTests({
cryptosuiteName, endpoints, expectedProofTypes, credential,
testDescription: `${vendorName}: ${keyType}`, vendorName,
optionalTests
});
}
} else {
this.implemented.push(vendorName);
runDataIntegrityProofFormatTests({
cryptosuiteName, credential, endpoints,
expectedProofTypes, testDescription: vendorName, vendorName,
optionalTests
});
}
}
} // end for loop
}); // end describe
}
/**
* Verifies a proof on Verifiable Credential.
*
* @param {object} options - Options to use.
* @param {Map<string,object>} options.implemented - The vendors being tested.
* @param {string} [options.expectedProofType] - An option to specify
* the expected proof type that is used to generate test titles.
* The default value is set to 'DataIntegrityProof'.
* @param {boolean} [options.isEcdsaTests] - A boolean option to specify
* if it is used in ecdsa test suite or not. The default value
* is set to false.
* @param {string} [options.testDescription] - An option to define
* the test description. The default value is set to
* `Data Integrity (verifier)`.
* @param {object} options.testDataOptions - Options for test data creation
* such as suite.
* @param {object} [options.optionalTests] - Options for running
* optional tests from DataIntegrity such as created and authentication.
*
* @returns {object} Returns the test suite being run.
*/
export function checkDataIntegrityProofVerifyErrors({
implemented, expectedProofType = 'DataIntegrityProof',
isEcdsaTests = false, testDescription = 'Data Integrity (verifier)',
testDataOptions,
optionalTests = {
dates: false,
authentication: false,
proofChain: false
}
} = {}) {
return describe(testDescription, async function() {
// this will tell the report
// to make an interop matrix with this suite
this.matrix = true;
this.report = true;
this.rowLabel = 'Test Name';
this.columnLabel = 'Verifier';
this.implemented = [];
const credentials = {};
before(async function() {
const data = await generateTestData({...testDataOptions, optionalTests});
// this might seem weird, but mocha won't wait for credentials to be set
// before passing the credentials var to the tests
// so we just update the credentials passed to the actual test suite
Object.assign(credentials, data);
});
for(const [vendorName, {endpoints}] of implemented) {
if(!endpoints) {
throw new Error(`Expected ${vendorName} to have endpoints.`);
}
for(const endpoint of endpoints) {
let name;
// FIXME remove this in MAJOR release and use testDataOptions.keyType
// if defined
if(isEcdsaTests) {
const {supportedEcdsaKeyTypes} = endpoint.settings;
const keyTypes = supportedEcdsaKeyTypes.join(', ');
name = `${vendorName}: ${keyTypes}`;
} else if(testDataOptions?.keyType && !isEcdsaTests) {
name = `${vendorName}: ${testDataOptions.keyType}`;
} else {
name = vendorName;
}
this.implemented.push(name);
runDataIntegrityProofVerifyTests({
endpoints,
expectedProofType,
testDescription: name,
vendorName,
credentials,
testDataOptions,
optionalTests
});
}
} // end for loop
}); // end describe
}
// export all assertions
export * as assertions from './assertions.js';
export {generators} from './vc-generator/generators.js';
export {deriveCloned, issueCloned} from './vc-generator/issuer.js';
export {createDocLoader} from './vc-generator/documentLoader.js';
// this is just here for backwards compatitility to avoid a major release
// FIXME remove this on next MAJOR release
export {
dateRegex, expectedMultibasePrefix, isObjectOrArrayOfObjects,
shouldBeErrorResponse, shouldBeUrl, isStringOrArrayOfStrings,
isValidMultibaseEncoded, shouldBeBs58, shouldBeBase64NoPadUrl,
verificationFail
} from './assertions.js';
export {createInitialVc} from './helpers.js';
export {algorithmsSuite} from './suites/algorithms.js';