-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.js
301 lines (257 loc) · 7.52 KB
/
build.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
/*!
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
*/
// generate citizenship vocab spec examples
// debugging globals
const verbose = true;
const diagnoseCborLd = verbose || false;
import * as EcdsaMultikey from '@digitalbazaar/ecdsa-multikey';
import {cryptosuite as ecdsaRdfc2019Cryptosuite} from
'@digitalbazaar/ecdsa-rdfc-2019-cryptosuite';
//import * as vc from '@digitalbazaar/vc';
import * as vc from '@digitalbazaar/vc';
import {DataIntegrityProof} from '@digitalbazaar/data-integrity';
import {driver} from '@digitalbazaar/did-method-key';
import {fileURLToPath} from 'node:url';
import fs from 'node:fs/promises';
import path from 'node:path';
import {util} from '@digitalbazaar/vpqr';
const {toQrCode} = util;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// setup document loader
import {CachedResolver} from '@digitalbazaar/did-io';
import {securityLoader} from '@digitalbazaar/security-document-loader';
//import secCtx from '@digitalbazaar/security-context';
import diCtx from '@digitalbazaar/data-integrity-context';
import * as credContexts from '@digitalbazaar/credentials-context';
const loader = securityLoader();
const contexts = [
diCtx,
credContexts
];
for(const context of contexts) {
for(const [url, c] of context.contexts.entries()) {
loader.addStatic(url, c);
}
}
for(const v of ['1', '2']) {
const json = await fs.readFile(
path.join(__dirname, `../contexts/citizenship-v${v}.jsonld`));
const context = JSON.parse(json);
loader.addStatic(`https://w3id.org/citizenship/v${v}`, context);
}
const resolver = new CachedResolver();
const didKeyDriverMultikey = driver();
didKeyDriverMultikey.use({
multibaseMultikeyHeader: 'zDna',
fromMultibase: EcdsaMultikey.from
});
resolver.use(didKeyDriverMultikey);
loader.setDidResolver(resolver);
const documentLoader = loader.build();
// generate new key
// NOTE: this will be different every run
async function generateKey() {
// generate example ecdsa keypair
const ecdsaKeyPair = await EcdsaMultikey.generate({curve: 'P-256'});
const {
didDocument, keyPairs, methodFor
} = await didKeyDriverMultikey.fromKeyPair({
verificationKeyPair: ecdsaKeyPair
});
ecdsaKeyPair.id = didDocument.assertionMethod[0];
ecdsaKeyPair.controller = didDocument.id;
return ecdsaKeyPair;
}
// example static key
// NOTE: use this to limit test output changes
async function exampleKey() {
const publicKeyMultibase =
'zDnaeW9VZZs7NH1ykvS5EMFmdodu2wj4dPcrV3DzTAadrXJee';
const secretKeyMultibase =
'z42tpY3zN95smjbeUnE5kUDdg2Graw8pD199ct1SMWQ2djhE';
const controller = `did:key:${publicKeyMultibase}`;
const keyId = `${controller}#${publicKeyMultibase}`;
const ecdsaMultikeyKeyPair = {
'@context': 'https://w3id.org/security/multikey/v1',
type: 'Multikey',
controller,
id: keyId,
publicKeyMultibase,
secretKeyMultibase
};
const ecdsaKeyPair = await EcdsaMultikey.from({...ecdsaMultikeyKeyPair});
console.log(ecdsaKeyPair);
return ecdsaKeyPair;
}
async function sign({
credential,
ecdsaKeyPair,
date,
documentLoader
}) {
// setup ecdsa-rdfc-2019 signing suite
const signingSuite = new DataIntegrityProof({
signer: ecdsaKeyPair.signer(),
date: '2023-01-01T01:01:01Z',
cryptosuite: ecdsaRdfc2019Cryptosuite
});
// sign credential
const verifiableCredential = await vc.issue({
credential,
suite: signingSuite,
documentLoader
});
// setup ecdsa-rdfc-2019 verifying suite
const verifyingSuite = new DataIntegrityProof({
cryptosuite: ecdsaRdfc2019Cryptosuite
});
// verify signed credential (only done to check)
const verifyResult = await vc.verifyCredential({
credential: verifiableCredential,
suite: verifyingSuite,
documentLoader
});
return {
verifiableCredential
};
}
async function _example({name}) {
console.log(`=== BUILDING: name=${name}`);
// track data for use in templates
const data = {};
const inputJson = await fs.readFile(
path.join(__dirname, `${name}.jsonld`), 'utf8');
const input = JSON.parse(inputJson);
// generate new key
//const ecdsaKeyPair = await generateKey();
// use static key to avoid test output churn
const ecdsaKeyPair = await exampleKey();
// set signing date
// NOTE: using static date to limit test output changes
const date = '2023-01-01T01:01:01Z';
// set issuer to key controller
input.issuer = ecdsaKeyPair.controller;
if(verbose) {
console.log(`=== INPUT WITH ISSUER: name=${name}`);
console.log(input);
}
// sign
const {verifiableCredential} = await sign({
credential: input,
ecdsaKeyPair,
date,
documentLoader
});
data.vc = verifiableCredential;
data.vcJsonLd = JSON.stringify(verifiableCredential);
if(verbose) {
console.log(`=== VC: name=${name}`);
console.log(data.vc);
}
// write out signed data
await fs.writeFile(
path.join(__dirname, `${name}-signed.jsonld`),
JSON.stringify(data.vc, null, 2));
// create QR code
const {
version, payload, imageDataUrl, encodedCborld, rawCborldBytes
} = await toQrCode({
header: 'VC1-',
jsonldDocument: verifiableCredential,
documentLoader,
diagnose: diagnoseCborLd ? console.log : null
});
data.vcQrCodeVersion = version;
data.vcRawCborLd = rawCborldBytes;
data.vcEncodedCborLd = encodedCborld;
data.vcQrPayload = payload;
data.vcQrCode = imageDataUrl;
if(verbose) {
console.log(`=== VC QR CODE VERSION: name=${name}`);
console.log(data.vcQrCodeVersion);
console.log(`=== VC RAW CBOR-LD: ` +
`name=${name} length=${data.vcRawCborLd.length}`);
console.log(data.vcRawCborLd);
console.log(`=== VC ENCODED CBOR-LD: ` +
`name=${name} length=${data.vcEncodedCborLd.length}`);
console.log(data.vcEncodedCborLd);
console.log(`=== VC QR PAYLOAD: ` +
`name=${name} length=${data.vcQrPayload.length}`);
console.log(data.vcQrPayload);
console.log(`=== VC QR CODE: ` +
`name=${name} length=${data.vcQrCode.length}`);
console.log(data.vcQrCode);
}
// write out QR code HTML
const qrcodeHTML = `
<dl>
<dt>QR Code</dt>
<dd><img alt="QR" src="${data.vcQrCode}"/></dd>
<dt>QR Code Version</dt>
<dd>${data.vcQrCodeVersion}</dd>
</dl>
`;
await fs.writeFile(
path.join(__dirname, `${name}-qrcode.html`),
qrcodeHTML);
// write out info HTML
const infoHTML = `
<div>
<table class="simple vc-info">
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>QR Code Version</td>
<td>${data.vcQrCodeVersion}</td>
</tr>
<tr>
<td>Verifiable Credential JSON-LD</td>
<td>${data.vcJsonLd.length} bytes</td>
</tr>
<tr>
<td>Raw CBOR-LD</td>
<td>${data.vcRawCborLd.length} bytes</td>
</tr>
<tr>
<td>Encoded CBOR-LD</td>
<td>${data.vcEncodedCborLd.length} bytes</td>
</tr>
<tr>
<td>QR Payload</td>
<td>${data.vcQrPayload.length} bytes</td>
</tr>
<tr>
<td>QR Code Image URL</td>
<td>${data.vcQrCode.length} bytes</td>
</tr>
</tbody>
</table>
</div>
`;
await fs.writeFile(
path.join(__dirname, `${name}-info.html`),
infoHTML);
console.log(`=== DONE: name=${name}`);
}
async function main() {
const examples = [
'citizenship-min',
'citizenship-full',
'ead-min',
'ead-full',
'naturalization-min',
'naturalization-full',
'prc-min',
'prc-full'
];
for(const example of examples) {
await _example({name: example});
}
}
await main();