UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

85 lines 3.43 kB
import assert from "assert"; import { hash256 } from "../../../../src/encryption/encryption"; import { calculateRoot, merklePath, validateMerklePath } from "../../../../src/merkle/merkleHelper"; function merkleRoot(stringList) { return calculateRoot(hashList(stringList)); } function stringToHash(string) { return hash256(Buffer.from(string)); } function hashList(stringList) { return stringList.map(stringToHash); } function checkDifferent(list1, list2) { const root1 = merkleRoot(list1); const root2 = merkleRoot(list2); expect(root1.equals(root2)).toBeFalsy(); } function testPath(stringList, stringToProve) { const path = merklePath(hashList(stringList), stringToHash(stringToProve)); expect(validateMerklePath(path, stringToHash(stringToProve), merkleRoot(stringList))).toBeTruthy(); } const a = ["a"]; const aa = ["a", "a"]; const abcde = ["a", "b", "c", "d", "e"]; const abcdee = ["a", "b", "c", "d", "e", "e"]; const abcdef = ["a", "b", "c", "d", "e", "f"]; const abcdefef = ["a", "b", "c", "d", "e", "f", "e", "f"]; describe("MerkleHelper", function () { it("merkle root of empty list is 32 0s", () => { expect(merkleRoot([]).equals(Buffer.alloc(32))).toBeTruthy(); }); it("merkle root of single element", () => { expect(merkleRoot(a).equals(stringToHash("a"))).toBeTruthy(); }); it("merkle root collisions", function () { checkDifferent(a, aa); checkDifferent(abcde, abcdee); checkDifferent(abcdef, abcdefef); }); it("merkle proof throws on empty list", () => { assert.throws(() => { merklePath([], stringToHash("a")); }, Error); }); it("merkle proof throws if tx not exist in list", () => { assert.throws(() => { merklePath(hashList(abcdee), stringToHash("f")); }, Error); }); it("merkle proof structure", () => { const path = merklePath(hashList(abcde), stringToHash("e")); assert.equal(path.length, 3); assert.equal(path[0].side, 1); // right assert.equal(path[1].side, 1); // right assert.equal(path[2].side, 0); // left expect(validateMerklePath(path, stringToHash("e"), merkleRoot(abcde))).toBeTruthy(); expect(validateMerklePath(path, stringToHash("c"), merkleRoot(abcde))).toBeFalsy(); }); it("merkle proof structure fails without hashes", () => { assert.throws(() => merklePath([], stringToHash("e")), Error); }); it("merkle path to size 20", () => { const txs = []; for (let i = 1; i < 20; i++) { txs.push(`${i}`); for (let j = 1; j <= i; j++) { testPath(txs, `${j}`); } } }, 400000); it("dummy", () => { testPath(["1", "2", "3"], "1"); }); it("merkle path negative test wrong side", () => { const path = merklePath(hashList(abcde), stringToHash("d")); path[1].side = 1; // Flip side of one path component expect(validateMerklePath(path, stringToHash("d"), merkleRoot(abcde))).toBeFalsy(); }); it("merkle path negative test wrong hash", () => { const path = merklePath(hashList(abcde), stringToHash("d")); path[2].hash = stringToHash("invalid"); // wrong hash expect(validateMerklePath(path, stringToHash("d"), merkleRoot(abcde))).toBeFalsy(); }); }); //# sourceMappingURL=merkleHelperTest.js.map