Constructing Transaction Outputs
How to build transaction outputs — defining recipients, amounts, and locking scripts. Includes P2PKH, P2WPKH, and change outputs.
0steps ·
What You’ll Learn
- The structure of a transaction output
- How P2PKH and P2WPKH locking scripts work
- Why you always need a change output
Output Structure
Each output defines a new UTXO that will exist after the transaction confirms. It contains two fields:
| Field | Size | Description |
|---|---|---|
| Value | 8 bytes | Amount in satoshis (little-endian) |
| ScriptPubKey | Variable | Locking script — conditions to spend this output |
Here is a single output in hex:
# Value: 100,000 satoshis (0.001 BTC), little-endian
a086010000000000
# ScriptPubKey Length
19
# ScriptPubKey (P2PKH)
76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac
Value: Amounts in Satoshis
Output values are always expressed in satoshis (1 BTC = 100,000,000 satoshis) as an 8-byte little-endian integer.
0.001 BTC = 100,000 sats → a086010000000000
0.01 BTC = 1,000,000 sats → 40420f0000000000
1.0 BTC = 100,000,000 sats → 00e1f50500000000
Converting to little-endian: take the hex value, pad to 16 hex characters, then reverse the byte pairs. For 100,000 (0x186A0):
0x186A0 → 00000000000186A0 → A086010000000000
ScriptPubKey: The Locking Script
The ScriptPubKey defines who can spend this output. Different script types create different spending conditions.
P2PKH (Pay-to-Public-Key-Hash)
The classic Bitcoin output. It locks funds to a public key hash (a Bitcoin address starting with 1):
OP_DUP OP_HASH160 <20-byte pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
In hex:
76 a9 14 <20 bytes> 88 ac
│ │ │ │ └─ OP_CHECKSIG
│ │ │ └───── OP_EQUALVERIFY
│ │ └───────────────────── Push 20 bytes
│ └───────────────────────── OP_HASH160
└───────────────────────────── OP_DUP
To spend this output, you must provide a signature and public key whose hash matches the embedded pubKeyHash.
P2WPKH (Pay-to-Witness-Public-Key-Hash)
The modern SegWit output. It locks funds to a witness public key hash (a bc1q... address):
OP_0 <20-byte pubKeyHash>
In hex:
00 14 <20 bytes>
│ │
│ └─── Push 20 bytes
└─────── OP_0 (witness version 0)
P2WPKH scripts are shorter, which means cheaper fees. The unlocking data moves to the witness field instead of ScriptSig — we cover this in Step 8.
The Change Output
Unless your input UTXOs add up to exactly the amount you want to send plus fees — which almost never happens — you need a change output that sends the remainder back to yourself.
Example:
Input: 0.005 BTC (your UTXO)
Output: 0.003 BTC (to recipient)
Output: 0.0019 BTC (change, back to you)
Fee: 0.0001 BTC (implicit — not an output)
The fee is implicit: it is the difference between total inputs and total outputs. There is no “fee” field in a transaction. If you forget the change output, the entire difference becomes the miner’s fee — an expensive mistake.
Best practice: Send change to a new address you control. Reusing addresses weakens privacy by linking your transactions together.
Output Ordering
Your transaction can have outputs in any order. However, randomizing the order (rather than always putting the payment first and change second) improves privacy. Some wallets implement BIP69 for deterministic output ordering, though this is debated.
Putting It Together
For our tutorial transaction with one payment and one change output:
# Output count
02
# Output 0: Payment (0.003 BTC to recipient's P2WPKH)
e093040000000000 # 300,000 satoshis
160014<20-byte-hash> # ScriptPubKey
# Output 1: Change (0.0019 BTC back to our P2WPKH)
b882020000000000 # 190,000 satoshis
160014<20-byte-hash> # ScriptPubKey
Next Step
Continue to Calculating Transaction Fees to understand how to set the right fee so your transaction confirms promptly.