From a246764d6b7e7082f1b00dad17c657cc886a7824 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Fri, 31 Jul 2020 22:52:38 -0700 Subject: [PATCH] Compare leaf hash to root if no proof array. Closes #25 --- index.ts | 7 ++++--- package.json | 2 +- test/merkletree.test.js | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 32be220..f0a57e7 100644 --- a/index.ts +++ b/index.ts @@ -558,10 +558,11 @@ export class MerkleTree { let hash = this._bufferify(targetNode) root = this._bufferify(root) - if (!Array.isArray(proof) || - !proof.length || + if ( + !Array.isArray(proof) || !targetNode || - !root) { + !root + ) { return false } diff --git a/package.json b/package.json index b0ad5d7..c30532f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "merkletreejs", - "version": "0.2.8", + "version": "0.2.9", "description": "Construct Merkle Trees and verify proofs", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/test/merkletree.test.js b/test/merkletree.test.js index 2307f5d..fcb9564 100644 --- a/test/merkletree.test.js +++ b/test/merkletree.test.js @@ -488,6 +488,21 @@ test('crypto-js SHA3 leaves SHA256 hash algo', t => { t.equal(verifications.every(Boolean), true) }) +test('crypto-js SHA3 1 leaf SHA256 hash algo', t => { + t.plan(4) + + const leaves = ['a'].map(SHA3) + const tree = new MerkleTree(leaves, SHA256) + t.deepEqual(tree.getLeaves(), leaves.map(MerkleTree.bufferify)) + const root = tree.getRoot() + + const leaf = leaves[0] + const proof = tree.getProof(leaf) + t.equal(proof.length, 0) + t.equal(MerkleTree.bufferify(leaf).toString('hex'), root.toString('hex')) + t.equal(tree.verify(proof, leaf, root), true) +}) + test('crypto-js bufferify', t => { t.plan(1)