Merkle Root of Bitcoin Block

This section describes the Merkle Root of a Bitcoin Block.

The Merkle Root of A Bitcoin block is the hash of all transactoin hashes.

For example, block # 500 of the Bitcoin Test Network has the following Merkle Root and transaction hashes:

```  ...
"height": 500,
"merkleroot": "dd3f288510dd3b632940bd3fb1db162d3ff99b19ddb0c586cfa3ac9a76d42517",
"tx": [
"a647d0c4112b4727f3c856782ff6bbaf099be929b27214a8e0dfedee4383eb68",
"24b8a4c788b8c805b810438ddd99e569e184ff20f4394ac49a6d832e69f57242",
"c5ffd70c3bc4998465cef55ed6d5d831ab3a550406423eb611117ed8ee41c278",
"4692772a73ea834c836915089acf97f2c790380a2b8fd32f82729da72545d8c5",
"82d6d88081e3e0eb36730f7f3aedb17228142b9e00a6dbaab4b53b798d0742c1",
"fc407d7a3b819daa5cf1ecc2c2a4b103c3782104d1425d170993bd534779a0da",
],
...
```

Here is the algorithm of how to calculate the Merkle Root:

• 1. Get the SHA256 hash of each transactions: H1, H2, H3, ..., Hn = tx1, tx2, tx3, ..., txn.
• 2. Group the hash list into groups of 2 sequentially. Clone the last hash, if the list has an odd number of hashes: (H1, H2), (H3, H4), ...
• 3. Generate a new hash for each group by running SHA256() function twice: H1 = SHA256(SHA256(H1+H2)), H2 = SHA256(SHA256(H3+H4)), ...
• 4. Repeat step 2 and 3 on the new hash list, until the list becomes 1 last hash: H1.
• 6. Return the last hash as the Merkle Root: merkleroot = H1

You can try the above algorithm to verify the Merkle Root on block # 500.

Last update: 2018.