forked from Open-Attestation/open-attestation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.ts
24 lines (21 loc) · 871 Bytes
/
digest.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
import { get, sortBy } from "lodash";
import { keccak256 } from "js-sha3";
import { Salt } from "./types";
import { TradeTrustDocument } from "../../__generated__/tt-schema.4.0";
export const digestCredential = (
document: Omit<TradeTrustDocument, "proof">,
salts: Salt[],
obfuscatedData: string[]
) => {
// Prepare array of hashes from visible data
const hashedUnhashedDataArray = salts
.filter((salt) => get(document, salt.path))
.map((salt) => {
return keccak256(JSON.stringify({ [salt.path]: `${salt.value}:${get(document, salt.path)}` }));
});
// Combine both array and sort them to ensure determinism
const combinedHashes = obfuscatedData.concat(hashedUnhashedDataArray);
const sortedHashes = sortBy(combinedHashes);
// Finally, return the digest of the entire set of data
return keccak256(JSON.stringify(sortedHashes));
};