UNPKG

jsonld-signatures-merkleproof2019

Version:

A jsonld signature implementation to support MerkleProof2019 verification in Verifiable Credential context

41 lines (40 loc) 1.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = ensureValidReceipt; const sha256_1 = __importDefault(require("sha256")); const VerifierError_1 = __importDefault(require("../models/VerifierError")); const data_1 = require("../utils/data"); const getText_1 = __importDefault(require("../helpers/getText")); function ensureValidReceipt(receipt) { let proofHash = receipt.targetHash; const merkleRoot = receipt.merkleRoot; try { const proof = receipt.path; if (proof && proofHash) { for (const index in proof) { const node = proof[index]; let appendedBuffer; if (typeof node.left !== 'undefined') { appendedBuffer = (0, data_1.toByteArray)(`${node.left}${proofHash}`); proofHash = (0, sha256_1.default)(appendedBuffer); } else if (typeof node.right !== 'undefined') { appendedBuffer = (0, data_1.toByteArray)(`${proofHash}${node.right}`); proofHash = (0, sha256_1.default)(appendedBuffer); } else { throw new VerifierError_1.default('checkReceipt', 'Trigger catch error.'); } } } } catch (e) { throw new VerifierError_1.default('checkReceipt', (0, getText_1.default)('errors', 'ensureValidReceipt')); } if (proofHash !== merkleRoot) { throw new VerifierError_1.default('checkReceipt', (0, getText_1.default)('errors', 'invalidMerkleReceipt')); } }