Address validation
The call
import { getChain, InvalidAddressError } from "@agntn/chains";
const bitcoin = getChain("btc");
bitcoin.validatesAddress; // true
bitcoin.assertAddress("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"); // returns the address
try {
bitcoin.assertAddress("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5");
} catch (error) {
error instanceof InvalidAddressError; // true, the checksum caught the last digit
(error as InvalidAddressError).chain; // "bitcoin"
(error as InvalidAddressError).reason; // "the Bech32 checksum does not hold, so a character is wrong"
}
chain.assertAddress(address) returns the address when it fits the chain's format and throws InvalidAddressError when it doesn't. Chains without a validator throw AddressValidationUnsupportedError instead of quietly saying yes, and chain.validatesAddress tells you which ones those are before you ask. Right now every built-in chain validates. A false green light costs more than a false alarm when the caller is about to send funds.
An EVM rejection also says why, in .reason and at the end of the message. 39 hex digits after 0x, not 40 is a digit lost in the copy. the mixed case does not match the EIP-55 checksum means the digits are there but one of them, or one letter's case, is wrong. The CLI and the agent tools print the reason too, so a model knows what to fix instead of sending the string again. Bitcoin and the chains on its encodings say why as well. the Base58Check checksum does not hold, so a character is wrong is a typo. version byte 0x00, where this chain writes 0x1e or 0x16 is a Bitcoin address pasted where Dogecoin was asked for. a Bech32 checksum under witness version 1, where BIP-350 wants Bech32m is an encoder still on the old checksum. Every rule the address breaks goes into one answer, so a string cut short on the wrong chain hears about its length, its version byte and its checksum at once. That covers Litecoin, Bitcoin Gold, Dogecoin, Pepecoin, Dash, Bitcoin SV, TRON and Decred. The other families still decode to a plain yes or no, so their errors carry no .reason yet.
The library doesn't trim. assertAddress(" bc1q…") fails on the space. The CLI's validate passes the argument through as typed too. The agent tools do trim, because an address lifted out of a log line keeps its trailing newline and one invisible byte should not turn a good address into a bad one.
Why decode
Character counts aren't enough. A 34-character window takes a Bitcoin legacy address and a TRON address, both 25 bytes of Base58Check under different version bytes. Solana's System Program is 32 characters and 32 bytes, a Bitcoin address is 34 characters and 25 bytes. So the base58 validators decode and look at the bytes, and the Base58Check ones hash the last four against the rest. The Bech32 validators run the polymod. And Octra is the one base58 chain where decoding would be wrong: a contract address there is cut out of a base58 string rather than encoded from a payload, so the width is the whole format.
What each family checks
| Chain | Accepted forms | Verified | Left alone |
|---|---|---|---|
| EVM, fourteen chains | 0x and 40 hex digits, mixed case under EIP-55 | the EIP-55 checksum wherever the case carries one | an address in one case throughout, which has no checksum to fail |
| Bitcoin | legacy 1… 3…, Bech32 bc1q…, Bech32m bc1p… | 25 Base58Check bytes under 0x00 or 0x05 with the checksum; the full BIP-173/350 encoding, witness version, program length, padding, checksum | nothing; one character off fails on either branch |
| Litecoin | legacy L… M…, ltc1…, MWEB ltcmweb1… | same as Bitcoin under versions 0x30 and 0x32 and the ltc prefix; MWEB's Bech32 checksum past 90 characters, version digit 0, two 33-byte keys | the deprecated 3… script version is out; curve points |
| Pepecoin | P…, 9…, A… | 25 Base58Check bytes under 0x38 or 0x16 with the checksum | no Bech32 at all |
| Dogecoin | D…, 9…, A… | 25 Base58Check bytes under 0x1e or 0x16 with the checksum | no Bech32 at all; 9… and A… pass as Pepecoin too |
| Bitcoin Cash | CashAddr with or without bitcoincash:, q… p… z… r… | the CashAddr checksum, types 0 to 3 with the CashTokens pair, a 20-byte hash or a 32-byte script hash, zero padding | legacy 1… 3…, the bytes are Bitcoin's; a bare eCash address fails on the checksum |
| Bitcoin Gold | legacy G… A…, btg1… | same as Bitcoin under versions 0x26 and 0x17 and the btg prefix | Bitcoin's 1… and 3…, the fork moved both version bytes |
| Dash | X…, 7… | 25 Base58Check bytes under 0x4c or 0x10 with the checksum | testnet y… and 8…; no Bech32 at all |
| Zcash | transparent t1… t3…, Sapling zs1…, TEX tex1…, Unified u1… | 26 Base58Check bytes under 0x1cb8 or 0x1cbd; Bech32 over 43 bytes; Bech32m over 20 bytes; ZIP-316 Revision 0: Bech32m past 90 characters, F4Jumble undone, padding, item order, receiver lengths, at least one shielded item | Sprout zc…, which nothing can pay since Canopy; testnet; the curve points in shielded receivers |
| Bitcoin SV | 1… | 25 Base58Check bytes under 0x00 with the checksum | 3…, which no transaction may pay since Genesis; no Bech32, no CashAddr; 1… passes as Bitcoin too |
| eCash | CashAddr with or without ecash: | the CashAddr checksum, version 0x00 or 0x08, 20-byte hash, zero padding | nothing; a bare Bitcoin Cash address fails on the checksum |
| Cardano | Shelley addr1…, stake1…, Byron Ae2… DdzFF… | Bech32 checksum past BIP-173's 90 characters, CIP-19 header, mainnet tag, payload length, pointer coordinates; Byron's CRC-32, root, attributes and type as the ledger decodes them | nothing; a Byron address with a network magic is a testnet address |
| Decred | Ds De DS Dc, Dk | 26-byte hash forms, 39-byte public key form with its selector, the BLAKE-256 checksum | curve points |
| Solana | base58 | exactly 32 decoded bytes | nothing else exists to check |
| Stellar | SEP-23 Strkey G… M… C… | canonical base32, version byte, CRC16-XModem | account existence |
| XRP Ledger | classic r…, X-address X… | the ledger's own base58 alphabet, the Base58Check checksum, version 0x00, X-address prefix, flag and reserved bytes | nothing else |
| Aptos, Sui | 64 hex digits or the single digit AIP-40 short form | shape | nothing else |
| TON | TEP-2 friendly, either base64 alphabet | 36 bytes, tag 0x11 or 0x51, workchain 0x00 or 0xff, the CRC16-XModem | the raw workchain:hex form isn't accepted |
| TRON | T… | 25 Base58Check bytes under 0x41 with the checksum | nothing else |
| Octra | oct and 44 base58 characters | the width and the alphabet | everything else, on purpose |
| Arweave | 43 base64url characters | zero unused bits in the last digit | the optional :checksum suffix is rejected |
| Monero | standard 4…, subaddress 8…, integrated 4… | base58 block bounds, mainnet type byte, 95 or 106 characters, the Keccak-256 checksum | curve points; testnet and stagenet are rejected |
Mixed case fails on every Bech32 and CashAddr chain, all upper or all lower passes, as the specs say. On an EVM chain mixed case is the checksum, so it has to match, and one case throughout is the unchecksummed form every wallet accepts. Testnet forms are rejected everywhere the format can tell: Bitcoin's version bytes, TON's testnet flag, Monero's network byte, XRP's testnet X-address prefix, the network magic a Byron address carries only on a test network.
What it pays to
The validator takes the address apart to check it. Then it says yes and throws the parts away. Bit of a waste, right? decodeAddress runs the same check and keeps them:
import { getChain } from "@agntn/chains";
getChain("btc").decodeAddress("1Fo65aKq8s8iquMt6weF1rku1moWVEd5Ua");
// { kind: "p2pkh", payload: "a24922852051a9002ebf4c864a55acb75bb4cf75", hash: "hash160" }
getChain("bch").decodeAddress("bitcoincash:qz3yjg59ypg6jqpwhaxgvjj44jm4hdx0w5wsxw2qez");
// { kind: "p2pkh", payload: "a24922852051a9002ebf4c864a55acb75bb4cf75", hash: "hash160" }
getChain("dcr").decodeAddress("DeeUhrRoTp4DftsqddVW96yMGMW4sgQFYUE");
// { kind: "p2pkh", payload: "456d8ee57a4b9121987b4ecab8c3bcb5797e8a53", hash: "ripemd160-blake256", signature: "ed25519" }
getChain("eth").decodeAddress("0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984");
// { kind: "account" }
Same hash twice. That's one key, paid on two chains, and no block explorer was needed to see it.
A bad address throws the same InvalidAddressError as assertAddress, same .reason too. Nothing new to catch. payload is lowercase hex of whatever the address pays to. hash names the digest behind it. That matters on Decred, where the hash is RIPEMD-160 of BLAKE-256 and only looks like a HASH160.
| Chain | Kinds |
|---|---|
| Bitcoin, Litecoin, Bitcoin Gold | p2pkh, p2sh, p2wpkh, p2wsh, p2tr, p2a for the anchor output, and witness with a version for a program no BIP names yet. Litecoin adds mweb with both stealth keys as the payload |
| Dogecoin, Pepecoin, Dash | p2pkh, p2sh |
| Bitcoin SV | p2pkh |
| Bitcoin Cash, eCash | p2pkh, p2sh, with tokens: true on the CashTokens types and a 32-byte script hash as hash256 |
| Decred | p2pkh and p2sh under ripemd160-blake256, p2pk with the compressed key, and signature saying ECDSA, Ed25519 or Schnorr |
| Zcash | p2pkh and p2sh for t1… and t3…, tex, sapling with the 43-byte receiver, unified |
| Cardano | base, pointer, enterprise with the payment credential, reward with the stake one, all blake2b-224, and credential saying key or script. byron |
| every other chain | account |
A Unified Address and a Byron address get no payload. A Unified Address bundles several receivers, and picking one would be a guess. A Byron root hashes the spending data together with its attributes, so it isn't a key or script hash at all. A base address carries a stake credential too, but the coins sit behind the payment one, so that's the one you get. A custom chain on UTXO won't guess either. Until you override decodeAddress, it throws AddressDecodingUnsupportedError. The agent tools and chains validate print the kind and payload after a valid address.
Move addresses and EVM addresses
Aptos and Sui want all 32 bytes of hex written out, or the single digit short form AIP-40 defines for the special addresses, which is how the framework address 0x1 is actually written. Anything in between stays rejected. Accepting dropped leading zeros would make every EVM address a valid Move address too, and identify would report a family nobody asked about.
Transaction ids
import { getChain } from "@agntn/chains";
getChain("btc").assertTxid("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); // the genesis coinbase, returned
getChain("sol").assertTxid("4AwYqQ8RbD6yhTbE9h38YiYufC7UHJfxzhP3BZsEoJRp4cY6TzphNFEsUWMjkrKNjE5dFMbBoxVwvr9m8vJukavE"); // a signature, 64 bytes of base58
getChain("xlm").validatesTxid; // true, on every chain that ships
assertTxid is the same contract for a transaction id: the string back when it fits, InvalidTxidError with .chain, .txid and .reason when it doesn't. Every built-in chain fills in the reason. A Bitcoin txid pasted with 0x comes back with 0x in front, which this chain's txids never carry, an uppercase Stellar hash with uppercase hex digits, and this chain reads lowercase only. It's a shape check and nothing else, there's no transaction to hash. Each rule is what the chain's own node writes and reads, so the case rules differ where the readers do.
| Chain | A txid is |
|---|---|
| EVM, fourteen chains | 0x and 64 hex digits, either case. 0X is out, same as for an address |
| UTXO: Bitcoin, Litecoin, Pepecoin, Dogecoin, Bitcoin Cash, Bitcoin SV, Bitcoin Gold, Dash, Zcash, eCash, Cardano, Decred | 64 hex digits, either case, no prefix. Double SHA-256 on the Bitcoin forks, BLAKE on Cardano and Decred, BLAKE2b on Zcash since v5 transactions, same width either way |
| Monero | 64 hex digits, a Keccak-256 |
| Arweave | 43 base64url characters with the last digit's two spare bits zero, the address rule. A transaction id there is the SHA-256 of the signature |
| Solana | The first signature of the transaction, 64 Ed25519 bytes in base58, so up to 88 characters that decode to exactly 64 bytes, the way Signature::from_str reads them |
| Sui | A 32-byte digest in base58, decoded and counted the way TransactionDigest::from_str does. Base58, not hex, so the two Move chains don't share a rule |
| Aptos | 0x and 64 hex digits, either case, as the node writes them. The node also reads bare hex, but nothing writes it, so it stays out like it does for an address |
| TON | The 32-byte hash in either of the two spellings TON's own tools write: 64 hex digits, either case, as hash().toString("hex") and tonapi print it, or 44 characters of base64 with the =, in toncenter's alphabet or the URL-safe one tonscan links with, never a mix of the two. Unpadded base64 is read by toncenter and written by nobody, so it's out |
| TRON | 64 hex digits, either case, the SHA-256 of raw_data. No 0x: the node strips one on the way in, but nothing on TRON writes one |
| Stellar | 64 hex digits, lowercase only. Horizon's isTransactionHash refuses uppercase, so a hash that passes here passes there |
| XRP Ledger | 64 hex digits, uppercase as rippled writes them, either case as its parseHex reads them. A CTID (XLS-37) is a different parameter, not a hash |
| Octra | 64 hex digits, lowercase only, which is the node's whole sanitize_hash check |
A custom chain that never overrides assertTxid throws TxidValidationUnsupportedError, and validatesTxid says false there before you ask. chains_txid_validate and chains validate --txid are the same check for a model and for the terminal.
Where this stops
None of this touches a network. Not existence, not balances, not whether a contract sits at the address. That belongs in @agntn/nodes, and the Playground says the same thing under every green result.