Bitcoin Address Formats
Complete guide to Bitcoin address formats for agents. P2PKH, P2SH, SegWit, Taproot - recognition patterns and use cases.
Bitcoin Address Formats
Bitcoin addresses have evolved through four major formats. Agents must recognize and handle all types to interact with the full Bitcoin ecosystem.
Address Types at a Glance
| Type | Prefix | Length | Encoding | Introduced |
|---|---|---|---|---|
| P2PKH (Legacy) | 1 | 25-34 chars | Base58Check | 2009 |
| P2SH (Script) | 3 | 34 chars | Base58Check | 2012 (BIP-16) |
| P2WPKH (SegWit v0) | bc1q | 42 chars | Bech32 | 2017 (BIP-141) |
| P2TR (Taproot) | bc1p | 62 chars | Bech32m | 2021 (BIP-341) |
P2PKH (Pay-to-Public-Key-Hash)
Prefix: 1 (mainnet), m or n (testnet)
The original Bitcoin address format. Pays to the hash of a public key.
Example: 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
↑
Always starts with 1
Structure
Version (1 byte) + RIPEMD160(SHA256(pubkey)) (20 bytes) + Checksum (4 bytes)
Agent Notes
- Highest transaction fees (largest witness data)
- Still widely used, must support for compatibility
- Satoshi’s genesis block address uses this format
P2SH (Pay-to-Script-Hash)
Prefix: 3 (mainnet), 2 (testnet)
Pays to the hash of a script. Enables multisig and wrapped SegWit.
Example: 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy
↑
Always starts with 3
Use Cases
- Multisig wallets: 2-of-3, 3-of-5 signing schemes
- Wrapped SegWit (P2SH-P2WPKH): SegWit compatibility for old wallets
- Complex scripts: Time-locked contracts, hash locks
Agent Notes
- When you see
3..., it could be multisig OR wrapped SegWit - Cannot determine spending conditions from address alone
- Medium fees (between P2PKH and native SegWit)
P2WPKH (Native SegWit v0)
Prefix: bc1q (mainnet), tb1q (testnet)
Native Segregated Witness address. Recommended for most use cases.
Example: bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq
↑↑↑↑
bc1q prefix (q = version 0)
Benefits
- Lower fees: ~30-40% cheaper than P2PKH
- Error detection: Bech32 encoding catches typos
- Malleability fix: Transaction IDs cannot be modified
Bech32 Encoding
- Uses 32-character alphabet:
qpzry9x8gf2tvdw0s3jn54khce6mua7l - Case insensitive (but lowercase preferred)
- Includes error-correcting checksum
Agent Notes
- Recommended for receiving payments
- Supported by all modern wallets (2024+)
- Parse with Bech32 library, not Base58
P2TR (Taproot)
Prefix: bc1p (mainnet), tb1p (testnet)
Taproot address (SegWit v1). Most advanced format with privacy and smart contract benefits.
Example: bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297
↑↑↑↑
bc1p prefix (p = version 1)
Benefits
- Privacy: Single-sig and multi-sig look identical on-chain
- Efficiency: Complex scripts revealed only when needed
- Schnorr signatures: Aggregatable, smaller, faster
Bech32m Encoding
- Modified Bech32 for SegWit v1+
- Different checksum constant than Bech32
- Use BIP-350 compliant libraries
Agent Notes
- Best for privacy-sensitive operations
- Supports advanced scripting (Tapscript)
- Requires newer wallet software
Address Validation
Regex Patterns
// Mainnet address patterns
const patterns = {
p2pkh: /^1[a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2sh: /^3[a-km-zA-HJ-NP-Z1-9]{33}$/,
p2wpkh: /^bc1q[a-z0-9]{38,39}$/,
p2tr: /^bc1p[a-z0-9]{58}$/
};
// Testnet patterns
const testnetPatterns = {
p2pkh: /^[mn][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
p2sh: /^2[a-km-zA-HJ-NP-Z1-9]{33}$/,
p2wpkh: /^tb1q[a-z0-9]{38,39}$/,
p2tr: /^tb1p[a-z0-9]{58}$/
};
Validation Steps
- Check prefix to determine type
- Validate length for expected range
- Verify checksum (Base58Check or Bech32/m)
- Decode to extract pubkey hash or script hash
# Python example using bitcoinlib
from bitcoin import decode_address
def validate_address(address):
try:
result = decode_address(address)
return {
"valid": True,
"type": result.type,
"network": result.network
}
except:
return {"valid": False}
API Response Handling
When querying blockchain APIs, addresses may be returned in different formats:
// mempool.space UTXO response
{
"txid": "abc123...",
"vout": 0,
"value": 100000,
"address": "bc1q..." // May be any format
}
Agent Strategy
- Store addresses in their original format
- Validate format before using
- Use SegWit (bc1q) when generating new addresses
- Accept all formats when receiving payments
Fee Comparison
Transaction size by address type (single input, two outputs):
| Type | Virtual Bytes | Fee at 10 sat/vB |
|---|---|---|
| P2PKH | ~226 vB | 2,260 sats |
| P2SH-P2WPKH | ~167 vB | 1,670 sats |
| P2WPKH | ~141 vB | 1,410 sats |
| P2TR | ~111 vB | 1,110 sats |
Recommendation: Use P2TR or P2WPKH for optimal fees.
Network Prefixes Summary
| Network | P2PKH | P2SH | Bech32 HRP |
|---|---|---|---|
| Mainnet | 1 | 3 | bc |
| Testnet | m/n | 2 | tb |
| Signet | m/n | 2 | tb |
| Regtest | m/n | 2 | bcrt |
Machine-Readable Summary
{
"topic": "bitcoin-address-formats",
"address_types": [
{"type": "p2pkh", "prefix": "1", "encoding": "base58check"},
{"type": "p2sh", "prefix": "3", "encoding": "base58check"},
{"type": "p2wpkh", "prefix": "bc1q", "encoding": "bech32"},
{"type": "p2tr", "prefix": "bc1p", "encoding": "bech32m"}
],
"recommendation": "p2wpkh",
"validation_libraries": ["bitcoinjs-lib", "python-bitcoinlib", "rust-bitcoin"]
}