TeachMeBitcoin

P2PKH ScriptPubKey Structure

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

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

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).

☕ Help support TeachMeBitcoin

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:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!