Transaction Structure
Overview
P2WPKH transactions differ from P2PKH in how the signature and public key are stored. Instead of being in the ScriptSig, they are placed in a separate witness field.
Output (Locking Script - ScriptPubKey)
The ScriptPubKey for P2WPKH is more compact than P2PKH, consisting only of the witness version byte and the 20-byte public key hash.
OP_0
OP_PUSHBYTES_20
24bc22345d16821eb478da2408245b831ca90431
# Hex value: 001424bc22345d16821eb478da2408245b831ca9043188acThe ScriptPubKey contains the hash of the recipient's public key, not the public key itself. Funds remain locked until someone provides the correct public key and a valid signature.
Input (Unlocking Script - ScriptSig)
For native P2WPKH, the ScriptSig is empty.
All signature data is moved to the witness field, which is not part of the traditional transaction structure.
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.
Witness Structure Breakdown
For P2WPKH, the witness contains two items:
- The signature.
- The public key.
02 # Item count (2 items)
48 # First item size (72 bytes)
3045022100...01 # First item: DER-encoded signature + SIGHASH type
21 # Second item size (33 bytes)
0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 # Second item: Compressed public key
# Hex value: 0248304502210082b4a8e6c0c8e5b1c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8091a2b3c4d5e6f022012a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f70810121210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
Script Execution
When validating a P2WPKH transaction, the witness data and ScriptPubKey are processed together. The node recognizes witness version 0 with a 20-byte program in the ScriptPubKey (OP_0 OP_PUSHBYTES_20 <pubkey_hash>), triggering P2WPKH validation rules. The following steps describe the execution flow:
- Push Signature
The signature is pushed from the witness data onto the stack. - Push Public Key
The compressed public key is pushed from the witness data onto the stack. - Duplicate Public Key (implicit)
The top stack element is duplicated. - Hash Public Key (implicit)
SHA-256 followed by RIPEMD-160 is applied to the public key. - Push Expected Hash
The public key hash from the ScriptPubKey is pushed onto the stack. - Compare Hash
The computed hash is compared with the witness pubkey hash from the ScriptPubKey. If they do not match, the script fails. - Verify Signature (implicit)
The signature is verified against the public key and transaction data using CHECKSIG. - Result
If the final stack contains 1, the transaction is valid and the funds are unlocked.
Steps marked as "implicit" are enforced by SegWit consensus rules rather than explicit opcodes in the script.