-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.js
97 lines (89 loc) · 3.45 KB
/
utils.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
var fs = require('file-system');
const {AssertionProofPurpose} = require("./lib/jsonld-signatures/purposes/AssertionProofPurpose");
const {PublicKeyProofPurpose} = require("./lib/jsonld-signatures/purposes/PublicKeyProofPurpose");
const {RsaSignature2018} = require("./lib/jsonld-signatures/suites/rsa2018/RsaSignature2018");
const {Ed25519Signature2018} = require("./lib/jsonld-signatures/suites/ed255192018/Ed25519Signature2018");
const {Resolver} = require("did-resolver");
const web = require("web-did-resolver");
const {Ed25519Signature2020} = require("@digitalbazaar/ed25519-signature-2020");
const {Ed25519VerificationKey2020} = require("@digitalcredentials/ed25519-verification-key-2020");
const readFile = (vcFilePath) => {
const filesRead = fs.readFileSync(vcFilePath);
return JSON.parse(filesRead.toString());
}
const ProofType = {
ED25519_Signature_2018: 'Ed25519Signature2018',
ED25519_Signature_2020: 'Ed25519Signature2020',
RSA_Signature_2018: 'RsaSignature2018',
};
const ProofPurpose = {
Assertion: 'assertionMethod',
PublicKey: 'publicKey',
};
const documentLoader = async url => {
if (url.startsWith('did:web')) {
const webResolver = web.getResolver();
const resolver = new Resolver({...webResolver});
const document = await resolver.resolve(url);
return {
contextUrl: null,
documentUrl: url,
document: document?.didDocument,
};
}
if (url.startsWith('https://')) {
const response = await fetch(url, "GET");
const json = await response.json();
return {
contextUrl: null,
documentUrl: url,
document: json,
};
}
return jsonld.documentLoaders.xhr(url);
};
const getProofPurpose = (verifiableCredential) => {
let purpose ;
switch (verifiableCredential.proof.proofPurpose) {
case ProofPurpose.Assertion:
purpose = new AssertionProofPurpose()
break;
case ProofPurpose.PublicKey:
purpose = new PublicKeyProofPurpose()
break;
}
return purpose;
}
const getSuite = async(verifiableCredential) => {
let suite ;
switch (verifiableCredential.proof.type) {
case ProofType.RSA_Signature_2018: {
const suiteOptions = {
verificationMethod: verifiableCredential.proof.verificationMethod,
date: verifiableCredential.proof.created,
};
suite = new RsaSignature2018(suiteOptions);
break;
}
case ProofType.ED25519_Signature_2018: {
const suiteOptions = {
verificationMethod: verifiableCredential.proof.verificationMethod,
date: verifiableCredential.proof.created,
};
suite = new Ed25519Signature2018(suiteOptions);
break;
}
case ProofType.ED25519_Signature_2020: {
let verificationMethod = verifiableCredential.proof.verificationMethod;
const {document} = await documentLoader(verificationMethod);
const suiteOptions = {
key: await Ed25519VerificationKey2020.from(document.verificationMethod?.find(d => d.id === verificationMethod)),
date: verifiableCredential.proof.created,
};
suite = new Ed25519Signature2020(suiteOptions);
break;
}
}
return suite;
}
module.exports = {readFile, getProofPurpose, getSuite, documentLoader}