/
vc.ts
668 lines (588 loc) · 20.8 KB
/
vc.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
Copyright (C) 2018-present evan GmbH.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License, version 3,
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see http://www.gnu.org/licenses/ or
write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA, 02110-1301 USA, or download the license from
the following URL: https://evan.network/license/
*/
import * as didJWT from 'did-jwt';
import { cloneDeep } from 'lodash';
import {
nullAddress,
nullBytes32,
getEnvironment,
} from '../common/utils';
import {
AccountStore,
ContractLoader,
DfsInterface,
Did,
Executor,
Logger,
LoggerOptions,
NameResolver,
SignerIdentity,
Verifications,
} from '../index';
import { CryptoProvider } from '../encryption/crypto-provider';
/**
* custom configuration for VC resolver
*/
export interface VcConfig {
credentialStatusEndpoint: string;
}
/**
* A VC's credential status property
*/
export interface VcCredentialStatus {
id: string;
type: string;
}
/**
* Information about a VC's subject
*/
export interface VcCredentialSubject {
id: string;
data?: any;
description?: any;
uri?: string;
name?: string;
}
/**
* (Optional) Payload for a VC credential subject
*/
export interface VcCredentialSubjectPayload {
name: string;
value: string;
}
/**
* A valid VC document
*/
export interface VcDocument {
'@context': string[];
id: string;
type: string[];
issuer: VcIssuer;
validFrom: string;
validUntil?: string;
credentialSubject: VcCredentialSubject;
credentialStatus?: VcCredentialStatus;
proof?: VcProof;
}
/**
* Template for a VC that will be converted into a valid VC by the resolver
*/
export interface VcDocumentTemplate {
'@context'?: string[];
id?: string;
type?: string[];
issuer: VcIssuer;
validFrom: string;
validUntil?: string;
credentialSubject: VcCredentialSubject;
credentialStatus?: VcCredentialStatus;
}
/**
* The details of encryption/decryption of VC documents
*/
export interface VcEncryptionInfo {
key?: string;
}
/**
* The parts an VC ID in evan is made of
*/
export interface VcIdSections {
environment: string;
internalId: string;
}
/**
* Issuer of a VC
*/
export interface VcIssuer {
id: string;
name?: string;
}
/**
* Options for VC resolver
*/
export interface VcOptions extends LoggerOptions {
accountStore: AccountStore;
contractLoader: ContractLoader;
dfs: DfsInterface;
did: Did;
executor: Executor;
nameResolver: NameResolver;
signerIdentity: SignerIdentity;
verifications: Verifications;
web3: any;
cryptoProvider: CryptoProvider;
}
/**
* The proof for a VC, containing the JWS and metadata
*/
export interface VcProof {
type: string;
created: string;
proofPurpose: string;
verificationMethod: string;
jws: string;
}
/**
* Holds a list of supported proof types for VC (JWS) proofs
*/
export const enum VcProofType {
EcdsaPublicKeySecp256k1 = 'EcdsaPublicKeySecp256k1',
}
const JWTProofMapping = {};
JWTProofMapping[(VcProofType.EcdsaPublicKeySecp256k1)] = 'ES256K-R';
const vcRegEx = /^vc:evan:(?:(testcore|core):)?(0x(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64}))$/;
const w3cMandatoryContext = 'https://www.w3.org/2018/credentials/v1';
/**
* Module for storing VCs in and retrieving VCs from the VC registry
*
* @class Vc
*/
export class Vc extends Logger {
public credentialStatusEndpoint: string;
private cache: any = {};
private config: VcConfig;
private options: VcOptions;
/**
* Creates a new `Vc` instance
*
* @param {VcOptions} options options for `Vc`
* @param {Did} did Instance of `Did` used for resolving DIDs
*/
public constructor(options: VcOptions, config: VcConfig) {
super(options as LoggerOptions);
this.options = options;
this.credentialStatusEndpoint = config.credentialStatusEndpoint;
}
/**
* Associates the active identity with a new ID in the registry to store a VC at.
*
* @returns {Promise<string>} The reserved ID.
*/
public async createId(): Promise<string> {
const id = await this.options.executor.executeContractTransaction(
await this.getRegistryContract(),
'createId', {
from: this.options.signerIdentity.activeIdentity,
event: { target: 'VcRegistry', eventName: 'VcIdRegistered' },
getEventResult: (event, args) => args.vcId,
},
);
return this.convertInternalVcIdToUri(id);
}
/**
* Creates a new VC document from a template.
*
* @param {VcDocumentTemplate} vcData Template for the VC document containing the relevant
* data.
* @returns {Promise<VcDocument} The final VC document as it is stored in the registry.
*/
public async createVc(vcData: VcDocumentTemplate): Promise<VcDocument> {
if (!vcData.id) {
throw new Error('VC misses id');
}
const types = vcData.type ? vcData.type : ['VerifiableCredential'];
const context = vcData['@context'] ? vcData['@context'] : [w3cMandatoryContext];
if (!context.includes(w3cMandatoryContext)) {
context.push(w3cMandatoryContext);
}
const vcDocument: VcDocument = {
'@context': context,
type: types,
...(vcData as VcDocument),
};
vcDocument.proof = await this.createProofForVc(vcDocument);
await this.validateVcDocument(vcDocument);
return vcDocument;
}
/**
* Returns a key.
*
* @returns {Promise<any} it returns a key for encrypting data which can
* be passed to `storeVc`.
*/
public async generateKey(): Promise<any> {
const cryptor = this.options.cryptoProvider.getCryptorByCryptoAlgo('aes');
return cryptor.generateKey();
}
/**
* get the Revoke status of a given VC document
*
* @param {string} vcId The registry ID the VC document is associated with.
* @return {revokationStatus} A boolean value. False = not revoked, True = revoked
*/
public async getRevokeVcStatus(vcId: string): Promise<void> {
const environment = await this.getEnvironment();
const vcIdHash = vcId.replace(`vc:evan:${environment}:`, '');
// `vcId` usually comes from a VC document, so non-existing VCs can be considered an edge case,
// running both requests in parallel to improve performance
const [revoked] = await Promise.all([
this.options.executor.executeContractCall(
await this.getRegistryContract(),
'vcRevoke',
vcIdHash,
),
(async () => {
const owned = await this.options.executor.executeContractCall(
await this.getRegistryContract(),
'vcOwner',
vcIdHash,
);
if (owned === nullAddress) {
throw new Error(`Given "${vcId}" is not a valid evan VC`);
}
})(),
]);
return revoked;
}
/**
* Returns a VC document for a given ID.
*
* @param {string} vcId The registry ID the VC document
* is associated with.
* @param {VcEncryptionInfo} encryptionInfo The encryption information for decryption.
* @returns {Promise<VcDocument} A VC document stored in the registry.
* @throws If an invalid VC ID is given or no document is registered under this ID or
* wrong decryption key is provided.
*/
public async getVc(vcId: string, encryptionInfo?: VcEncryptionInfo): Promise<VcDocument> {
// Check whether the full URI (vc:evan:[vcId]) or just the internal ID was given
let identityAddress = vcId;
if (!identityAddress.startsWith('0x')) {
const groups = vcRegEx.exec(vcId);
if (!groups) {
throw new Error(`Given VC ID ("${vcId}") is no valid evan VC ID`);
}
const [, vcEnvironment = 'core', address] = groups;
identityAddress = address;
const environment = await this.getEnvironment();
if ((environment === 'testcore' && vcEnvironment !== 'testcore')
|| (environment === 'core' && vcEnvironment !== 'core')) {
throw new Error(`Given VC ID environment "${vcEnvironment}" does not match current "${environment}"`);
}
}
const vcDfsHash = await this.options.executor.executeContractCall(
await this.getRegistryContract(),
'vcStore',
identityAddress,
);
if (vcDfsHash === nullBytes32) {
throw Error(`VC for address ${vcId} does not exist`);
}
let document = JSON.parse(await this.options.dfs.get(vcDfsHash) as any);
if (!encryptionInfo) {
await this.validateProof(document as VcDocument);
return document;
}
const cryptor = this.options.cryptoProvider.getCryptorByCryptoAlgo('aes');
document = Buffer.from(document.data);
try {
const decryptedDocument = await cryptor.decrypt(document, { key: encryptionInfo.key });
await this.validateProof(decryptedDocument);
return decryptedDocument;
} catch {
throw new Error('Incorrect key: decryption failed');
}
}
/**
* Revokes a given VC document
*
* @param {string} vcId The registry ID the VC document is associated with.
* @return {Promise<void>} resolved when done
*/
public async revokeVc(vcId: string): Promise<void> {
const environment = await this.getEnvironment();
const vcIdHash = vcId.replace(`vc:evan:${environment}:`, '');
await this.validateVcIdOwnership(vcIdHash);
await this.options.executor.executeContractTransaction(
await this.getRegistryContract(),
'revokeVC',
{ from: this.options.signerIdentity.activeIdentity },
vcIdHash,
);
}
/**
* Stores the given VC document in the registry under the provided ID.
* The ID has to be a valid and registered VC ID.
* Creates a proof if none is given or validates it if one is given.
*
* @param {VcDocumentTemplate} vcData Template for the VC document containing
* the relevant data.
* @param {VcEncryptionInfo} encryptionInfo The encryption information for encryption.
* @returns {Promise<VcDocument} The final VC document as it
* is stored in the registry.
*/
public async storeVc(vcData: VcDocumentTemplate, encryptionInfo?: VcEncryptionInfo):
Promise<VcDocument> {
const dataTemplate: VcDocumentTemplate = cloneDeep(vcData);
let internalId;
if (!dataTemplate.id) {
dataTemplate.id = await this.createId();
internalId = (await this.validateVcIdAndGetSections(dataTemplate.id)).internalId;
} else {
// We prefix the ID specified in the document with
// the evan identifier (vc:evan:[core|testcore]:)
// However, we only need the actual ID to address the registry
const sections = await this.validateVcIdAndGetSections(dataTemplate.id);
internalId = sections.internalId;
// Is the given VC ID valid and the active identity the owner of the VC ID?
await this.validateVcIdOwnership(internalId);
}
dataTemplate.credentialStatus = {
id: `${this.credentialStatusEndpoint}${dataTemplate.id}`,
type: 'evan:evanCredential',
};
const documentToStore = await this.createVc(dataTemplate);
let vcDfsAddress;
if (!encryptionInfo) {
vcDfsAddress = await this.options.dfs.add('vc',
Buffer.from(JSON.stringify(documentToStore), 'utf-8'));
} else {
const cryptor = this.options.cryptoProvider.getCryptorByCryptoAlgo('aes');
const encryptedDocumentToStore = await cryptor.encrypt(documentToStore,
{ key: encryptionInfo.key });
vcDfsAddress = await this.options.dfs.add('vc',
Buffer.from(JSON.stringify(encryptedDocumentToStore), 'utf-8'));
}
await this.options.executor.executeContractTransaction(
await this.getRegistryContract(),
'setVc',
{ from: this.options.signerIdentity.activeIdentity },
internalId,
vcDfsAddress,
);
return documentToStore;
}
/**
* Converts an interal VC ID (0x...) to a URI (vc:evan:...)
*
* @param internalVcId Internal 32bytes ID
* @returns The VC's URI
*/
private async convertInternalVcIdToUri(internalVcId: string): Promise<string> {
const environment = await this.getEnvironment();
return `vc:evan:${environment}:${internalVcId}`;
}
/**
* Create a JWT over a VC document
*
* @param {VcDocument} vc The VC document
* @param {VcProofType} proofType The type of algorithm used for generating the JWT
*/
private async createJwtForVc(vc: VcDocument, proofType: VcProofType): Promise<string> {
const signer = didJWT.SimpleSigner(
await this.options.accountStore.getPrivateKey(this.options.signerIdentity.underlyingAccount),
);
const jwt = await didJWT.createJWT(
{
vc,
exp: vc.validUntil,
}, {
alg: JWTProofMapping[proofType],
issuer: vc.issuer.id,
signer,
},
);
return jwt;
}
/**
* Creates a new `VcProof` object for a given VC document, including generating a JWT token over
* the whole document.
*
* @param {VcDocument} vc The VC document to create the proof for.
* @param {VcProofType} proofType Specify if you want a proof type different from the
* default one.
* @returns {VcProof} A proof object containing a JWT.
* @throws If the VC issuer identity and the signer identity differ from each other
*/
private async createProofForVc(vc: VcDocument,
proofType: VcProofType = VcProofType.EcdsaPublicKeySecp256k1): Promise<VcProof> {
let issuerIdentity;
try {
issuerIdentity = await this.options.did.convertDidToIdentity(vc.issuer.id);
} catch (e) {
throw Error(`Invalid issuer DID: ${vc.issuer.id}`);
}
if (this.options.signerIdentity.activeIdentity !== issuerIdentity) {
throw Error('You are not authorized to issue this VC');
}
const jwt = await this.createJwtForVc(vc, proofType);
const verMethod = await this.getPublicKeyUriFromDid(vc.issuer.id);
const proof: VcProof = {
type: `${proofType}`,
created: new Date(Date.now()).toISOString(),
proofPurpose: 'assertionMethod',
verificationMethod: verMethod,
jws: jwt,
};
return proof;
}
/**
* Get current environment ('testcore:' || 'core'). Result is cached.
*
* @returns {Promise<string>} current environment
*/
private async getEnvironment(): Promise<string> {
if (!this.cache.environment) {
this.cache.environment = await getEnvironment(this.options.web3);
}
return this.cache.environment;
}
/**
* Retrieves the ID of the public key of an VC's issuer's DID document that matches the active
* identity's public key.
*
* @param {string} issuerDid DID of the VC issuer.
* @throws If there is no authentication material given in the DID or no key matching
* the active identity is found.
*/
private async getPublicKeyUriFromDid(issuerDid: string): Promise<string> {
const doc = await this.options.did.getDidDocument(issuerDid);
if (!(doc.authentication || doc.publicKey || doc.publicKey.length === 0)) {
throw Error(`Document for ${issuerDid}`
+ 'does not provide authentication material. Cannot sign VC.');
}
let keyId;
const signaturePublicKey = (await this.options.signerIdentity.getPublicKey(
this.options.signerIdentity.underlyingAccount,
)).toLocaleLowerCase();
const account = this.options.signerIdentity.underlyingAccount.toLocaleLowerCase();
for (const entry of doc.publicKey) {
if (('ethereumAddress' in entry && entry.ethereumAddress.toLocaleLowerCase() === account)
|| ('publicKeyHex' in entry && entry.publicKeyHex.toLocaleLowerCase() === signaturePublicKey)) {
keyId = entry.id;
break;
}
}
if (!keyId) {
throw Error('The signature key of the active account is not associated to its DID document. Cannot sign VC.');
}
return keyId;
}
/**
* Get web3 contract instance for VC registry contract via ENS. Result is cached.
*
* @returns {Promise<any>} VC registry contract
*/
private async getRegistryContract(): Promise<any> {
if (!this.cache.vcRegistryContract) {
const vcRegistryDomain = this.options.nameResolver.getDomainName(
this.options.nameResolver.config.domains.vcRegistry,
);
const vcRegistryAddress = await this.options.nameResolver.getAddress(vcRegistryDomain);
this.cache.vcRegistryContract = this.options.contractLoader.loadContract(
'VcRegistry', vcRegistryAddress,
);
}
return this.cache.vcRegistryContract;
}
/**
* Validates the JWS of a VC Document proof
*
* @param {VcDocument} document The VC Document
* @returns {Promise<void>} Resolves when done
*/
private async validateProof(document: VcDocument): Promise<void> {
// Mock the did-resolver package that did-jwt usually requires
const didResolver = this.options.did;
const resolver = {
async resolve() {
const doc = await didResolver.getDidDocument(document.issuer.id);
return doc as any;
},
};
// fails if invalid signature
const verifiedSignature = await didJWT.verifyJWT(document.proof.jws, { resolver });
// fails if signed payload and the VC differ
const payload = {
...verifiedSignature.payload.vc,
};
delete payload.proof;
const prooflessDocument = {
...document,
};
delete prooflessDocument.proof;
const proofPayloadHash = await this.options.nameResolver.soliditySha3(JSON.stringify(payload));
const documentHash = await this.options.nameResolver.soliditySha3(
JSON.stringify(prooflessDocument),
);
if (proofPayloadHash !== documentHash) {
throw Error('Invalid proof. Signed payload does not match given document.');
}
}
/**
* Checks various criteria a VC document has to meet
*
* @param {VcDocument} document The VC document to check.
* @returns {Promise<void>} If the checks are succesfull.
* @throws If any of the criteria is not met.
*/
private async validateVcDocument(document: VcDocument): Promise<void> {
// Subject
if (!document.credentialSubject.id || document.credentialSubject.id === '') {
throw new Error('No Subject ID provided');
}
await this.options.did.validateDid(document.credentialSubject.id);
// Issuer
if (!document.issuer.id || document.issuer.id === '') {
throw new Error('No Issuer ID provided');
}
await this.options.did.validateDid(document.issuer.id);
// Proof
if (!document.proof || !document.proof.jws || document.proof.jws === '') {
throw new Error('VC misses proof');
} else if (!document.proof.type) {
throw new Error('VC proof misses type');
}
await this.validateProof(document);
}
/**
* Validates whether a given ID is a valid evan VC ID and returns
* its sections (environment and internal ID)
*
* @param {string} vcId VC ID
* @returns {VcIdSections} Sections of the ID
*/
private async validateVcIdAndGetSections(vcId: string): Promise<VcIdSections> {
const groups = vcRegEx.exec(vcId);
if (!groups) {
throw new Error(`Given VC ID ("${vcId}") is no valid evan VC ID`);
}
const [, vcEnvironment = 'core', internalId] = groups;
const environment = await this.getEnvironment();
if ((environment === 'testcore' && vcEnvironment !== 'testcore')
|| (environment === 'core' && vcEnvironment !== 'core')) {
throw new Error(`VCs environment "${environment} does not match ${vcEnvironment}`);
}
return { environment: vcEnvironment, internalId };
}
/**
* Checks if the given VC ID is associated to the active identity at the VC registry
*
* @param {string} vcId VC ID registered at the VC registry
* @returns {Promise<void>} Resolves when successful
* @throws {Error} If the ID is not valid or the identity is not the ID owner
*/
private async validateVcIdOwnership(vcId: string): Promise<void> {
const ownerAddress = await this.options.executor.executeContractCall(
await this.getRegistryContract(),
'vcOwner',
vcId,
);
if (this.options.signerIdentity.activeIdentity !== ownerAddress) {
throw Error(`Active identity is not the owner of the given VC ID ${await this.convertInternalVcIdToUri(vcId)}`);
}
}
}