TeachMeBitcoin

The P2PKH Unlocking Script

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

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:

  1. Push Data: Signature and PubKey are pushed onto the stack.

  2. OP_DUP: PubKey is duplicated. Stack: [Sig, PubKey, PubKey].

  3. OP_HASH160: The top PubKey is hashed. Stack: [Sig, PubKey, CalculatedHash].

  4. Push Target: TargetHash (from the sender) is pushed. Stack: [Sig, PubKey, CalculatedHash, TargetHash].

  5. OP_EQUALVERIFY: Compares the two hashes. If equal, they are popped. Stack: [Sig, PubKey].

  6. OP_CHECKSIG: Verifies the Sig against the PubKey. If valid, pushes 1 (Success).

3. The Security Barrier

If an attacker tries to spend your coins with their own signature, they have two problems:

  1. If they use your Public Key, their signature won't match.

  2. If they use their Public Key, the OP_EQUALVERIFY step will fail because their key's hash doesn't match the TargetHash.

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.

☕ 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!