TeachMeBitcoin

Custom Python wTXID Calculator

From TeachMeBitcoin, the free encyclopedia Reading time: 3 min

Custom Python wTXID Calculator

In this final guide, we will build a Python script that calculates both the TXID and the wTXID from a raw SegWit transaction hex. This will prove mathematically that the TXID is independent of the signatures.

The Dual-ID Calculator

import hashlib

def hash256(data_hex):
    # Standard Bitcoin Double-SHA256
    binary = bytes.fromhex(data_hex)
    first_pass = hashlib.sha256(binary).digest()
    second_pass = hashlib.sha256(first_pass).digest()
    # Bitcoin IDs are displayed in Little-Endian (Reversed)
    return second_pass[::-1].hex()

def audit_ids(version, marker, flag, base_data, witness_data, locktime):
    # 1. TXID Calculation (Exclude Marker, Flag, and Witness)
    # Stream: Version + BaseData + Locktime
    tx_stream = version + base_data + locktime
    txid = hash256(tx_stream)

    # 2. wTXID Calculation (Include Everything)
    # Stream: Version + Marker + Flag + BaseData + Witness + Locktime
    wtx_stream = version + marker + flag + base_data + witness_data + locktime
    wtxid = hash256(wtx_stream)

    print(f"--- Transaction Identity Audit ---")
    print(f"[*] Base Data Length:    {len(base_data)//2} bytes")
    print(f"[*] Witness Data Length: {len(witness_data)//2} bytes")
    print(f"----------------------------------")
    print(f"[*] TXID:  {txid}")
    print(f"[*] wTXID: {wtxid}")

    if txid == wtxid:
        print("[STATUS] This is a Legacy (Non-SegWit) Transaction.")
    else:
        print("[STATUS] This is a SegWit Transaction. TXID is protected.")

# --- Simulation ---

v = "01000000" # Version 1
m = "00"       # SegWit Marker
f = "01"       # SegWit Flag
lt = "00000000" # Locktime 0

# Base data includes inputs (TXID/VOUT) and outputs
base = "01" + ("00" * 32) + "ffffffff" + "01" + "e803000000000000160014" + ("aa" * 20)
# Witness data includes the stack
witness = "02" + "47" + ("bb" * 71) + "21" + ("cc" * 33)

audit_ids(v, m, f, base, witness, lt)

# Observe: If we change a byte in the witness (the signature)
print("\n--- Modifying Witness Data (Miner Malleability Simulation) ---")
witness_malleated = "02" + "47" + ("dd" * 71) + "21" + ("cc" * 33)
audit_ids(v, m, f, base, witness_malleated, lt)

How to Run the Calculator

  1. Ensure you have Python 3 installed.

  2. Copy the code into a file named wtxid_calculator.py.

  3. Run it using python3 wtxid_calculator.py.

Technical Takeaways

  1. Immutability: Notice how the TXID remains identical even when we change the witness bytes. This is what fixed malleability.

  2. Unique Commitment: The wTXID changes every time, ensuring that the exact witness used is committed to the block.

  3. Efficiency: By separating these IDs, Bitcoin allows for a "layered" verification where nodes can check transaction logic separately from signature logic.

Congratulations! You have completed the wTXID module. You now understand the dual identity of modern Bitcoin transactions.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!