Compare leaf hash to root if no proof array. Closes #25

This commit is contained in:
Miguel Mota 2020-07-31 22:52:38 -07:00
parent d78ea67599
commit a246764d6b
3 changed files with 20 additions and 4 deletions

View File

@ -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
}

View File

@ -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",

View File

@ -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)