Transaction Structure

Overview

P2PKH transactions consist of inputs and outputs, each containing scripts that define how funds are locked and unlocked.

Output (Locking Script - ScriptPubKey)

When sending funds to a P2PKH address, the transaction output contains a ScriptPubKey that locks the funds. This script specifies the conditions required to spend the funds.

The following diagrams shows the P2PKH ScriptPubKey structure:
P2PKH ScriptPubKey Structure OP_DUP 0x76 OP_HASH160 0xa9 OP_PUSHBYTES_20 0x14 Public Key Hash 20 bytes (HASH160 of public key) OP_EQUALVERIFY 0x88 OP_CHECKSIG 0xac
The following code shows a typical P2PKH ScriptPubKey script:
plaintext
OP_DUP
OP_HASH160
OP_PUSHBYTES_20
24bc22345d16821eb478da2408245b831ca90431
OP_EQUALVERIFY
OP_CHECKSIG

# Hex value: 76a91424bc22345d16821eb478da2408245b831ca9043188ac

The ScriptPubKey contains the hash of the recipient's public key, not the public key itself. Funds remain locked until someone provides the correct public key and a valid signature.

Input (Unlocking Script - ScriptSig)

To spend funds from a P2PKH address, the transaction input must provide a ScriptSig containing the data needed to satisfy the locking conditions.

The following diagrams shows the P2PKH ScriptSig structure:
P2PKH ScriptSig Structure Size variable <signature> 71-72 bytes (DER encoded + SIGHASH) Size variable <public key> 33 bytes (compressed) or 65 bytes (uncompressed)
The following code shows a typical P2PKH ScriptSig script:
plaintext
OP_PUSHBYTES_72
3044022041b2a8a8e6c0c8e5b1c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8091a2b3c4d5e6f022012a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708101
OP_PUSHBYTES_33
0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798

# Hex value: 483044022041b2a8a8e6c0c8e5b1c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8091a2b3c4d5e6f022012a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708101210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798

Script Execution

When a transaction is validated, the ScriptSig and ScriptPubKey are concatenated and executed together:

P2PKH ScriptPubKey Stack Execution Initial from ScriptSig OP_DUP 0x76 OP_HASH160 0xa9 Push <hash> from scriptPubKey OP_EQUALVERIFY 0x88 OP_CHECKSIG 0xac <signature> <pubkey> top <signature> <pubkey> <pubkey> top <signature> <pubkey> <pubkeyhash> top <signature> <pubkey> <pubkeyhash> <expected hash> top <signature> <pubkey> top 1 top
  1. The signature and public key are pushed onto the stack from the ScriptSig.
  2. OP_DUP duplicates the public key on the stack.
  3. OP_HASH160 hashes the duplicated public key (SHA-256 followed by RIPEMD-160).
  4. The expected public key hash is pushed from the ScriptPubKey.
  5. OP_EQUALVERIFY verifies that the computed hash matches the expected hash.
  6. OP_CHECKSIG verifies the signature against the public key and transaction data. Push 1 on the stack if it is valid.

If all operations succeed, the transaction is valid and the funds are unlocked for spending.