UNPKG

immudb-node

Version:

Node.js SDK for immudb written in TypeScript

196 lines 7.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.verifyDualProof = exports.verifyInclusion = void 0; const util_1 = require("./util"); const consts_1 = require("./consts"); const verifyInclusionAHT = (inclusionProofList, i, j, iLeaf, jRoot) => { if (i > j || i === 0 || i < j && inclusionProofList.length === 0) { return false; } let i1 = i - 1; let j1 = j - 1; let ciRoot = iLeaf; for (let h of inclusionProofList) { let b = new Uint8Array(consts_1.NODE_PREFIX.length + ciRoot.length + h.length); b.set(consts_1.NODE_PREFIX); if (i1 % 2 === 0 && i1 !== j1) { b.set(ciRoot, consts_1.NODE_PREFIX.length); b.set(h, consts_1.NODE_PREFIX.length + ciRoot.length); } else { b.set(h, consts_1.NODE_PREFIX.length); b.set(ciRoot, consts_1.NODE_PREFIX.length + h.length); } ciRoot = util_1.hashUint8Array(b); i1 = i1 >> 1; j1 = j1 >> 1; } return util_1.equalArray(jRoot, ciRoot); }; const verifyConsistency = (consistencyProofList, i, j, iRoot, jRoot) => { if (i > j || i === 0 || (i < j && consistencyProofList.length === 0)) { return false; } if (i === j && consistencyProofList.length === 0) { return util_1.equalArray(iRoot, jRoot); } let fn = i - 1; let sn = j - 1; while (fn % 2 === 1) { fn = fn >> 1; sn = sn >> 1; } let ciRoot = consistencyProofList[0]; let cjRoot = consistencyProofList[0]; for (let h of consistencyProofList.slice(1)) { if (fn % 2 === 1 || fn === sn) { let b = new Uint8Array(consts_1.NODE_PREFIX.length + h.length + ciRoot.length); b.set(consts_1.NODE_PREFIX); b.set(h, consts_1.NODE_PREFIX.length); b.set(ciRoot, consts_1.NODE_PREFIX.length + h.length); ciRoot = util_1.hashUint8Array(b); b = new Uint8Array(consts_1.NODE_PREFIX.length + h.length + cjRoot.length); b.set(consts_1.NODE_PREFIX); b.set(h, consts_1.NODE_PREFIX.length); b.set(cjRoot, consts_1.NODE_PREFIX.length + h.length); cjRoot = util_1.hashUint8Array(b); while (fn % 2 === 0 && fn !== 0) { fn = fn >> 1; sn = sn >> 1; } } else { let b = new Uint8Array(consts_1.NODE_PREFIX.length + cjRoot.length + h.length); b.set(consts_1.NODE_PREFIX); b.set(cjRoot, consts_1.NODE_PREFIX.length); b.set(h, consts_1.NODE_PREFIX.length + cjRoot.length); cjRoot = util_1.hashUint8Array(b); } fn = fn >> 1; sn = sn >> 1; } return util_1.equalArray(iRoot, ciRoot) && util_1.equalArray(jRoot, cjRoot); }; const verifyLastInclusion = (lastInclusionproofList, i, leaf, root) => { if (i === 0) { return false; } let i1 = i - 1; let iRoot = leaf; for (let h of lastInclusionproofList) { let b = new Uint8Array(consts_1.NODE_PREFIX.length + h.length + iRoot.length); b.set(consts_1.NODE_PREFIX); b.set(h, consts_1.NODE_PREFIX.length); b.set(iRoot, consts_1.NODE_PREFIX.length + h.length); iRoot = util_1.hashUint8Array(b); i1 = i1 >> 1; } return util_1.equalArray(root, iRoot); }; const verifyLinearProof = (linearProof, sourceId, targetId, sourceAlh, targetAlh) => { if (linearProof === undefined) { return false; } const sourceTxId = linearProof.getSourcetxid(); const targetTxId = linearProof.getTargettxid(); const termsList = linearProof.getTermsList_asU8(); if (sourceId !== sourceTxId || targetId !== targetTxId) { return false; } if (sourceTxId === 0 || sourceTxId > targetTxId || termsList.length === 0 || !util_1.equalArray(sourceAlh, termsList[0])) { return false; } let calculatedAlh = termsList[0]; for (let i = 1; i < termsList.length; i++) { const txi = util_1.encodeInt64(linearProof.getSourcetxid() + i); const term = termsList[i]; const bs = new Uint8Array(txi.length + calculatedAlh.length + term.length); bs.set(txi); bs.set(calculatedAlh, txi.length); bs.set(term, txi.length + calculatedAlh.length); calculatedAlh = util_1.hashUint8Array(bs); } return util_1.equalArray(targetAlh, calculatedAlh); }; exports.verifyInclusion = (proof, digest, root) => { if (proof === undefined) { return false; } const leaf = util_1.withLeafPrefix(digest); let calcRoot = util_1.hashUint8Array(leaf); let i = proof.getLeaf(); let r = proof.getWidth() - 1; for (let t of proof.getTermsList_asU8()) { const b = new Uint8Array(consts_1.NODE_PREFIX.length + t.length + calcRoot.length); b.set(consts_1.NODE_PREFIX); if (i % 2 === 0 && i !== r) { b.set(calcRoot, consts_1.NODE_PREFIX.length); b.set(t, consts_1.NODE_PREFIX.length + calcRoot.length); } else { b.set(t, consts_1.NODE_PREFIX.length); b.set(calcRoot, consts_1.NODE_PREFIX.length + t.length); } calcRoot = util_1.hashUint8Array(b); i = Math.floor(i / 2); r = Math.floor(r / 2); } return i === r && util_1.equalArray(root, calcRoot); }; exports.verifyDualProof = (dualProof, sourceId, targetId, sourceAlh, targetAlh) => { if (dualProof === undefined) { return false; } const sourcetxmetadata = dualProof.getSourcetxmetadata(); const targettxmetadata = dualProof.getTargettxmetadata(); const inclusionproofList = dualProof.getInclusionproofList_asU8(); const consistencyproofList = dualProof.getConsistencyproofList_asU8(); const lastinclusionproofList = dualProof.getLastinclusionproofList_asU8(); const targetbltxalh = dualProof.getTargetbltxalh_asU8(); if (sourcetxmetadata === undefined || targettxmetadata === undefined) { return false; } const sId = sourcetxmetadata.getId(); const tId = targettxmetadata.getId(); const tPrevalh = util_1.getAlh(targettxmetadata); const tBltxid = targettxmetadata.getBltxid(); const tBlroot = targettxmetadata.getBlroot_asU8(); const sPrevalh = util_1.getAlh(sourcetxmetadata); const sBltxid = sourcetxmetadata.getBltxid(); const sBlroot = sourcetxmetadata.getBlroot_asU8(); if (sId !== sourceId || tId !== targetId) { return false; } if (sId === 0 || sId > tId) { return false; } if (!util_1.equalArray(sourceAlh, sPrevalh) || !util_1.equalArray(targetAlh, tPrevalh)) { return false; } if (sourceId < tBltxid && verifyInclusionAHT(inclusionproofList, sourceId, tBltxid, util_1.hashUint8Array(util_1.withLeafPrefix(sourceAlh)), tBlroot) === false) { return false; } if (sBltxid > 0 && verifyConsistency(consistencyproofList, sBltxid, tBltxid, sBlroot, tBlroot) === false) { return false; } if (tBltxid > 0 && verifyLastInclusion(lastinclusionproofList, tBltxid, util_1.hashUint8Array(util_1.withLeafPrefix(targetbltxalh)), tBlroot) === false) { return false; } const linearproof = dualProof.getLinearproof(); if (linearproof === undefined) { return false; } else { let ret; if (sourceId < tBltxid) { ret = verifyLinearProof(linearproof, tBltxid, targetId, targetbltxalh, targetAlh); } else { ret = verifyLinearProof(linearproof, sourceId, targetId, sourceAlh, targetAlh); } return ret; } }; //# sourceMappingURL=verification.js.map