Merkle Tree

Understanding how the Merkle root is computed for WitsCoin mining

What is a Merkle Tree?

The Merkle root is a single hash that represents ALL transactions in a block. This is how Bitcoin and other blockchains verify transactions without downloading everything.

How it Works

  1. Hash each transaction individually (SHA-256)
  2. Pair up hashes and hash each pair together
  3. If odd number of hashes, duplicate the last one
  4. Repeat until only ONE hash remains = The MERKLE ROOT

Working Example: Computing the Merkle Root

Here's a complete example with 3 transactions that you can verify yourself:

Input Transactions

Transaction 1:
  ID:     550e8400-e29b-41d4-a716-446655440000
  From:   wc_a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
  To:     joshua_wits_coin_2026
  Amount: 100
  Nonce:  1

Transaction 2:
  ID:     550e8400-e29b-41d4-a716-446655440001
  From:   wc_b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7
  To:     wc_a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
  Amount: 50
  Nonce:  2

Transaction 3:
  ID:     550e8400-e29b-41d4-a716-446655440002
  From:   wc_c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8
  To:     joshua_wits_coin_2026
  Amount: 75
  Nonce:  3

Step 1: Hash Each Transaction

Each transaction is hashed using: SHA-256(id + from + to + amount + nonce)

Transaction 1 hash: 5d2e051e4221fc669b6886ab973f6f0f323f308a84b760b8a62c281ebcb85fb1
Transaction 2 hash: 15f5508d4a7a4bed76249a17df1890c77eed6fcc9e434ae51c2f952475eecba7
Transaction 3 hash: e288049774319e8fd960d2b68a1d828826da6132cb96a15b18bed8cb33ac94f7

Step 2: Build the Tree (Level 1)

We have 3 hashes (odd number), so we duplicate the last one for pairing.

IMPORTANT: Before concatenating, sort the two hashes - the smaller hash goes first!

Pair 1: hash_pair(tx1_hash, tx2_hash)
  Sort: 15f5508d... < 5d2e051e... so tx2 goes first
  SHA-256("15f5508d4a7a4bed76249a17df1890c77eed6fcc9e434ae51c2f952475eecba7"
        + "5d2e051e4221fc669b6886ab973f6f0f323f308a84b760b8a62c281ebcb85fb1")
  = e80dc6c5ce934447bce19f292b3c4bde9f22c9ff62b844326b1cf5a37a3b092c (Hash A)

Pair 2: hash_pair(tx3_hash, tx3_hash) - duplicated!
  SHA-256("e288049774319e8fd960d2b68a1d828826da6132cb96a15b18bed8cb33ac94f7"
        + "e288049774319e8fd960d2b68a1d828826da6132cb96a15b18bed8cb33ac94f7")
  = e8faa67e9d08fd9afa648f93ad9b3ab892690037e0ab513291a23216ec54a961 (Hash B)

Step 3: Build the Tree (Level 2 - Root)

Now we have 2 hashes (Hash A and Hash B). Sort them, then hash together:

Root: hash_pair(hash_a, hash_b)
  Sort: e80dc6c5... < e8faa67e... so Hash A goes first
  SHA-256("e80dc6c5ce934447bce19f292b3c4bde9f22c9ff62b844326b1cf5a37a3b092c"
        + "e8faa67e9d08fd9afa648f93ad9b3ab892690037e0ab513291a23216ec54a961")
  = bc2c04be23624f75584b38c1530d485f016038f1932931df75fd6a76a8873780

Final Merkle Root

bc2c04be23624f75584b38c1530d485f016038f1932931df75fd6a76a8873780

Merkle Root for Mining

When mining a block, the Merkle root is computed from all transactions that will be in the block. Every block includes at least one transaction: the coinbase reward paid to the miner.

The Reward Transaction

The server creates a deterministic reward transaction for each block:

Reward TX ID:  uuid5(NameSpaceOID, wallet_address + timestamp)
From:          "coinbase"
To:            your_wallet_address
Amount:        50
Nonce:         0

The reward transaction ID is computed using UUID v5 (SHA-1 based) with the OID namespace:

Namespace OID: 6ba7b812-9dad-11d1-80b4-00c04fd430c8

Real Example: Block 6 (0 pending transactions)

With wallet wc_ee964162ad97b399f7cbbc540148ad8e and timestamp 1777581991:

Reward TX ID:   64782db8-c8a2-59fb-8dcb-b200a95b8ee6
  = uuid5(OID, "wc_ee964162ad97b399f7cbbc540148ad8e1777581991")

TX Hash Input:  "64782db8-c8a2-59fb-8dcb-b200a95b8ee6" + "coinbase"
              + "wc_ee964162ad97b399f7cbbc540148ad8e" + "50" + "0"

TX Hash:        66e06a276989e104ca2bc8e3917f9cd150df0ae90088dde229747a48c9342cad

With only 1 transaction, the Merkle root IS the transaction hash:

Merkle Root: 66e06a276989e104ca2bc8e3917f9cd150df0ae90088dde229747a48c9342cad

With Pending Transactions

If there are pending transactions in the pool, they are included after the reward transaction. The Merkle tree is then built over all transaction hashes (reward first, then pending in order).

Python Script

Download the complete Python script that implements Merkle root computation and mining:

merkle_root.py - Merkle root calculator with worked examples
Download merkle_root.py

Quick Start

# Run the demonstration with example transactions
python3 merkle_root.py

# Compute a Merkle root from transaction hashes
python3 merkle_root.py hash1 hash2 hash3