-
Notifications
You must be signed in to change notification settings - Fork 2
/
conformancePreviousProof.js
165 lines (154 loc) · 5.21 KB
/
conformancePreviousProof.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
/*
Steps to create a signed verifiable credential with a simple
proof chain using Ed25519 signatures.
*/
import { mkdir } from 'fs/promises';
import { createRequire } from 'module';
import jsonld from 'jsonld';
import { localLoader } from './documentLoader.js';
import {secureDocument} from './conformanceTools.js';
const require = createRequire(import.meta.url);
// Create output directory for the results
const baseDir = "./output/eddsa-rdfc-2022/conformance/";
// recursively create the dirs for the baseDir is needed
let status = await mkdir(baseDir, {recursive: true});
jsonld.documentLoader = localLoader; // Local loader for JSON-LD
const keyPairs = require('./input/multiKeyPairs.json');
const chainKeys = [keyPairs.keyPair1, keyPairs.keyPair2]
const previousProof = 'urn:uuid:26329423-bec9-4b2e-88cb-a7c7d9dc4544';
const proofIds = [previousProof];
// set the first entry to null to prevent the first proof
// from having a previousProof set
const previousProofs = [null, previousProof];
// Read input documents from files.
const documents = new Map([
['1.1', require('./input/v1/unsecured.json')],
['2.0', require('./input/v2/unsecured.json')]
]);
// function to get all matching proofs (only first level no dependencies)
// prevProofs is either a string or an array
// proofs is an array of proofs
const findMatchingProofs = {
valid(prevProofs, proofs) {
console.log(`findMatch called with ${prevProofs}`);
let matches = [];
if (!prevProofs) { // In case of no previous proof edge case
return matches;
}
if (Array.isArray(prevProofs)) {
prevProofs.forEach(pp => {
let matchProof = proofs.find(p => p.id === pp);
if (!matchProof) {
throw new Error(`Missing proof for id = ${pp}`);
}
matches.push(matchProof);
})
} else {
let matchProof = proofs.find(p => p.id === prevProofs);
if (!matchProof) {
throw new Error(`Missing proof for id = ${prevProofs}`);
}
matches.push(matchProof);
}
return matches;
},
invalidType(prevProofs, proofs) {
console.log(`findMatch called with ${prevProofs}`);
let matches = [];
if (!prevProofs) { // In case of no previous proof edge case
return matches;
}
if (Array.isArray(prevProofs)) {
prevProofs.forEach(pp => {
// NOTE String is strictly for allowing the creation
// of invalid test data in this case a number as a previousProof
let matchProof = proofs.find(p => String(p.id) === String(pp));
if (!matchProof) {
throw new Error(`Missing proof for id = ${pp}`);
}
matches.push(matchProof);
})
} else {
// NOTE String is strictly for allowing the creation
// of invalid test data in this case a number as a previousProof
let matchProof = proofs.find(p => String(p.id) === String(prevProofs));
if (!matchProof) {
throw new Error(`Missing proof for id = ${prevProofs}`);
}
matches.push(matchProof);
}
return matches;
},
missingPreviousProof(prevProofs, proofs){
// for this test prevProofs don't match proof ids
// so just return the proofs
return proofs;
}
}
// create versioned VCs with previousProof as string
for(const [version, credential] of documents) {
await secureDocument({
baseDir,
credential,
fileName: `${version}-previousProofStringOk`,
previousProofType: 'string',
proofIds,
previousProofs,
findMatchingProofs: findMatchingProofs.valid,
chainKeys
});
}
// create versioned VCs with previousProof as an Array
for(const [version, credential] of documents) {
await secureDocument({
baseDir,
credential,
fileName: `${version}-previousProofArrayOk`,
findMatchingProofs: findMatchingProofs.valid,
previousProofType: 'Array',
proofIds,
previousProofs,
chainKeys
});
}
// create versioned VCs with previousProof as a Number
for(const [version, credential] of documents) {
await secureDocument({
baseDir,
credential,
chainKeys,
fileName: `${version}-previousProofNotStringFail`,
findMatchingProofs: findMatchingProofs.invalidType,
previousProofType: 'string',
previousProofs: [null, 456321],
proofIds: ['456321']
});
}
// create versioned VCs with missing previousProof
for(const [version, credential] of documents) {
await secureDocument({
baseDir,
credential,
chainKeys,
fileName: `${version}-previousProofStringMissingFail`,
findMatchingProofs: findMatchingProofs.missingPreviousProof,
previousProofType: 'string',
// this will result in a signed VC with a missing previousProof
previousProofs: [null, 'urn:uuid:38329423-2179-4b2e-88cb-a7c7d9dc4544'],
proofIds
});
}
// create versioned VCs with missing previousProof
for(const [version, credential] of documents) {
await secureDocument({
baseDir,
credential,
chainKeys,
fileName: `${version}-previousProofArrayMissingFail`,
findMatchingProofs: findMatchingProofs.missingPreviousProof,
previousProofType: 'Array',
// this will result in a signed VC with a missing previousProof
previousProofs: [null, 'urn:uuid:38329423-2179-4b2e-88cb-a7c7d9dc4544'],
proofIds
});
}