Module 07
P2PKH
Pay-to-Public-Key-Hash is the most common script template on the Bitcoin network. Every address starting with '1' encodes a P2PKH. Let's dissect it opcode by opcode.
What you'll learn
- →Identify the scriptSig and scriptPubKey halves of a P2PKH transaction
- →Trace every opcode step in a P2PKH execution
- →Explain why Bitcoin hashes public keys before embedding them in addresses
- →Connect a Base58Check address to the 20-byte hash inside it
01
Two scripts, one validation
A Bitcoin transaction input carries a scriptSig (the unlocking script). The output being spent carries a scriptPubKey (the locking script). To validate, nodes concatenate them and run the combined script. This is the P2PKH pattern.
For P2PKH the split is:
scriptSig (unlocking)
<sig> <pubkey>
Provided by the spender. Proves ownership.
scriptPubKey (locking)
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Embedded in the output. Defines the conditions.
Address reuse risk
02
Step by step
Let's trace what each opcode does when P2PKH runs.
<sig> <pubkey>The spender pushes their signature and public key onto the stack.
OP_DUPDuplicates the pubkey. Now the stack has two copies — one for hashing, one for signature verification.
OP_HASH160Hashes the top copy of the pubkey with RIPEMD160(SHA256). Produces the 20-byte pubkey hash.
<pubKeyHash>The expected hash from the scriptPubKey is pushed. This is what the address was derived from.
OP_EQUALVERIFYPops both hashes and checks they match. If they differ, the script fails immediately — wrong pubkey.
OP_CHECKSIGUses the original pubkey (still on the stack) to verify the signature against the transaction hash. Pushes 1 if valid.
Now watch it run. The pubkey hash below is the live HASH160 of the test-vector pubkey:0x751e76e8199196d454941c45d1b3a323f1433bd6
↑ top of stack
nothing here yet
press Step or Run to push an item
03
Why hash the pubkey?
Publishing a raw public key reveals it to the world. Bitcoin's designers added a second layer of hashing so that even if ECDSA were broken, the owner's public key would remain hidden until the coin is spent.
Additionally, a 20-byte hash is smaller than a 33-byte compressed public key — fewer bytes on-chain means lower fees.
The tradeoff: once you spend a P2PKH output, the pubkey is revealed. That's why address reuse is discouraged — each spend exposes your key.
04
Your turn
Assemble the full P2PKH script from scratch using the test vectors. The pubkey hash is 0x751e76e8199196d454941c45d1b3a323f1433bd6.
Run a full P2PKH
Ctrl+Enter to check