merkle-tree

merkletreejs method getHexProof doesn't work?


I am trying test merkle proof with merkletreejs library and I can't figure out why this works

const tree = new MerkleTree(leaves, SHA256)
const root = tree.getHexRoot()
const leaf = SHA256('a')
const proof = tree.getProof(leaf) // РАБОТАЕТ
console.log(tree.verify(proof, leaf, root)) // true

But this not ?

const tree = new MerkleTree(leaves, SHA256)
const root = tree.getHexRoot()
const leaf = SHA256('a')
const proof = tree.getHexProof(leaf)
console.log(tree.verify(proof, leaf, root)) // false

Solution

  • It seems it needs some extra code in order to work. This code works:

    const { MerkleTree } = require('merkletreejs')
    const sha1 = require('crypto-js/sha1')
    
    const leaves = [
        'd89f84d948796605a413e196f40bce1d6294175d',
        '32f04c7f572bf75a266268c6f4d8c92731dc3b7f',
        'b80b52d80f5fe940ac2c987044bc439e4218ac94',
        '1553c75a1d637961827f4904a0955e57915d8310'
      ]
    
      const tree = new MerkleTree(leaves, sha1, {
        sortLeaves: true,
        sortPairs: true
      })
    
      const root = tree.getHexRoot()
      const leaf = 'b80b52d80f5fe940ac2c987044bc439e4218ac94'
      const proof = tree.getHexProof(leaf)
    
      
    
      console.log(tree.verify(proof, leaf, root))