Schemas

Crypto addresses

vld::crypto — wallet addresses of Bitcoin, Ethereum, TRON, Solana, Cardano, Polkadot, TON, Monero and more, with real checksum rules, plus your own templates.

Feature crypto (dependencies sha2, sha3, blake2, all RustCrypto, MIT / Apache-2.0). Addresses are checked offline against their network's encoding, checksum, version / prefix and length rules.

A valid address is well-formed for its network. Whether it exists, has ever been used, holds funds or belongs to the person who typed it cannot be checked offline. A valid address of the right network with a typo that the checksum misses (Solana has none; Base58Check misses one typo in 2³²) still passes.

On a string

use vld::prelude::*;
use vld::crypto::Network;

let btc = vld::string().trim().crypto_address(Network::Bitcoin);
assert!(btc.parse(r#""bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4""#).is_ok());

let err = btc.parse(r#""bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5""#).unwrap_err();
assert_eq!(err.issues[0].code.key(), "invalid_crypto_address");
// params: network = "bitcoin", reason = "checksum"
// message: "Invalid bitcoin address (checksum)"

Several networks: crypto_address_any([Network::Bitcoin, Network::Ethereum]). The first network that accepts the value wins. A failure names the network whose checksum / network / kind rule failed, or every network joined with | (bitcoin|ethereum) when none recognised the shape.

crypto_address_msg(network, "...") sets the message; with_messages uses the key invalid_crypto_address.

Typed: what was detected

vld::crypto::address(network) (and address_any) outputs a CryptoAddress:

use vld::prelude::*;
use vld::crypto::{AddressKind, Network};

let a = vld::crypto::address(Network::Bitcoin)
    .parse(r#""BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4""#)
    .unwrap();
assert_eq!(a.network, "bitcoin");
assert_eq!(a.kind, AddressKind::P2wpkh);
assert!(!a.testnet);
assert_eq!(a.normalized, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4");
assert_eq!(a.payload.len(), 20); // the witness program
FieldMeaning
networknetwork name (bitcoin, cosmos:osmo, substrate:1284, a custom name)
kindP2pkh, P2sh, P2wpkh, P2wsh, P2tr, WitnessUnknown, Account, Base / Pointer / Enterprise / Reward (Cardano), Bounceable / NonBounceable / Raw (TON), Standard / Integrated / Subaddress (Monero)
testnettestnet / stagenet address; always false where the address does not encode the network
normalizedEIP-55 case (Ethereum), lowercase (bech32, raw TON), bitcoincash: + lowercase (CashAddr), the input otherwise
payloaddecoded data without version and checksum (hash, key, witness program, account id)

CryptoNetwork::validate(&str) gives the same result (or the CryptoAddressReason) without a schema.

Networks

NetworkFormsRules checkedSpec
Bitcoin1… / 3…, bc1q…, bc1p…Base58Check versions 0x00 / 0x05 (testnet 0x6f / 0xc4); SegWit v0 bech32, v1+ bech32m, hrp bc / tb, program 2–40 bytes (v0: 20 or 32), ≤ 90 chars, no mixed caseBIP-173, BIP-350
LitecoinL… / M… / 3…, ltc1…versions 0x30 / 0x32 / 0x05 (testnet 0x6f / 0x3a / 0xc4), hrp ltc / tltcLitecoin chainparams
DogecoinD…, A… / 9…versions 0x1e / 0x16 (testnet 0x71 / 0xc4)Dogecoin chainparams
BitcoinCashbitcoincash:q…, legacy 1… / 3…CashAddr: prefix bitcoincash / bchtest (optional: the checksum covers it), 40-bit polymod, version byte type (P2PKH / P2SH) and hash size; legacy as BitcoinCashAddr spec
Ethereum0x + 40 hexEIP-55 checksum when mixed case; all-lower / all-upper accepted unless .checksummed(). Any EVM chain (BSC, Polygon, Arbitrum, …)EIP-55
TronT…Base58Check, version 0x41, 20-byte accountTRON docs
SolanaBase58decodes to exactly 32 bytes. No checksum: a typo that still decodes to 32 bytes passesSolana docs
Xrpr…Base58Check in the Ripple alphabet, version 0x00. X-addresses are not acceptedXRPL "Address encoding"
Cardanoaddr1…, addr_test1…, stake1…, stake_test1…bech32 without the 90-char limit, header type (base 57 bytes, pointer with three var-length naturals, enterprise / reward 29), network id 1 / 0 matching the hrp. Byron addresses are not acceptedCIP-19
Cosmoscosmos1…bech32 (not bech32m), 20- or 32-byte payloadCosmos SDK
Polkadot / Kusama / SubstrateSS58Base58, 1- or 2-byte prefix 0 / 2 / 42, 32-byte account id or 33-byte ECDSA key, 2-byte blake2b-512("SS58PRE" ++ data) checksumSS58 spec
TonEQ… / UQ… (48 chars), 0:<hex>base64 / base64url (canonical) of flags (0x11 bounceable, 0x51 non-bounceable, +0x80 testnet) + workchain + 32-byte hash + CRC16-XMODEM; raw <workchain>:<64 hex> (workchain −128..127)TON docs
Monero4… / 8… (95 chars), integrated (106)Monero Base58 (8-byte blocks), keccak-256 4-byte checksum, network byte 18 / 19 / 42 (testnet 53 / 54 / 63, stagenet 24 / 25 / 36)Monero source

Other Cosmos chains and Substrate prefixes:

use vld::crypto::CryptoNetwork;

let osmo = vld::string().crypto_address(CryptoNetwork::cosmos("osmo"));  // named "cosmos:osmo"
let moonbeam = vld::string().crypto_address(CryptoNetwork::ss58(1284));  // named "substrate:1284"

Options

use vld::crypto::{AddressKind, Network};

vld::string().crypto_address(Network::Bitcoin.mainnet());                         // tb1… fails: reason "network"
vld::string().crypto_address(Network::Bitcoin.kinds(&[AddressKind::P2wpkh, AddressKind::P2tr]));
vld::string().crypto_address(Network::Ethereum.checksummed());                    // all-lowercase fails: "checksum"

mainnet() / testnet() do nothing where the address does not encode the network (Ethereum, TRON, Solana, XRP, Cosmos, SS58, raw TON).

Reasons

reasonWhen
encodinga character outside the alphabet, undecodable data, bad bech32 padding, unknown witness version / header type
lengthwrong character count, decoded length, witness program / payload length
prefixwrong version byte, hrp, CashAddr / custom prefix, SS58 network prefix, TON flags; a valid address of another chain
checksumBase58Check / bech32 / CashAddr / SS58 / CRC16 / keccak checksum, bech32 instead of bech32m (or the reverse), EIP-55 case
casemixed case in bech32 or CashAddr
networktestnet where mainnet() is set (or the reverse); Cardano network id not matching the hrp
kindan address kind not in kinds(...)

Custom templates

Networks that are not built in are composed from primitives with CryptoNetwork::custom(name) and used wherever a network is:

use vld::prelude::*;
use vld::crypto::{Alphabet, Checksum, CryptoNetwork};

// Base58Check with version 0x1e, 34 characters (Dogecoin P2PKH by hand).
let mycoin = CryptoNetwork::custom("mycoin").base58check(&[0x1e]).length(34);
let s = vld::string().crypto_address(mycoin);
assert!(s.parse(r#""DH5yaieqoZN36fDVciNyRueRGvGLR3mr7L""#).is_ok());

// EVM-style hex with a required EIP-55 checksum.
let evm = CryptoNetwork::custom("evm").prefix("0x").hex().payload_len(20).checksum(Checksum::Eip55);

// bech32m with two hrps and a 32-byte payload.
let chain = CryptoNetwork::custom("chain").bech32m(&["ch", "tch"]).payload_len(32);

// Ripple-alphabet Base58 with a CRC32 and a two-byte version.
let other = CryptoNetwork::custom("other")
    .base58(Alphabet::Ripple)
    .version_bytes(&[&[0x05, 0x44]])
    .checksum(Checksum::Crc32)
    .payload_len_range(20, 32);
BuilderMeaning
base58(Alphabet)plain Base58: Alphabet::Bitcoin, Ripple, Flickr
base58check(&[versions])Base58 (Bitcoin alphabet, change with alphabet(..)) + Checksum::Sha256d + one leading version byte
version_bytes(&[&[..]])allowed leading version byte sequences (multi-byte)
bech32(&[hrps]) / bech32m(&[hrps])BIP-173 / BIP-350 with one of the hrps (any when empty); payload = regrouped data
hex()hex, either case (0x goes in prefix)
base64() / base64url()canonical base64, padding optional
prefix("…")literal before the encoded part
checksum(Checksum)None, Sha256d, Keccak256 (4 bytes), Eip55 (hex case), Ss58 (2 bytes blake2b), Crc16Xmodem, Crc32 (big-endian)
payload_len(n) / payload_len_range(min, max)decoded payload bytes (after version, before checksum)
length(n) / length_range(min, max)characters, prefix included
pattern(regex)whole-address regex (feature regex); with no encoding set, the template is pattern + length only

Checks run in this order: length, pattern, prefix, decoding, checksum, version, payload length. template.encode(&payload) builds the address of a payload (first version / hrp), which is handy for fixtures.

JSON Schema, TypeScript, fake data

The JSON Schema carries format: "crypto-address", x-network (a name, or a list for crypto_address_any), a pattern approximating the shape (never stricter than the check) and, when set, x-testnet, x-kinds, x-checksummed:

{ "type": "string", "format": "crypto-address", "x-network": "ethereum",
  "pattern": "^(?:0x[0-9a-fA-F]{40})$", "x-checksummed": true }

vld::ts generates the pattern as a regex with a comment that the checksum is not checked. vld::fake generates valid addresses (random payload, correct version bytes and checksum) for every built-in network, Cosmos hrp and SS58 prefix, honouring x-testnet / x-kinds; CryptoNetwork::random_address(rng) does the same in Rust, for custom templates too.