Address Generation & Validation
In the UTXO model, coins are locked by output scripts (scriptPubKeys). Addresses are human-readable encodings of those scripts, so generating and validating addresses is how you turn Bitcoin Script into something users and APIs can use. This page covers the main address types (P2PKH, P2SH, P2WPKH, P2WSH, P2TR), encoding (Base58Check, Bech32, Bech32m), and how to derive and validate them in code. Decode and inspect addresses in the Address Decoder tool.
Legacy Addresses (P2PKH)
Format: Starts with 1 (mainnet) or m/n (testnet)
1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2
Script: Pay-to-Public-Key-Hash
OP_DUP OP_HASH160 <pubkey_hash> OP_EQUALVERIFY OP_CHECKSIG
Script Hash Addresses (P2SH)
Format: Starts with 3 (mainnet) or 2 (testnet)
3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy
Script: Pay-to-Script-Hash (can contain any script)
OP_HASH160 <script_hash> OP_EQUAL
Native SegWit (P2WPKH)
Format: Starts with bc1q (mainnet) or tb1q (testnet)
bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq
Script: Pay-to-Witness-Public-Key-Hash
OP_0 <20-byte-pubkey-hash>
Native SegWit Script (P2WSH)
Format: Starts with bc1q but longer (mainnet)
bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3
Script: Pay-to-Witness-Script-Hash
OP_0 <32-byte-script-hash>
Taproot (P2TR)
Format: Starts with bc1p (mainnet) or tb1p (testnet)
bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297
Script: Pay-to-Taproot
OP_1 <32-byte-x-only-pubkey>
| Type | Prefix | Size | Encoding | Fee Efficiency |
|---|---|---|---|---|
| P2PKH | 1 | 25 bytes | Base58Check | Lowest |
| P2SH | 3 | 23 bytes | Base58Check | Low |
| P2WPKH | bc1q | 22 bytes | Bech32 | High |
| P2WSH | bc1q | 34 bytes | Bech32 | Medium |
| P2TR | bc1p | 34 bytes | Bech32m | Highest |
From Public Key to P2WPKH (Native SegWit)
Bech32 vs Bech32m
Bech32 (BIP-173):
- Used for SegWit v0 (P2WPKH, P2WSH)
- Checksum constant: 1
Bech32m (BIP-350):
- Used for SegWit v1+ (Taproot)
- Checksum constant: 0x2bc830a3
Why Bech32m?
Bech32 had a weakness where certain error patterns could go undetected. Bech32m fixes this for future witness versions.
BIP Standards
| BIP | Path | Address Type | Example |
|---|---|---|---|
| BIP44 | m/44'/0'/0' | P2PKH | 1... |
| BIP49 | m/49'/0'/0' | P2SH-P2WPKH | 3... |
| BIP84 | m/84'/0'/0' | P2WPKH | bc1q... |
| BIP86 | m/86'/0'/0' | P2TR | bc1p... |
Deriving Addresses from Seed
Creating 2-of-3 Multisig
Address Validation Pitfalls
- BAD: Only checking prefix (
address.startsWith('bc1')) - GOOD: Full validation with checksum verification using library functions
Network Mismatch
Always use consistent network parameters. Never mix mainnet keys with testnet addresses.
Checksum Errors
Always validate checksums before using an address - all libraries provide validation functions.
Address Handling
- Case Sensitivity: Bech32 is case-insensitive, Base58 is case-sensitive
- Display Formatting: Consider QR codes for long addresses
- Copy Protection: Use checksums to detect copy errors
- Network Verification: Always verify mainnet vs testnet
Understanding Bitcoin addresses requires knowledge of:
- Encoding Schemes: Base58Check, Bech32, Bech32m
- Script Types: P2PKH, P2SH, P2WPKH, P2WSH, P2TR
- Derivation: BIP32/39/44/49/84/86 standards
- Validation: Checksum verification and format checking
Modern applications should default to Bech32m (Taproot) addresses for the best fee efficiency and privacy features.
- Key Management - Managing private and public keys
- Output Descriptors - Deriving addresses from descriptors
- Payment Requests - bitcoin: URIs and receiving payments
- Address Types - Detailed address type comparison
- HD Wallets - Derivation paths and wallet structure
- Transaction Construction - Using addresses in transactions