Crypto addresses
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.
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
| Field | Meaning |
|---|---|
network | network name (bitcoin, cosmos:osmo, substrate:1284, a custom name) |
kind | P2pkh, P2sh, P2wpkh, P2wsh, P2tr, WitnessUnknown, Account, Base / Pointer / Enterprise / Reward (Cardano), Bounceable / NonBounceable / Raw (TON), Standard / Integrated / Subaddress (Monero) |
testnet | testnet / stagenet address; always false where the address does not encode the network |
normalized | EIP-55 case (Ethereum), lowercase (bech32, raw TON), bitcoincash: + lowercase (CashAddr), the input otherwise |
payload | decoded 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
Network | Forms | Rules checked | Spec |
|---|---|---|---|
Bitcoin | 1… / 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 case | BIP-173, BIP-350 |
Litecoin | L… / M… / 3…, ltc1… | versions 0x30 / 0x32 / 0x05 (testnet 0x6f / 0x3a / 0xc4), hrp ltc / tltc | Litecoin chainparams |
Dogecoin | D…, A… / 9… | versions 0x1e / 0x16 (testnet 0x71 / 0xc4) | Dogecoin chainparams |
BitcoinCash | bitcoincash: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 Bitcoin | CashAddr spec |
Ethereum | 0x + 40 hex | EIP-55 checksum when mixed case; all-lower / all-upper accepted unless .checksummed(). Any EVM chain (BSC, Polygon, Arbitrum, …) | EIP-55 |
Tron | T… | Base58Check, version 0x41, 20-byte account | TRON docs |
Solana | Base58 | decodes to exactly 32 bytes. No checksum: a typo that still decodes to 32 bytes passes | Solana docs |
Xrp | r… | Base58Check in the Ripple alphabet, version 0x00. X-addresses are not accepted | XRPL "Address encoding" |
Cardano | addr1…, 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 accepted | CIP-19 |
Cosmos | cosmos1… | bech32 (not bech32m), 20- or 32-byte payload | Cosmos SDK |
Polkadot / Kusama / Substrate | SS58 | Base58, 1- or 2-byte prefix 0 / 2 / 42, 32-byte account id or 33-byte ECDSA key, 2-byte blake2b-512("SS58PRE" ++ data) checksum | SS58 spec |
Ton | EQ… / 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 |
Monero | 4… / 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
reason | When |
|---|---|
encoding | a character outside the alphabet, undecodable data, bad bech32 padding, unknown witness version / header type |
length | wrong character count, decoded length, witness program / payload length |
prefix | wrong version byte, hrp, CashAddr / custom prefix, SS58 network prefix, TON flags; a valid address of another chain |
checksum | Base58Check / bech32 / CashAddr / SS58 / CRC16 / keccak checksum, bech32 instead of bech32m (or the reverse), EIP-55 case |
case | mixed case in bech32 or CashAddr |
network | testnet where mainnet() is set (or the reverse); Cardano network id not matching the hrp |
kind | an 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);
| Builder | Meaning |
|---|---|
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.