Transaction Structure
Overview
P2WSH transactions store all script and signature data in the witness field, leaving the ScriptSig empty.
Output (Locking Script - ScriptPubKey)
The ScriptPubKey for P2WSH consists of the witness version and the 32-byte script hash.
OP_0
OP_PUSHBYTES_32
1863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262
# Hex value: 00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262Input (Unlocking Script - ScriptSig)
For native P2WSH, the ScriptSig is empty.
All signature and script data is stored in the witness field.
Witness Data
The witness data follows a specific serialization format. It begins with the number of stack items as a single byte, followed by each item's size and value in sequence.
For a 2-of-3 multisig P2WSH, the witness contains: [dummy element][signatures][witness script].
04 # Item count (4 items)
00 # First item size (0 bytes - empty dummy element)
# First item value: (none - empty for CHECKMULTISIG bug)
48 # Second item size (72 bytes)
3045022100...01 # Second item value: First signature + SIGHASH
48 # Third item size (72 bytes)
304402...01 # Third item value: Second signature + SIGHASH
69 # Fourth item size (105 bytes)
522102...53ae # Fourth item value: Witness script (2-of-3 multisig)Script Execution
When validating a P2WSH transaction, the script execution occurs in two phases:
- Script Hash Verification
- The witness script (last item in witness data) is hashed using SHA-256.
- The computed hash is compared against the 32-byte script hash in the ScriptPubKey.
- Witness Script Execution
- If the hashes match, the witness script is deserialized into its component opcodes.
- The script is executed with the remaining witness items as stack inputs.
- If the hashes do not match, the transaction fails immediately.
The execution trace varies depending on the type of witness script used:
- A 2-of-3 multisig script requires signature verification against multiple public keys.
- A timelock script involves time comparison operations.
- A HTLC script performs hash verification with conditional branches.
Each script type defines its own set of opcodes and stack operations.
Execution Trace
The example below demonstrates execution for a 2-of-3 multisig script.
Phase 1: Witness Script Hash Verification
- Witness Version Detection
The node recognizes witness version 0 with a 32-byte program in the ScriptPubKey, triggering P2WSH validation rules. - Extract Witness Script
The last item in the witness data is identified as the witness script and saved for execution. - Hash Witness Script
Apply SHA-256 to the witness script, producing a 32-byte hash. - Compare Hashes
Compare the computed hash with the 32-byte script hash from the ScriptPubKey. If they do not match, the transaction fails immediately.
Phase 2: Witness Script Execution
- Push Dummy Element
Push the empty element from the witness data onto the stack. This dummy element is required due to a bug in OP_CHECKMULTISIG. - Push First Signature
Push the first DER-encoded signature with SIGHASH suffix from the witness data onto the stack. - Push Second Signature
Push the second DER-encoded signature with SIGHASH suffix from the witness data onto the stack. - Deserialize Witness Script
The witness script is deserialized into its component opcodes. - Push Required Signature Count
Push the number 2 onto the stack, indicating the minimum number of signatures required. - Push Public Keys
Push all three public keys from the witness script onto the stack. - Push Total Key Count
Push the number 3 onto the stack, indicating the total number of public keys. - Verify Multisig
Verify that 2 valid signatures exist for any 2 of the 3 public keys. The dummy element is consumed due to the off-by-one bug. If verification succeeds, 1 is pushed onto the stack. - Result
If the final stack contains 1, the transaction is valid and the funds are unlocked.