The P2PKH Unlocking Script
The P2PKH Unlocking Script
To spend a P2PKH output, the spender must provide two pieces of data in their input: a cryptographic signature and the original public key that hashes to the target.
1. The ScriptSig Template
The unlocking script looks like this:
[Signature] [Public Key Bytes]
Notice that unlike P2PK, the spender must provide the public key here because it is not stored in the locking script.
2. Combined Execution Flow
When a node verifies the spend, it runs the scripts together on the stack:
[Signature] [PubKey] OP_DUP OP_HASH160 [TargetHash] OP_EQUALVERIFY OP_CHECKSIG
The Step-by-Step Logic:
-
Push Data:
SignatureandPubKeyare pushed onto the stack. -
OP_DUP:
PubKeyis duplicated. Stack:[Sig, PubKey, PubKey]. -
OP_HASH160: The top
PubKeyis hashed. Stack:[Sig, PubKey, CalculatedHash]. -
Push Target:
TargetHash(from the sender) is pushed. Stack:[Sig, PubKey, CalculatedHash, TargetHash]. -
OP_EQUALVERIFY: Compares the two hashes. If equal, they are popped. Stack:
[Sig, PubKey]. -
OP_CHECKSIG: Verifies the
Sigagainst thePubKey. If valid, pushes1(Success).
3. The Security Barrier
If an attacker tries to spend your coins with their own signature, they have two problems:
-
If they use your Public Key, their signature won't match.
-
If they use their Public Key, the
OP_EQUALVERIFYstep will fail because their key's hash doesn't match theTargetHash.
4. Size Considerations
Because a P2PKH spend requires both a signature (~71 bytes) and a public key (33-65 bytes), it is physically larger than a P2PK spend. However, because the outputs are smaller (20 bytes vs 65 bytes), P2PKH is considered more efficient for the overall health of the UTXO set.
| Component | Provided By | Role in Execution |
|---|---|---|
| Signature | Spender | Proves ownership of private key |
| Public Key | Spender | Bridges the hash to the signature |
| Hash Target | Sender | Defines the owner |
In the next section, we will discuss Base58Check Encoding.
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: