Bitcoin address validator

How to validate a Bitcoin address?

Validating Bitcoin addresses is an essential task in the development of Bitcoin applications, ensuring that users are sending funds to correct and valid addresses. This article outlines the process of validating Bitcoin addresses.

Introduction to Bitcoin Addresses

Bitcoin addresses are alphanumeric strings derived from public keys and used to receive funds. They come in different formats, with the most common being:

  • P2PKH (Pay to Public Key Hash) addresses start with a '1'.
  • P2SH (Pay to Script Hash) addresses start with a '3'.
  • Bech32 addresses start with 'bc1q', used for SegWit transactions.
  • Bech32m addresses start with 'bc1p', used for Taproot transactions.
Steps for Validating legacy Bitcoin Addresses (P2PKH and P2SH)
Step 1: Base58Check Encoding

Both P2PKH and P2SH addresses use Base58Check encoding, which includes a version byte and a checksum for error detection. To validate an address, we first need to decode it from Base58Check to hexadecimal.

Step 2: Checksum Verification

After decoding, the last 4 bytes of the address are the checksum, which is used to verify the rest of the address. To validate the checksum:

  • Take the first 21 bytes (excluding the checksum).
  • Perform a double SHA-256 hash on them.
  • Compare the first 4 bytes of the result with the checksum. If they match, the address is likely valid.
Step 3: Version Byte

The first byte of the decoded address is the version byte. It determines the address type:

  • `0x00` for P2PKH addresses.
  • `0x05` for P2SH addresses.

Validate that the version byte corresponds to the expected address format.

Step 4: Length Check

Ensure the decoded address is 25 bytes long (20-byte hash, 1-byte version, 4-byte checksum).

Steps for Validating Bech32 and Bech32m Bitcoin Addresses (P2WPKH,P2WSH,P2TR)
Step 1: Prefix and Separator Verification

A valid Bech32 or Bech32m address starts with the network-specific prefix (`bc` for Bitcoin mainnet) followed by the digit `1`, which acts as a separator. The presence and correctness of this prefix and separator are the first validation checks.

Step 2: Bech32 and Bech32m Decoding

After verifying the prefix and separator, decode the rest of the address from the Bech32 or Bech32m format to check its validity. This involves converting the characters after the `1` separator into their 5-bit values, following the Bech32 or Bech32m character set.

Step 3: Checksum Validation

Bech32 and Bech32m addresses include a checksum for error detection. Validate this checksum to ensure the address has not been mistyped or altered. The decoding process will verify the checksum automatically and indicate if there’s an error.

Step 4: Witness Version and Program Length

The decoded data includes a "witness version" byte followed by the "witness program." The witness version (the first byte after decoding) should be between 0 and 16. The length of the witness program (the part following the witness version) is also crucial:

  • For version `0`, valid lengths are `20` and `32` bytes.
  • For versions `1` to `16`, valid lengths are between `2` and `40` bytes.
Step 5: Encoding Check

After validation, re-encode the witness version and program back into a Bech32 or Bech32m address using the same network prefix. If the re-encoded address matches the original input, the address is valid.

Conclusion

Validating Bitcoin addresses is crucial for applications that process Bitcoin transactions to avoid errors and potential loss of funds. This guide outlined the steps for validating the most common types of Bitcoin addresses. Developers can integrate these validation steps into their applications to ensure the integrity and correctness of Bitcoin transactions.