-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
570 lines (520 loc) · 16.8 KB
/
index.ts
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import { CachedResolver } from "@digitalbazaar/did-io";
import { DataIntegrityProof } from "@digitalbazaar/data-integrity";
import { cryptosuite } from "@digitalbazaar/eddsa-rdfc-2022-cryptosuite";
import * as vc from "@digitalbazaar/vc";
import jsig from "jsonld-signatures";
const { extendContextLoader } = jsig;
import Ajv2020 from "ajv/dist/2020.js"; // Use 2020-12 schema
import jsonld from "jsonld";
import dataIntegrityContext from "@digitalbazaar/data-integrity-context";
import * as credentialsContext from "@digitalbazaar/credentials-context";
import { sha256 } from "multiformats/hashes/sha2";
import { base32 } from "multiformats/bases/base32";
import * as json from "multiformats/codecs/json";
import { compareBinaryToMultibaseHashes } from "@dsnp/hash-util";
type JsonLdContext =
/* Either a string, or an array containing strings and objects with string values */
string | (string | { [name: string]: string })[];
export interface JsonSchema_2020_12 {
$schema: "https://json-schema.org/draft/2020-12/schema";
title: string;
[key: string]: any; // Allow additional keys
}
export interface VerifiableCredential {
"@context": JsonLdContext;
id?: string;
type: string[];
issuer:
| string
| {
id: string;
authority: [];
[key: string]: any;
};
issuanceDate: string;
expirationDate?: string;
credentialSchema: {
type: "JsonSchemaCredential" | "JsonSchema";
id: string;
digestSRI?: string;
[key: string]: any; // Allow additional keys
};
credentialSubject: {
[key: string]: any;
};
proof?: {
verificationMethod: string;
[key: string]: any;
};
[key: string]: any; // Allow additional keys
}
const ajv = new Ajv2020.default();
const nodeDocumentLoader = jsonld.documentLoaders.node();
type VerificationMethod = {
id: string;
};
/**
* Finds an assertionMethod within the DID document with a matching
* fragment identifier.
*/
function findAssertionMethod(
document: {
assertionMethod?: VerificationMethod[];
},
fragment: string,
): object | null {
if (!document.assertionMethod) {
return null;
}
// TODO allow for single assertionMethod as well as array?
const foundMethod = document.assertionMethod.find(
(verificationMethod: VerificationMethod) => {
return verificationMethod.id.endsWith("#" + fragment);
},
);
return foundMethod || null;
}
export type SignResult = {
signed: boolean;
reason?: "exception";
context?: any;
};
const didRegex = new RegExp(
"^did:[a-z0-9]+(:([-a-zA-Z0-9._]|%[A-F0-9][A-F0-9])+)+$",
);
export type VerifyResult = {
verified: boolean;
reason?:
| "invalidDid"
| "proofNotFromIssuer"
| "signatureFailsVerification"
| "schemaUrlNotHttps"
| "unknownSchemaType"
| "credentialTitleMismatch"
| "schemaValidationError"
| "untrustedIssuer"
| "unresolvableDid"
| "fileNotFound"
| "incorrectAttributeSetType"
| "fileIntegrityError"
| "exception";
context?: any;
display?: object;
};
type DocumentLoaderResult = {
document: string | object;
documentUrl: string;
contextUrl: string | null;
};
export class DSNPVC {
private loaderCache: { [schemaUrl: string]: object } = {};
private didResolver: CachedResolver | null;
private documentLoader: (url: string) => Promise<DocumentLoaderResult>;
constructor(options: { resolver: null | CachedResolver }) {
this.addToCache({
document: dataIntegrityContext.CONTEXT,
documentUrl: dataIntegrityContext.CONTEXT_URL,
});
["v1", "v2", "undefined-terms-v2"].forEach((shortName) => {
const contextMetadata = credentialsContext.named.get(shortName);
this.addToCache({
document: contextMetadata.context,
documentUrl: contextMetadata.id,
});
});
this.didResolver = options.resolver;
// Document loader used to resolve links in credentials and schema
// TODO currently never expires anything from cache, this should be tuneable
this.documentLoader = extendContextLoader(async (url: string) => {
const cached = this.loaderCache[url];
if (cached) {
return cached;
}
// Resolve DID URLs via the DID resolver framework
if (url.startsWith("did:")) {
if (this.didResolver) {
const fragmentIndex = url.lastIndexOf("#");
const baseDid =
fragmentIndex === -1 ? url : url.substring(0, fragmentIndex);
const didDocument = await this.didResolver.get({ did: baseDid });
if (didDocument) {
let document: object | null = didDocument;
// We have a DID document, but might only need a key identified by fragment
if (fragmentIndex !== -1) {
document = findAssertionMethod(
didDocument,
url.substring(fragmentIndex + 1),
);
//TODO deal with null value here?
}
return { document };
}
}
}
// Fall back to loading from the web
const output = nodeDocumentLoader(url);
this.addToCache(output);
return output;
});
}
async sign(
credential: VerifiableCredential,
signer: {
id: string;
algorithm: string;
sign: (obj: any) => Uint8Array;
},
): Promise<SignResult> {
try {
const suite = new DataIntegrityProof({ signer, cryptosuite });
await vc.issue({
credential,
suite,
documentLoader: this.documentLoader,
});
} catch (e) {
return {
signed: false,
reason: "exception",
context: e,
};
}
return { signed: true };
}
async verify(
credential: VerifiableCredential,
attributeSetType?: string,
): Promise<VerifyResult> {
try {
// issuer or issuer.id should be a valid DID
const issuerId =
typeof credential.issuer === "string"
? credential.issuer
: credential.issuer.id;
if (!didRegex.test(issuerId)) {
return { verified: false, reason: "invalidDid" };
}
// proof.verificationMethod should start with issuer User URI + "#"
if (!credential.proof?.verificationMethod.startsWith(issuerId + "#")) {
return { verified: false, reason: "proofNotFromIssuer" };
}
const suite = new DataIntegrityProof({ cryptosuite });
// Perform verification of the signature (does not validate against schema)
const output = await vc.verifyCredential({
credential,
suite,
documentLoader: this.documentLoader,
purpose: {
validate: (proof: any) => {
return {
valid: proof.proofPurpose === "assertionMethod",
};
},
match: (proof: any, { document, documentLoader }: any) => {
return true;
},
},
});
if (!output.verified) {
return {
verified: false,
reason: "signatureFailsVerification",
context: output,
};
}
let display = undefined;
// Retrieve schema
// TODO should we allow credentials with no schema?
if (credential.credentialSchema) {
const schemaUrl = credential.credentialSchema.id;
// Only accept HTTPS URLs
if (!schemaUrl.startsWith("https://")) {
return { verified: false, reason: "schemaUrlNotHttps" };
}
if (
schemaUrl ===
"https://www.w3.org/2022/credentials/v2/json-schema-credential-schema.json"
) {
// Document we're verifying is a schema VC, no need to check its schema
return { verified: true };
}
const { document: schemaDocument } =
await this.documentLoader(schemaUrl);
if (credential.credentialSchema.type === "JsonSchemaCredential") {
const schemaCredential =
typeof schemaDocument === "string"
? JSON.parse(schemaDocument)
: schemaDocument;
// Ensure that it is a schemaCredential
if (schemaCredential.type.indexOf("JsonSchemaCredential") == -1) {
return {
verified: false,
reason: "unknownSchemaType",
context: { type: schemaCredential.type },
};
}
// Check the schema credential's schema title against the type of the VC
if (
credential.type.indexOf(
schemaCredential.credentialSubject.jsonSchema.title,
) == -1
) {
return {
verified: false,
reason: "credentialTitleMismatch",
context: {
title: schemaCredential.credentialSubject.jsonSchema.title,
type: credential.type,
},
};
}
// Verify the schema credential's proof
const schemaVerifyResult = await this.verify(schemaCredential);
// Validate the credential against its schema
const valid = ajv.validate(
schemaCredential.credentialSubject.jsonSchema,
credential,
);
if (!valid) {
return {
verified: false,
reason: "schemaValidationError",
context: ajv.errors || undefined,
};
}
// Check for required trust chains
if (schemaCredential.credentialSubject.dsnp?.trust) {
const trust: any = schemaCredential.credentialSubject.dsnp.trust;
if (trust.oneOf) {
const promises: Promise<VerifyResult>[] = [];
trust.oneOf.forEach((attributeSetType: string) => {
promises.push(
this.resolveAndVerifyAuthority(
credential.issuer,
attributeSetType,
),
);
});
const results = await Promise.all(promises);
if (!results.some((result) => result.verified)) {
return {
verified: false,
reason: "untrustedIssuer",
context: {
issuer: issuerId,
oneOf: trust.oneOf,
results,
},
};
}
}
if (trust.allOf) {
const promises: Promise<VerifyResult>[] = [];
trust.oneOf.forEach((attributeSetType: string) => {
promises.push(
this.resolveAndVerifyAuthority(
credential.issuer,
attributeSetType,
),
);
});
const results = await Promise.all(promises);
if (!results.every((result) => result.verified)) {
return {
verified: false,
reason: "untrustedIssuer",
context: {
issuer: issuerId,
oneOf: trust.oneOf,
results,
},
};
}
}
} // has trust section
display = schemaCredential.credentialSubject.dsnp?.display;
} // type JsonSchemaCredential
// Check attributeSetType matches, if specified
if (attributeSetType) {
const credentialAttributeSetType = await this.getAttributeSetType(
credential,
schemaDocument,
);
if (attributeSetType !== credentialAttributeSetType) {
return {
verified: false,
reason: "incorrectAttributeSetType",
context: {
attributeSetType,
credentialAttributeSetType,
},
};
}
}
return {
verified: true,
display,
};
} // has schema
// All checks complete
// TODO cache the result?
return {
verified: true,
};
} catch (e) {
console.log(e);
return {
verified: false,
reason: "exception",
context: e,
};
}
}
async resolveAndVerifyAuthority(
issuer:
| string
| {
id: string;
authority: {
id: string;
rel: string;
hash: string[];
}[];
},
attributeSetType: string,
): Promise<VerifyResult> {
if (typeof issuer === "string") {
return {
verified: false,
reason: "untrustedIssuer",
};
}
let context: {
[key: string]: object | string;
} = {
issuer: issuer.id,
attributeSetType,
};
// Does issuer claim to have matching authority?
const found = issuer.authority.find((authority) => {
return authority.rel === attributeSetType;
});
if (found) {
// TODO check that found.id URL is allowed
let { document } = await this.documentLoader(found.id);
if (!document)
return {
verified: false,
reason: "fileNotFound",
context: found.id,
};
// Verify hash of retrieved document against found.hash
const documentBytes =
typeof document === "string"
? new TextEncoder().encode(document)
: json.encode(document);
const hasMatch = compareBinaryToMultibaseHashes(
documentBytes,
found.hash,
);
if (!hasMatch)
return {
verified: false,
reason: "fileIntegrityError",
context: {
url: found.id,
expectedHash: found.hash,
},
};
const documentObj =
typeof document === "string" ? JSON.parse(document) : document;
// TODO verify that it is a VerifiableCredential before the "as"?
const accreditationCheckResult = await this.verify(
documentObj as VerifiableCredential,
);
if (accreditationCheckResult.verified) {
return accreditationCheckResult;
} else {
context = {
issuerCredential: documentObj,
verifyResult: accreditationCheckResult,
};
}
}
return {
verified: false,
reason: "untrustedIssuer",
context,
};
}
addToCache(options: {
contextUrl?: string;
document: string | object;
documentUrl: string;
}) {
// if (typeof options.document === "object")
this.loaderCache[options.documentUrl] = { ...options };
}
/**
* Returns the (claimed) attributeSetType to use for a given credential, following the DSNP algorithm.
* Note that this function does not perform verification of any kind; use the verify() method for that.
*/
async getAttributeSetType(
credential: VerifiableCredential,
schemaDocument: object | string | null = null,
): Promise<string> {
const vcType: string =
credential.type.find((type) => type !== "VerifiableCredential") || "";
// Schema-less credentials use first type (if any)
if (!credential.credentialSchema) return "$" + vcType;
const { document } = schemaDocument
? { document: schemaDocument }
: await this.documentLoader(credential.credentialSchema.id);
// Credentials with JsonSchemaCredential
if (credential.credentialSchema.type === "JsonSchemaCredential") {
// Determine if the credentialSchema document is signed
let schemaCredential: VerifiableCredential;
let schemaCredentialString: string | null = null;
if (typeof document === "string") {
schemaCredential = JSON.parse(document);
schemaCredentialString = document;
} else {
schemaCredential = document as VerifiableCredential;
}
if (
schemaCredential.type.indexOf("JsonSchemaCredential") == -1 ||
!schemaCredential.proof
) {
// Not a signed schema credential: calculate the sha2-256 hash only
// attributeSetType = {hash}${vcType}
const message = new TextEncoder().encode(
schemaCredentialString || JSON.stringify(schemaCredential),
);
return (await makeMultihash(message)) + "$" + vcType;
}
// attributeSetType = {issuer}${vcType}
const schemaCredentialIssuerId =
typeof schemaCredential.issuer === "string"
? schemaCredential.issuer
: schemaCredential.issuer.id;
return schemaCredentialIssuerId + "$" + vcType;
}
// Not using JsonSchemaCredential: calculate the sha2-256 hash only
let schema: JsonSchema_2020_12;
let schemaString: string | null = null;
if (typeof document === "string") {
schema = JSON.parse(document);
schemaString = document;
} else {
schema = document as JsonSchema_2020_12;
}
const message = new TextEncoder().encode(
schemaString || JSON.stringify(schema),
);
return (await makeMultihash(message)) + "$" + vcType;
}
}
async function makeMultihash(message: Uint8Array) {
const digest = await sha256.encode(message);
return base32.encode(new Uint8Array([0x12, 0x20, ...digest]));
}