-
Notifications
You must be signed in to change notification settings - Fork 10
/
update_conformance_vcs.js
executable file
·397 lines (353 loc) · 13.8 KB
/
update_conformance_vcs.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
#!/usr/bin/env node
/* eslint-disable no-await-in-loop */
const { CONTEXT_URL, CONTEXT } = require('credentials-context');
const jsonld = require('jsonld');
const { strictDocumentLoader } = require('jsonld-signatures');
const { klona } = require('klona/json');
const fs = require('fs');
// If this changes, the `issuer` in `valid-credential.json` must be updated to
// match. In order for signed credentials to be valid, the controller for a
// verification method must match the credential `issuer`.
const seed = Buffer.from('9b937b81322d816cfab9d5a3baacc9b2');
// suitePromise generates promise that resolves to an Ed25519Signature2018
// signature suite implementation to use for issuing verifiable credentials.
const suitePromise = (async () => {
const { Ed25519VerificationKey2018 } = await import('@digitalbazaar/ed25519-verification-key-2018');
const { Ed25519Signature2018 } = await import('@digitalbazaar/ed25519-signature-2018');
const keyPair = await Ed25519VerificationKey2018.generate({ seed });
keyPair.id = `did:key:${keyPair.fingerprint()}#${keyPair.fingerprint()}`;
keyPair.controller = `did:key:${keyPair.fingerprint()}`;
return new Ed25519Signature2018({ key: keyPair });
})();
// noopMutator returns the input without any mutation.
const noopMutator = (input) => klona(input);
// mutatingDocumentLoader is a document loader implementation that allows mutation of
// the https://www.w3.org/2018/credentials/v1 context via a given mutation
// function.
const mutatingDocumentLoader = async ({ mutate = noopMutator } = {}) => {
const didKey = await import('@digitalbazaar/did-method-key');
// The default value is `Ed25519VerificationKey2020`, but we are using the
// `Ed25519VerificationKey2018` verification suite.
const didKeyDriver = didKey.driver('Ed25519VerificationKey2018');
return async (url) => {
if (url && url.startsWith('did:key')) {
const document = await didKeyDriver.get({ url });
return {
contextUrl: null,
document,
documentUrl: url,
tag: 'static',
};
}
if (url && url === CONTEXT_URL) {
return {
contextUrl: null,
document: mutate(klona(CONTEXT)),
documentUrl: url,
};
}
if (url && url == 'https://w3id.org/traceability/v1') {
let context = fs.readFileSync('./traceability-v1.jsonld', 'utf8');
context = JSON.parse(context);
return {
contextUrl: null,
// Don't mutate traceability context
document: klona(context),
documentUrl: url,
};
}
return strictDocumentLoader(url);
};
};
const addProof = async ({ credentialMutator = noopMutator, contextMutator = noopMutator } = {}) => {
const suite = await suitePromise;
const validVC = require('./valid-credential.json');
const document = credentialMutator(klona(validVC));
const documentLoader = await mutatingDocumentLoader({ mutate: contextMutator });
let proof = {
// Using a specific date allows proof creation to be idempotent.
created: '2006-01-02T15:04:05Z',
verificationMethod: suite.verificationMethod,
proofPurpose: 'assertionMethod',
type: 'Ed25519Signature2018',
};
const verifyData = await suite.createVerifyData({
document,
proof,
documentLoader,
});
proof = await suite.sign({ verifyData, proof });
jsonld.addValue(document, 'proof', proof);
return document;
};
const addNegativeTesting = async (suiteJson) => {
const sampleVCs = new Map();
{
const description = 'vc:@context:missing';
const credentialMutator = (credential) => {
const doc = klona(credential);
delete doc['@context'];
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
{
const invalidValues = new Map([
// Some mutations cause errors to be thrown when signing credentials. If a
// suitable context mutator can be found that will allow these to be signed,
// it should be implemented and these should be added to the suite. For
// now, these are left inline to indicate which mutations are outstanding.
//
// ['vc:@context:boolean', false],
// ['vc:@context:integer', 4],
// ['vc:@context:null', null],
['vc:@context:object', { '@vocab': 'https://www.w3.org/2018/credentials/v1/#' }],
['vc:@context:string', 'https://www.w3.org/2018/credentials/v1'],
]);
for (const [description, invalidValue] of invalidValues) {
const credentialMutator = (credential) => {
const doc = klona(credential);
doc['@context'] = invalidValue;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
}
{
const invalidValues = new Map([
// Some mutations cause errors to be thrown when signing credentials. If a
// suitable context mutator can be found that will allow these to be signed,
// it should be implemented and these should be added to the suite. For
// now, these are left inline to indicate which mutations are outstanding.
//
// ['vc:@context:item:array', ['https://www.w3.org/2018/credentials/v1', []]],
// ['vc:@context:item:boolean', ['https://www.w3.org/2018/credentials/v1', false]],
// ['vc:@context:item:integer', ['https://www.w3.org/2018/credentials/v1', 4]],
// ['vc:@context:item:null', ['https://www.w3.org/2018/credentials/v1', null]],
[
'vc:@context:item:object',
['https://www.w3.org/2018/credentials/v1', { '@vocab': 'https://www.w3.org/2018/credentials/v1/#' }],
],
]);
for (const [description, invalidValue] of invalidValues) {
const credentialMutator = (credential) => {
const doc = klona(credential);
doc['@context'] = invalidValue;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
}
{
const invalidValues = new Map([
['vc:id:array', ['urn:uuid:07aa969e-b40d-4c1b-ab46-ded252003ded']],
['vc:id:boolean', false],
['vc:id:integer', 123],
['vc:id:null', null],
['vc:id:object', { key: 'urn:uuid:07aa969e-b40d-4c1b-ab46-ded252003ded' }],
]);
for (const [description, invalidValue] of invalidValues) {
const credentialMutator = (credential) => {
const doc = klona(credential);
doc.id = invalidValue;
return doc;
};
const contextMutator = (context) => {
// Prevent 'protected term redefinition' error when mutating `id`
delete context['@context']['@protected'];
// Change `id` from `@id` to `xsd:string` to allow invalid values.
context['@context'].VerifiableCredential['@context'].id = 'xsd:string';
return context;
};
sampleVCs.set(description, await addProof({ credentialMutator, contextMutator }));
}
}
{
const description = 'vc:type:missing';
const credentialMutator = (credential) => {
const doc = klona(credential);
delete doc.type;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
{
const invalidValues = new Map([
// Some mutations cause errors to be thrown when signing credentials. If a
// suitable context mutator can be found that will allow these to be signed,
// it should be implemented and these should be added to the suite. For
// now, these are left inline to indicate which mutations are outstanding.
//
// ['vc:type:boolean', false],
// ['vc:type:integer', 123],
// ['vc:type:null', null],
// ['vc:type:object', { key: 'VerifiableCredential' }],
['vc:type:string', 'VerifiableCredential'],
// ['vc:type:item:array', ['VerifiableCredential', []]],
// ['vc:type:item:boolean', ['VerifiableCredential', false]],
// ['vc:type:item:integer', ['VerifiableCredential', 4]],
// ['vc:type:item:null', ['VerifiableCredential', null]],
// ['vc:type:item:object', ['VerifiableCredential', { foo: true }]],
]);
for (const [description, invalidValue] of invalidValues) {
const credentialMutator = (credential) => {
const doc = klona(credential);
doc.type = invalidValue;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
}
{
const description = 'vc:issuer:missing';
const credentialMutator = (credential) => {
const doc = klona(credential);
delete doc.issuer;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
{
const invalidValues = new Map([
['vc:issuer:array', ['did:example:123']],
['vc:issuer:boolean', false],
['vc:issuer:integer', 123],
['vc:issuer:null', null],
['vc:issuer:id:missing', {}],
['vc:issuer:id:array', { id: ['did:example:123'] }],
['vc:issuer:id:boolean', { id: false }],
['vc:issuer:id:integer', { id: 123 }],
['vc:issuer:id:null', { id: null }],
['vc:issuer:id:object', { id: { key: 'did:example:123' } }],
]);
for (const [description, invalidValue] of invalidValues) {
const credentialMutator = (credential) => {
const doc = klona(credential);
doc.issuer = invalidValue;
return doc;
};
// Context must be mutated to prevent nested `issuer.id` from being
// treated as a default `@id` element.
const contextMutator = (context) => {
context['@context'].VerifiableCredential['@context'].issuer = {
'@id': 'cred:issuer',
'@type': '@id',
'@context': { id: 'xsd:string' },
};
return context;
};
sampleVCs.set(description, await addProof({ credentialMutator, contextMutator }));
}
}
{
const description = 'vc:issuanceDate:missing';
const credentialMutator = (credential) => {
const doc = klona(credential);
delete doc.issuanceDate;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
{
const invalidValues = new Map([
['vc:issuanceDate:array', ['2010-01-01T19:23:24Z']],
['vc:issuanceDate:boolean', false],
['vc:issuanceDate:integer', 123],
['vc:issuanceDate:null', null],
['vc:issuanceDate:object', { key: '2010-01-01T19:23:24Z' }],
['vc:issuanceDate:string', 'not a valid XML Date Time string'],
]);
for (const [description, invalidValue] of invalidValues) {
const credentialMutator = (credential) => {
const doc = klona(credential);
doc.issuanceDate = invalidValue;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
}
{
const description = 'vc:credentialSubject:missing';
const credentialMutator = (credential) => {
const doc = klona(credential);
delete doc.credentialSubject;
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
{
const invalidValues = new Map([
['vc:credentialSubject:array', ['did:example:123']],
['vc:credentialSubject:boolean', false],
['vc:credentialSubject:integer', 123],
['vc:credentialSubject:null', null],
['vc:credentialSubject:string', 'did:example:123'],
['vc:credentialSubject:id:array', { id: ['did:example:123'] }],
['vc:credentialSubject:id:boolean', { id: false }],
['vc:credentialSubject:id:integer', { id: 123 }],
['vc:credentialSubject:id:null', { id: null }],
['vc:credentialSubject:id:object', { id: { key: 'did:example:123' } }],
]);
for (const [description, invalidValue] of invalidValues) {
const credentialMutator = (credential) => {
const doc = klona(credential);
doc.credentialSubject = invalidValue;
return doc;
};
// Context must be mutated to prevent nested `credentialSubject.id` from
// being treated as a default `@id` element.
const contextMutator = (context) => {
context['@context'].VerifiableCredential['@context'].credentialSubject = {
'@id': 'cred:credentialSubject',
'@type': '@id',
'@context': { id: 'xsd:string' },
};
return context;
};
sampleVCs.set(description, await addProof({ credentialMutator, contextMutator }));
}
}
let node = suiteJson.item.find((e) => e.name === 'Credentials - Verify');
node = node.item.find((e) => e.name === 'Negative Testing');
node = node.item.find((e) => e.name === 'Bad Request');
for (const [description, vc] of sampleVCs) {
const name = `credentials_verify:${description}`;
const test = node.item.find((e) => e.name === name);
if (test) {
const json = JSON.stringify({ verifiableCredential: vc }, null, 4).replace(/[\n]/g, '\n');
test.request.body.raw = json;
}
}
};
const addPositiveTesting = async (suiteJson) => {
const sampleVCs = new Map();
{
const description = 'credentials_verify';
sampleVCs.set(description, await addProof());
}
{
const description = 'credentials_verify:issuer:object';
const credentialMutator = (credential) => {
const doc = klona(credential);
doc.issuer = { id: doc.issuer };
return doc;
};
sampleVCs.set(description, await addProof({ credentialMutator }));
}
let node = suiteJson.item.find((e) => e.name === 'Credentials - Verify');
node = node.item.find((e) => e.name === 'Positive Testing');
for (const [description, vc] of sampleVCs) {
const test = node.item.find((e) => e.name === description);
if (test) {
const json = JSON.stringify({ verifiableCredential: vc }, null, 4).replace(/[\n]/g, '\n');
test.request.body.raw = json;
}
}
};
(async () => {
console.warn('🔥 TODO: update conformance tests to use vc+ld+jwt');
const suite = require('./conformance_suite.postman_collection.json');
await addNegativeTesting(suite);
await addPositiveTesting(suite);
console.log(JSON.stringify(suite, null, '\t'));
})();