I'm using merkle.cpp code (which uses the libbitcoin library for some helper functions) from Mastering Bitcoin book.

I was trying to re-create Merkleroot hash for Bitcoin block #100000 containing 4 transactions:

 "tx": [

Thus, I modified main() function by adding hashes of all four txs:

int main()
    // Replace these hashes with ones from a block to reproduce the same merkle root.
    bc::hash_list tx_hashes{{

    const bc::hash_digest merkle_root = create_merkle(tx_hashes);
    std::cout << "Result: " << bc::encode_base16(merkle_root) << std::endl;
    return 0;

Compiling and running it gave me:

Current merkle hash list:

Current merkle hash list:

Result: 6657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f3

while the actual merkleroot is different:

"merkleroot": "f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766",

What I did wrong?


You actually did get the same hash. It's just that every Bitcoin software displays hashes byte swapped. If you look closely, you'll see that the first byte of your result (66) matches the last byte of the displayed merkleroot. The second byte matches the second to last byte, and so on.

Internally, the merkleroot (and other hashes) are in the same order as the output of the hash function. But for display to users, they are always byte swapped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.