UNPKG

@govtechsg/open-attestation

Version:
21 lines (20 loc) 1.08 kB
import { get, sortBy } from "lodash"; import { keccak256 } from "js-sha3"; /** * @deprecated will be removed in the next major release in favour of OpenAttestation v4.0 (more info: https://github.com/Open-Attestation/open-attestation/tree/alpha) */ export var digestCredential = function (document, salts, obfuscatedData) { // Prepare array of hashes from visible data var hashedUnhashedDataArray = salts // Explictly allow falsy values (e.g. false, 0, etc.) as they can exist in the document .filter(function (salt) { return get(document, salt.path) !== undefined; }) .map(function (salt) { var _a; return keccak256(JSON.stringify((_a = {}, _a[salt.path] = "".concat(salt.value, ":").concat(get(document, salt.path)), _a))); }); // Combine both array and sort them to ensure determinism var combinedHashes = obfuscatedData.concat(hashedUnhashedDataArray); var sortedHashes = sortBy(combinedHashes); // Finally, return the digest of the entire set of data return keccak256(JSON.stringify(sortedHashes)); };