THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Bitcoin Address Validation: Network Checks, Witness Programs, and Common Mistakes

permalink THAMER
#address-validation#base58check#bech32#bitcoin#mainnet#testnet

A Bitcoin address validator should answer a narrow question: does this string encode a destination recognized under the intended network and output-format rules? It cannot answer whether the recipient owns the keys, whether the payment is appropriate, or whether a transaction will confirm. Those are separate concerns.

Identify the intended network

The caller must provide the intended network independently of the address string. Mainnet, testnet, signet, and regtest should not be conflated. Some test networks share address encodings even when their consensus environments differ, so an address alone may not uniquely identify the user's chosen testing environment. Use explicit wallet and node network configuration rather than inferring all context from a prefix.

For Base58Check, decode the complete payload and verify its version byte and checksum. For native SegWit, decode the human-readable part, witness version, program, and Bech32-family checksum. Mainnet native SegWit uses bc; testnet-style addresses use tb; regtest uses bcrt.

Enforce version-specific rules

Legacy P2PKH and P2SH require known version bytes and appropriate payload lengths. SegWit witness version 0 requires the original Bech32 checksum and a 20- or 32-byte program. Witness versions 1 through 16 use Bech32m; version 1 Taproot requires a 32-byte program. Generic SegWit witness programs must satisfy the protocol's length limits, but parsing a future version is not equivalent to understanding its future spending semantics.

Reject mixed-case Bech32 strings, illegal characters, invalid checksums, and malformed bit conversion or padding. Do not trim or silently rewrite internal characters. Leading and trailing whitespace should be handled deliberately at the UI boundary and never used to excuse ambiguous inputs.

Validate more than the string

Before sending, display the full normalized destination, network, and amount for user confirmation. Compare the intended payee's address using a trusted channel, especially after pasting from a clipboard. A valid checksum will not detect a maliciously substituted address.

When accepting a BIP21 payment URI, parse the URI separately before validating its destination. The amount and optional fields are request metadata, not part of the address checksum. Reject unknown required parameters according to the URI rules rather than ignoring them.

For wallet recovery and indexing, addresses are only an interface. Wallets also need descriptors, derivation paths, script information, and relevant keys. Correct validation of an address does not imply that a wallet can discover or spend its outputs.

Primary references

BIP173 — Bech32 SegWit address format: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

BIP350 — Bech32m for witness versions 1–16: https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki

BIP21 — URI scheme: https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects