The Witness Stack for P2WPKH
The Witness Stack for P2WPKH
The most radical change in P2WPKH is the total elimination of the ScriptSig. In a Native SegWit transaction, the input contains Zero Bytes of script data. Instead, all authorization data is moved to the Witness Stack.
1. The Empty ScriptSig
In a legacy transaction, the Signature and PubKey are part of the transaction's "Body."
In a P2WPKH transaction, the Input's script field is replaced with an empty 0x00 byte. This is why SegWit effectively "Fixes" Transaction Malleability—because the data that generates the TXID no longer contains the signature.
2. The Witness Area
The signature and public key are placed in a separate data structure at the end of the transaction:
[Signature] [Public Key]
-
Signature: Usually 71-72 bytes.
-
Public Key: Must be a Compressed 33-byte public key. (Uncompressed keys are not allowed in SegWit).
3. The Validation Process
When an upgraded node validates the spend:
-
It sees the
00 14in the ScriptPubKey. -
It looks at the Witness Stack.
-
It pops the
SignatureandPubKey. -
It hashes the
PubKeyand ensures it matches theHashfrom the ScriptPubKey. -
It verifies the
Signatureagainst thePubKey.
4. Why Compressed Keys Only?
To maximize efficiency, SegWit enforces the use of Compressed Public Keys.
-
An uncompressed key is 65 bytes.
-
A compressed key is 33 bytes. If a wallet tries to use an uncompressed key in a SegWit transaction, the node will reject it as Invalid, even if the signature is correct. This forces everyone to use the most efficient data format.
| Data Type | Legacy Location | Native SegWit Location |
|---|---|---|
| Previous TXID | Body | Body |
| Output Index | Body | Body |
| Signature | ScriptSig (Body) | Witness Stack |
| Public Key | ScriptSig (Body) | Witness Stack |
In the next section, we will discuss Bech32 Encoding (BIP 173).
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: