-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkchains_badges.js
361 lines (322 loc) · 12.5 KB
/
linkchains_badges.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
/*
* Copyright (c) 2021.
* Knowledge Media Institute, The Open University
*/
const jsonld = require('jsonld');
const pngitxt = require("png-itxt");
// how to override the default document loader with a custom one -- for
// example, one that uses pre-loaded contexts:
// define a mapping of context URL => context doc
const CONTEXTS = {
"https://w3id.org/openbadges/v2": {
"@context": {
"id": "@id",
"type": "@type",
"extensions": "https://w3id.org/openbadges/extensions#",
"obi": "https://w3id.org/openbadges#",
"validation": "obi:validation",
"cred": "https://w3id.org/credentials#",
"dc": "http://purl.org/dc/terms/",
"schema": "http://schema.org/",
"sec": "https://w3id.org/security#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"AlignmentObject": "schema:AlignmentObject",
"CryptographicKey": "sec:Key",
"Endorsement": "cred:Credential",
"Assertion": "obi:Assertion",
"BadgeClass": "obi:BadgeClass",
"Criteria": "obi:Criteria",
"Evidence": "obi:Evidence",
"Extension": "obi:Extension",
"FrameValidation": "obi:FrameValidation",
"IdentityObject": "obi:IdentityObject",
"Image": "obi:Image",
"HostedBadge": "obi:HostedBadge",
"hosted": "obi:HostedBadge",
"Issuer": "obi:Issuer",
"Profile": "obi:Profile",
"RevocationList": "obi:RevocationList",
"SignedBadge": "obi:SignedBadge",
"signed": "obi:SignedBadge",
"TypeValidation": "obi:TypeValidation",
"VerificationObject": "obi:VerificationObject",
"author": { "@id": "schema:author", "@type": "@id" },
"caption": { "@id": "schema:caption" },
"claim": { "@id": "cred:claim", "@type": "@id" },
"created": { "@id": "dc:created", "@type": "xsd:dateTime" },
"creator": { "@id": "dc:creator", "@type": "@id" },
"description": { "@id": "schema:description" },
"email": { "@id": "schema:email" },
"endorsement": { "@id": "cred:credential", "@type": "@id" },
"expires": { "@id": "sec:expiration", "@type": "xsd:dateTime" },
"genre": { "@id": "schema:genre" },
"image": { "@id": "schema:image", "@type": "@id" },
"name": { "@id": "schema:name" },
"owner": { "@id": "sec:owner", "@type": "@id" },
"publicKey": { "@id": "sec:publicKey", "@type": "@id" },
"publicKeyPem": { "@id": "sec:publicKeyPem" },
"related": { "@id": "dc:relation", "@type": "@id" },
"startsWith": { "@id": "http://purl.org/dqm-vocabulary/v1/dqm#startsWith" },
"tags": { "@id": "schema:keywords" },
"targetDescription": { "@id": "schema:targetDescription" },
"targetFramework": { "@id": "schema:targetFramework" },
"targetName": { "@id": "schema:targetName" },
"targetUrl": { "@id": "schema:targetUrl" },
"telephone": { "@id": "schema:telephone" },
"url": { "@id": "schema:url", "@type": "@id" },
"version": { "@id": "schema:version" },
"alignment": { "@id": "obi:alignment", "@type": "@id" },
"allowedOrigins": { "@id": "obi:allowedOrigins" },
"audience": { "@id": "obi:audience" },
"badge": { "@id": "obi:badge", "@type": "@id" },
"criteria": { "@id": "obi:criteria", "@type": "@id" },
"endorsementComment": { "@id": "obi:endorsementComment" },
"evidence": { "@id": "obi:evidence", "@type": "@id" },
"hashed": { "@id": "obi:hashed", "@type": "xsd:boolean" },
"identity": { "@id": "obi:identityHash" },
"issuedOn": { "@id": "obi:issueDate", "@type": "xsd:dateTime" },
"issuer": { "@id": "obi:issuer", "@type": "@id" },
"narrative": { "@id": "obi:narrative" },
"recipient": { "@id": "obi:recipient", "@type": "@id" },
"revocationList": { "@id": "obi:revocationList", "@type": "@id" },
"revocationReason": { "@id": "obi:revocationReason" },
"revoked": { "@id": "obi:revoked", "@type": "xsd:boolean" },
"revokedAssertions": { "@id": "obi:revoked" },
"salt": { "@id": "obi:salt" },
"targetCode": { "@id": "obi:targetCode" },
"uid": { "@id": "obi:uid" },
"validatesType": "obi:validatesType",
"validationFrame": "obi:validationFrame",
"validationSchema": "obi:validationSchema",
"verification": { "@id": "obi:verify", "@type": "@id" },
"verificationProperty": { "@id": "obi:verificationProperty" },
"verify": "verification"
}
}
};
var documentLoader;
if (typeof XMLHttpRequest !== 'undefined') {
documentLoader = jsonld.documentLoaders.xhr()
} else {
documentLoader = jsonld.documentLoaders.node();
}
// change the default document loader
const customLoader = async (url, options) => {
if (url in CONTEXTS) {
return {
contextUrl: null, // this is for a context via a link header
document: CONTEXTS[url], // this is the actual document that was loaded
documentUrl: url // this is the actual context URL after redirects
};
}
// call the default documentLoader
return documentLoader(url);
};
jsonld.documentLoader = customLoader;
const linkchains = require('linkchains-merkle/linkchains.js');
const assertion_template = {
"@context": [
"https://w3id.org/openbadges/v2",
{
"@vocab": "https://blockchain.open.ac.uk/vocab/"
}
],
"type": "Assertion",
"recipient": {
"type": "email",
"hashed": true,
"salt": "deadsea"
}
};
const badge_template = {
"@context": [
"https://w3id.org/openbadges/v2",
{
"@vocab": "https://blockchain.open.ac.uk/vocab/"
}
],
"type": "BadgeClass",
"name": "a title",
"description": "some description",
"image": "imageurl",
"version": "version",
"criteria": {
"type": "Criteria",
"narrative": "The holder of this badge has demonstrated the specified skills",
"skills": "skills"
},
"issuer": {
"type": "Issuer",
"name": "issuername",
"description": "issuerdescription"
}
};
const issued_badge_template = {
"badge": {
"@context": [
"https://w3id.org/openbadges/v2",
{
"@vocab": "https://blockchain.open.ac.uk/vocab/"
}
],
"type": "BadgeClass",
"name": "a title",
"description": "some description",
"image": "imageurl",
"version": "version",
"issuer": {
"type": "Issuer",
"name": "issuername",
"description": "issuerdescription",
"url": "issuerurl",
"email": "issueremail",
"telephone": "",
"image": "issuerimageurl"
},
"criteria": {
"type": "Criteria",
"narrative": "The holder of this badge has achieved great things in QualiChain",
"skills": "skills"
},
"tags": [
"decentralisation",
"blockchain"
]
},
"recipient": {
"recipientname": "name of recipient",
"recipientemail": "email address of the recipient"
}
};
const templated_used_to_verify = {
"type": "MerQLVerification2020"
};
async function createSmartBadge(details) {
try {
const badgeTemplate = badge_template;
var badge = JSON.parse(JSON.stringify(badgeTemplate));
//console.log(stringify(badge, { space: 4 }));
if (details.title &&
details.description &&
details.issuer &&
details.issuer.issuername &&
details.issuer.issuerdescription &&
details.version &&
details.imageurl) {
badge.name = details.title;
badge.description = details.description;
badge.image = details.imageurl;
badge.version = details.version;
badge.issuer.name = details.issuer.issuername;
badge.issuer.description = details.issuer.issuerdescription;
} else {
throw new Error("Missing parameters");
}
if (details.issuer) {
if (details.issuer.issuerurl) {
badge.issuer.url = details.issuer.issuerurl;
}
if (details.issuer.issuerimageurl) {
badge.issuer.image = details.issuer.issuerimageurl;
}
if (details.issuer.issueremail) {
badge.issuer.email = details.issuer.issueremail;
}
if (details.issuer.issuertelephone) {
badge.issuer.telephone = details.issuer.issuertelephone;
}
}
if (details.criteria) {
badge.criteria = details.criteria;
}
if (details.skills) {
badge.criteria.skills = details.skills;
}
return badge;
} catch (error) {
throw error;
}
}
async function issueSmartBadge(cfg, details, anchorData) {
var sha256 = require('js-sha256');
const assertionTemplate = assertion_template;
var assertion = JSON.parse(JSON.stringify(assertionTemplate));
const verificationTemplate = templated_used_to_verify;
var verification = JSON.parse(JSON.stringify(verificationTemplate));
var badge = "";
var recipient = "";
if (details.badge && details.recipient) {
badge = details.badge;
recipient = details.recipient;
} else {
throw new Error("Missing parameters");
}
//console.log(stringify(assertion, { space : 4 }));
/* Create assertion */
assertion.recipient.name = recipient.name;
assertion.recipient.email = recipient.email;
delete badge['@context'];
assertion.badge = badge;
// Issue timestamp + SHA256 hashing email with salt to get identity
assertion.issuedOn = Date.now();
assertion.recipient.identity = 'sha256$' + sha256(recipient.email + recipient.salt);
//console.log(stringify(assertion, { space: 4 }));
//console.log(stringify(badge, { space: 4 }));
var quads = await jsonld.canonize(assertion, {
algorithm: 'URDNA2015',
format: 'application/n-quads',
documentLoader: customLoader
});
//console.log(quads);
/* Pass the badge to the linkchains library to get the metadta */
var metadata = await linkchains.getVerificationMetadata(assertion, cfg.options);
/* Call anchor with metadata and get metadata + more */
var anchoredMetadata = await anchorData(cfg, metadata);
/* Add verification and signature in the badge */
// verification template used
assertion.verification = verification;
// signature contents comes directly from anchoredMetadata
assertion.signature = anchoredMetadata.merkletrees.anchor;
/* Return badge and metadata + more */
return {
badge: assertion,
metadata: anchoredMetadata
};
}
async function verifySmartBadge(cfg, badge, getAnchoredData) {
if (badge.verification.type === "MerQLVerification2020" &&
badge.signature.type === "ETHMerQL") {
var metadata = {};
metadata.anchor = badge.signature;
metadata.indexhash = badge.signature.indexhash;
metadata.settings = badge.signature.settings;
delete metadata.anchor.indexhash;
delete metadata.anchor.settings;
delete badge.verification;
delete badge.signature;
var quads = await jsonld.canonize(badge, {
algorithm: 'URDNA2015',
format: 'application/n-quads',
documentLoader: customLoader
});
var verifyResult = await linkchains.verify(quads, metadata, cfg, getAnchoredData);
if (typeof (verifyResult.unverified) !== undefined
&& typeof (verifyResult.verified) !== undefined
&& verifyResult.unverified === "") {
//console.log(stringify(verifyResult, { space : 4 }));
return true;
} else {
return false;
}
} else {
throw new Error("Unsupported badge type");
}
}
async function addSmartBadgeToImage(png, badge) {
var result = await streamifier.createReadStream(png).pipe(pngitxt.set({
keyword: 'openbadges',
value: badge
}));
return result;
}
module.exports = { createSmartBadge, issueSmartBadge, verifySmartBadge, addSmartBadgeToImage };