Skip to main content

Validate addresses

Every network descriptor provides an isValidAddress() method that checks whether a string is a valid address for that specific network. This is useful for validating user input before creating a transaction.

Basic usage

import { ChainGate } from 'chaingate'

const cg = new ChainGate({ apiKey: 'your-api-key' })

// Bitcoin
cg.networks.bitcoin.isValidAddress('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4') // true
cg.networks.bitcoin.isValidAddress('invalid-address') // false

// Ethereum
cg.networks.ethereum.isValidAddress('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045') // true
cg.networks.ethereum.isValidAddress('not-an-address') // false

// Bitcoin Cash
cg.networks.bitcoincash.isValidAddress('bitcoincash:qq...') // true

EVM address validation

EVM networks accept both checksummed (mixed-case) and all-lowercase or all-uppercase addresses. When the address uses mixed case, the EIP-55 checksum is verified.

// All valid
cg.networks.ethereum.isValidAddress('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045') // true (checksummed)
cg.networks.ethereum.isValidAddress('0xd8da6bf26964af9d7eed9e03e53415d37aa96045') // true (lowercase)

// Invalid checksum (wrong mixed-case)
cg.networks.ethereum.isValidAddress('0xD8DA6bf26964af9d7eed9e03e53415d37aa96045') // false

This also works on custom RPC networks:

const bsc = cg.networks.evmRpc({
rpcUrl: 'https://bsc-dataseed.binance.org',
chainId: 56,
name: 'BNB Smart Chain',
symbol: 'BNB',
})

bsc.isValidAddress('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045') // true

UTXO address type identification

UTXO networks provide an additional identifyAddressType() method that returns the detailed type of a valid address. This is useful for detecting which encoding or script type an address uses.

const btc = cg.networks.bitcoin

btc.identifyAddressType('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4')
// → 'segwit-p2wpkh'

btc.identifyAddressType('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')
// → 'legacy-p2pkh'

btc.identifyAddressType('bc1p...')
// → 'taproot-p2tr'

btc.identifyAddressType('invalid')
// → 'unknown'

Possible return values

ValueDescription
'legacy-p2pkh'Pay-to-Public-Key-Hash (starts with 1 on Bitcoin)
'legacy-p2sh'Pay-to-Script-Hash (starts with 3 on Bitcoin)
'legacy-p2pk'Pay-to-Public-Key
'legacy-p2ms'Bare multisig
'segwit-p2wpkh'Native SegWit (starts with bc1q on Bitcoin)
'segwit-p2wsh'SegWit script hash
'taproot-p2tr'Taproot (starts with bc1p on Bitcoin)
'taproot-n-of-n'Taproot n-of-n multisig
'taproot-m-of-n'Taproot m-of-n multisig
'unknown'Not a recognized address format

Bitcoin Cash validation

Bitcoin Cash accepts both CashAddr format (e.g., bitcoincash:qq...) and legacy Base58Check format:

const bch = cg.networks.bitcoincash

bch.isValidAddress('bitcoincash:qqkv9wr69ry2p9l53lxp67xtt0m405eecfkpk648ks') // true
bch.isValidAddress('1BpEi6DfDAUFd7GtWo8VKMmk7CQi4xr2pG') // true
bch.isValidAddress('invalid') // false

Standalone EVM validation

A standalone isValidEvmAddress() function is also exported for use without a ChainGate instance:

import { isValidEvmAddress } from 'chaingate'

isValidEvmAddress('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045') // true

Use cases

  • Form validation -- Validate recipient addresses in your UI before creating a transaction.
  • Address detection -- Use identifyAddressType() to detect the encoding of a UTXO address and adapt your UI accordingly.
  • Multi-network support -- Check which network an address belongs to by testing it against multiple network descriptors.