P2PKH ScriptPubKey Structure
P2PKH ScriptPubKey Structure
The P2PKH locking script is more complex than P2PK because it must verify two things: that the provided public key is correct, and that the signature is valid. It uses a sequence of 5 opcodes and a 20-byte hash.
1. The Locking Script (ScriptPubKey)
A P2PKH script follows this exact template:
OP_DUP OP_HASH160 [20-Byte Public Key Hash] OP_EQUALVERIFY OP_CHECKSIG
2. Breaking Down the Opcodes
-
OP_DUP: Duplicates the top item on the stack (the public key).
-
OP_HASH160: Hashes the duplicated public key using SHA256 followed by RIPEMD160.
-
[20-Byte Hash]: The "locked" target provided by the sender.
-
OP_EQUALVERIFY: Compares the hash just calculated with the hash provided in the script. If they don't match, the script fails immediately.
-
OP_CHECKSIG: Finally, checks if the signature matches the public key (exactly like in P2PK).
3. Raw Hex Representation
In a raw transaction, a P2PKH script looks like this:
76 a9 14 [20-byte-hash] 88 ac
| Hex | Opcode | Purpose |
|---|---|---|
0x76 |
OP_DUP | Duplicate the key |
0xa9 |
OP_HASH160 | Hash the key |
0x14 |
Push 20 Bytes | The next 20 bytes are data |
0x88 |
OP_EQUALVERIFY | Check if hashes match |
0xac |
OP_CHECKSIG | Verify the signature |
4. Why the complexity?
By requiring the public key to match a hash before checking the signature, Bitcoin ensures that the sender only needs to know your "address" (the hash) to pay you. You only reveal your full public key when you are ready to spend, keeping it safe from potential vulnerabilities until the last possible second.
In the next section, we will look at the Unlocking Script (ScriptSig).
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: