<p>Most likely, you have some endian-ness issues. Remember that the merkle root published is in little-endian and so are the transaction hashes. You'll have to reverse them and then calculate merkle calculations.</p>

<p>You can take a look at the details of a block with the tx hashes here:</p>

<p><a href="https://webbtc.com/block/00000000000000000375c25e5af8f3b2310585ad6a7c264d66398018ce4c6a41.json" rel="nofollow">https://webbtc.com/block/00000000000000000375c25e5af8f3b2310585ad6a7c264d66398018ce4c6a41.json</a></p>

<p>There's a very helpful key in that json payload called "mrkl_tree" which tells you all nodes of the merkle tree (so the parent of two adjacent leaf nodes is in that set). It's probably easiest to build tests around that.</p>

CC BY-SA 3.0
2016-08-03T03:23:08.657Z
2016-08-03T03:23:08.657Z
Jimmy Song
<p>I've started playing with Bitcoin on a more technical level recently (I'm an experienced software developer but just a user of Bitcoin up until now). I'm trying to implement a function to calculate a merkle root, as described by the <a href="http://bitcoinfactswiki.github.io/Protocol_documentation/" rel="nofollow">protocol documentation here</a>. I think I have implemented a function as described, but I'm not too sure how to go about verifying what I've done. Ideally I'd like to write some unit tests, but I need some input values and an expect output.</p>

<p>Does anyone know where I might find a set of transaction hashes, along with the correct merkle root which should be produced from there? I'm really looking for example inputs &amp; output which I can use to verify the correctness of my function.</p>

<p>I've tried copying &amp; pasting the transaction hashes from <a href="https://blockchain.info/block-index/110851/000000000007b4e174a5dd07f1a785a5d0a52faaffd9d610ff65f64270ea7674" rel="nofollow">Bitcoin block 96001</a>, but the merkle root I'm calculating does not match what is displayed on blockchain.info. The strange thing is, when I feed the same transaction hashes into the <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/block.js#L137" rel="nofollow">merkle root function in BitcoinJS-lib</a>, it produces the same merkle root as my function. Are my function and BitcoinJS-lib both wrong? Is blockchain.info wrong? Or is there something else going on here?</p>

<pre><code>var sha256 = (buffer) =&gt; {
  return crypto.createHash('sha256')
    .update(buffer)
    .digest()
}

var doubleSha256 = (buffer) =&gt; sha256(sha256(buffer))

var merkleRoot = (hashes) =&gt; {
  if (hashes.length == 1)
    return hashes[0]

  return merkleRoot(
    _.chain(hashes)
      .chunk(2)
      .each(tuple =&gt; tuple[1] = tuple[1] || tuple[0])
      .map(tuple =&gt; Buffer.concat(tuple))
      .map(doubleSha256)
      .value()
  )
}

// Transaction hashes from block 96001:
var tx1 = Buffer.from('dcc95c8740525e27d87333558e5d0a288d0eac9062598c86c77e75dcde7178d2', 'hex')
var tx2 = Buffer.from('043cd1d9166cf16a4cc02d4385667657056cbe23f7058877c7fde09ef7de904a', 'hex')
var tx3 = Buffer.from('88b058d7e3e7b6d174d0f5a76a209a3107de65c01b010cda72ee64b22c980998', 'hex')

console.log(merkleRoot([tx1, tx2, tx3].sort()).toString('hex'));

// expected 'e7ae478c684e965d730e72b9cac644fd1ebeb1517fe69cc458d9d4070b9de8d8'
// output   'b2f52e4efbbab7653489b83d5ad331f1ada1b7e5959ac63b8aa45a786ae90279'
</code></pre>

<development><merkle-tree><javascript>
How can I verify my merkle root function is correct?
MattDavey
# How can I verify my merkle root function is correct?

(Asked by: username_0 on 2016-08-02)

I've started playing with Bitcoin on a more technical level recently (I'm an experienced software developer but just a user of Bitcoin up until now). I'm trying to implement a function to calculate a merkle root, as described by the [protocol documentation here](http://bitcoinfactswiki.github.io/Protocol_documentation/). I think I have implemented a function as described, but I'm not too sure how to go about verifying what I've done. Ideally I'd like to write some unit tests, but I need some input values and an expect output.

Does anyone know where I might find a set of transaction hashes, along with the correct merkle root which should be produced from there? I'm really looking for example inputs & output which I can use to verify the correctness of my function.

I've tried copying & pasting the transaction hashes from [Bitcoin block 96001](https://blockchain.info/block-index/110851/000000000007b4e174a5dd07f1a785a5d0a52faaffd9d610ff65f64270ea7674), but the merkle root I'm calculating does not match what is displayed on blockchain.info. The strange thing is, when I feed the same transaction hashes into the [merkle root function in BitcoinJS-lib](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/src/block.js#L137), it produces the same merkle root as my function. Are my function and BitcoinJS-lib both wrong? Is blockchain.info wrong? Or is there something else going on here?

```
var sha256 = (buffer) => {
  return crypto.createHash('sha256')
    .update(buffer)
    .digest()
}

var doubleSha256 = (buffer) => sha256(sha256(buffer))

var merkleRoot = (hashes) => {
  if (hashes.length == 1)
    return hashes[0]

  return merkleRoot(
    _.chain(hashes)
      .chunk(2)
      .each(tuple => tuple[1] = tuple[1] || tuple[0])
      .map(tuple => Buffer.concat(tuple))
      .map(doubleSha256)
      .value()
  )
}

// Transaction hashes from block 96001:
var tx1 = Buffer.from('dcc95c8740525e27d87333558e5d0a288d0eac9062598c86c77e75dcde7178d2', 'hex')
var tx2 = Buffer.from('043cd1d9166cf16a4cc02d4385667657056cbe23f7058877c7fde09ef7de904a', 'hex')
var tx3 = Buffer.from('88b058d7e3e7b6d174d0f5a76a209a3107de65c01b010cda72ee64b22c980998', 'hex')

console.log(merkleRoot([tx1, tx2, tx3].sort()).toString('hex'));

// expected 'e7ae478c684e965d730e72b9cac644fd1ebeb1517fe69cc458d9d4070b9de8d8'
// output   'b2f52e4efbbab7653489b83d5ad331f1ada1b7e5959ac63b8aa45a786ae90279'

```

**Tags:** development, merkle-tree, javascript


---

## 1 answers

### Answer by username_1 on 2016-08-03. Score: 3 | Accepted answer

Most likely, you have some endian-ness issues. Remember that the merkle root published is in little-endian and so are the transaction hashes. You'll have to reverse them and then calculate merkle calculations.

You can take a look at the details of a block with the tx hashes here:

<https://webbtc.com/block/00000000000000000375c25e5af8f3b2310585ad6a7c264d66398018ce4c6a41.json>

There's a very helpful key in that json payload called "mrkl\_tree" which tells you all nodes of the merkle tree (so the parent of two adjacent leaf nodes is in that set). It's probably easiest to build tests around that.


---

