Custom Python Nested SegWit Auditor
Custom Python Nested SegWit Auditor
In this final guide, we will build a Python script that analyzes an Unlocking Script (ScriptSig) to determine if it is a Nested SegWit spend. We will look for the specific 22-byte pattern that signals a SegWit transition.
The Nested SegWit Auditor
def audit_scriptsig(scriptsig_hex):
# 1. Identify the length of the script
# The first byte is usually the length of the redeem script
try:
script_len = int(scriptsig_hex[:2], 16)
redeem_script = scriptsig_hex[2:]
except:
print("[ERROR] Invalid ScriptSig format.")
return
print(f"--- Nested SegWit Audit ---")
print(f"[*] Full ScriptSig: {scriptsig_hex}")
print(f"[*] Script Length: {script_len} bytes")
# 2. Check for the Nested SegWit P2WPKH signature
# Pattern: 00 (Version 0) 14 (Push 20 bytes)
if script_len == 22 and redeem_script.startswith("0014"):
print("[STATUS] MATCH: This is a Nested SegWit (P2SH-P2WPKH) spend.")
pkh = redeem_script[4:]
print(f"[*] Public Key Hash: {pkh}")
print(f"[*] Logic: Move signatures to the Witness block.")
# 3. Check for the Nested SegWit P2WSH signature
# Pattern: 00 (Version 0) 20 (Push 32 bytes)
elif script_len == 34 and redeem_script.startswith("0020"):
print("[STATUS] MATCH: This is a Nested SegWit (P2SH-P2WSH) spend.")
sh = redeem_script[4:]
print(f"[*] Script Hash: {sh}")
print(f"[*] Logic: Multi-signature witness logic detected.")
else:
print("[STATUS] NO MATCH: This is a standard P2SH (Legacy) spend.")
# --- Simulation ---
# Case 1: Standard Nested SegWit (P2WPKH)
# Length 22 (16 hex), starts with 0014
p2wpkh_scriptsig = "16001462e907b15cbf27d5425399ebf6f0fb50ebb88f18"
audit_scriptsig(p2wpkh_scriptsig)
# Case 2: Legacy P2SH Multisig
# Length is not 22 or 34
print("\n--- Next Audit ---")
legacy_scriptsig = "47522102f9e61c56f7e841f77d337d45e4120f44e132e01b3d36b85994f31c28b5e28a952103333333333333333333333333333333333333333333333333333333333333333352ae"
audit_scriptsig(legacy_scriptsig)
How to Run the Auditor
-
Ensure you have Python 3 installed.
-
Copy the code into a file named
nested_auditor.py. -
Run it using
python3 nested_auditor.py.
Technical Takeaways
-
Strict Signaling: Notice how the script length must be exactly 22 bytes. If a user added even one extra byte of data, the node would treat it as a legacy P2SH script and fail to find the signatures.
-
Witness Redirection: The presence of
00 14tells the validator to "look elsewhere" for the data. This is why SegWit was able to increase throughput without breaking old nodes. -
Upgrade Path: Nested SegWit showed that Bitcoin can support multiple script "Versions" at once. Version 0 is SegWit, and we are now moving into Version 1 (Taproot).
Congratulations! You have completed the Nested SegWit (P2SH-P2WPKH) module. You now understand how Bitcoin bridged the gap between its legacy and modern eras.
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: