Custom Python wTXID Calculator
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
-
Ensure you have Python 3 installed.
-
Copy the code into a file named
wtxid_calculator.py. -
Run it using
python3 wtxid_calculator.py.
Technical Takeaways
-
Immutability: Notice how the TXID remains identical even when we change the witness bytes. This is what fixed malleability.
-
Unique Commitment: The wTXID changes every time, ensuring that the exact witness used is committed to the block.
-
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.
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: