/
common.js
36 lines (32 loc) · 970 Bytes
/
common.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
const jsonld = require("jsonld");
const crypto = require("crypto");
const canonize = async data => {
return jsonld.canonize(data);
};
const sha256 = data => {
const h = crypto.createHash("sha256");
h.update(data);
return h.digest("hex");
};
const createVerifyData = async (data, options) => {
const transformedOptions = {
...options,
"@context": "https://w3id.org/identity/v1"
};
delete transformedOptions["type"];
delete transformedOptions["id"];
delete transformedOptions["signatureValue"];
const canonizedOptions = await canonize(transformedOptions);
const optionsHash = await sha256(canonizedOptions);
const transformedData = { ...data };
delete transformedData["signature"];
const cannonidedData = await canonize(transformedData);
const documentHash = await sha256(cannonidedData);
const verifyData = `${optionsHash}${documentHash}`;
return verifyData;
};
module.exports = {
sha256,
canonize,
createVerifyData
};