SPB Git

Repository Public

address-vectors.gen.mts 28.3 KB 216 lines typescript

1// SP Wallet Core — generator of Tests/SPWalletCoreTests/Fixtures/address-registry.json and address-vectors.json.2// Runs the BACKEND's own code (normalizeAddress, NETWORKS, its encoders) over a candidate list and records its verdicts.3// Usage: cd ~/Desktop/Cluster/projets/sp-wallet && npx tsx /tmp/spw-vectors-address.mts <output-dir>4// No network access, no key material: "valid" synthetic addresses are encodings of SHA-256("spw-address-vector:<label>").5import { createHash } from 'node:crypto';6import { writeFileSync } from 'node:fs';7import { NETWORKS, normalizeAddress, cashaddrEncode, ss58Encode, stellarEncodeAccount, tonFormatAddress } from '/Users/simon-pierreboucher/Desktop/Cluster/projets/sp-wallet/packages/core/src/index.ts';8import { base58, base58xrp, bech32, bech32m, base32nopad, createBase58check } from '/Users/simon-pierreboucher/Desktop/Cluster/projets/sp-wallet/packages/core/node_modules/@scure/base/index.js';910const out = process.argv[2] ?? '/tmp/spw-gen';11const sha256 = (b: Uint8Array | string) => new Uint8Array(createHash('sha256').update(b).digest());12const h = (label: string, n: number) => Uint8Array.from([0, 1, 2].flatMap((k) => Array.from(sha256(`spw-address-vector:${label}${k ? `#${k}` : ''}`)))).slice(0, n);13const hex = (b: Uint8Array) => Buffer.from(b).toString('hex');14const cat = (...p: (Uint8Array | number[])[]) => Uint8Array.from(p.flatMap((x) => Array.from(x)));15const b58c = createBase58check(sha256);16const crc16 = (b: Uint8Array) => { let c = 0; for (const x of b) { c ^= x << 8; for (let k = 0; k < 8; k++) c = c & 0x8000 ? ((c << 1) ^ 0x1021) & 0xffff : (c << 1) & 0xffff; } return c; };1718// ---------------------------------------------------------------- registry19const registry = Object.values(NETWORKS).map((n) => ({20  id: n.id, family: n.family, chainRef: n.chainRef,21  ...(n.utxo ? { utxo: { ...(n.utxo.bech32 !== undefined ? { bech32: n.utxo.bech32 } : {}), pubKeyHash: n.utxo.pubKeyHash, scriptHash: n.utxo.scriptHash, kind: n.utxo.kind, ...(n.utxo.cashaddr !== undefined ? { cashaddr: n.utxo.cashaddr } : {}) } } : {}),22  ...(n.cosmos ? { cosmos: { bech32: n.cosmos.bech32 } } : {}),23}));24writeFileSync(`${out}/address-registry.json`, JSON.stringify(registry, null, 1) + '\n');2526// ---------------------------------------------------------------- candidates27type Cand = { input: string; kind: string };28const per: Record<string, Cand[]> = {};29const add = (net: string, kind: string, ...inputs: string[]) => { for (const input of inputs) (per[net] ??= []).push({ input, kind }); };30const seeds: Record<string, string[]> = {}; // well-formed addresses of each network: mutated below and offered to every other network31const seed = (net: string, kind: string, ...inputs: string[]) => { (seeds[net] ??= []).push(...inputs); add(net, kind, ...inputs); };3233// EVM: EIP-55 specification vectors, well-known public contracts/accounts, synthetic34const EVM_KNOWN = [35  '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed', '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359', '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB', '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb', // EIP-5536  '0x52908400098527886E0F7030069857D2E4169EE7', '0x8617E340B3D01FA5F11F306F4090FD50E238070D', '0xde709f2102306220921060314715629080e2fb77', '0x27b1fdb04752bbc536007a920d24acb045561c26', // EIP-55 all caps / all lower37  '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', '0x00000000219ab540356cBB839Cbe05303d7705Fa', // vitalik.eth, USDC, WETH, deposit contract38  '0x000000000000000000000000000000000000dEaD',39];40const evmIds = Object.values(NETWORKS).filter((n) => n.family === 'evm').map((n) => n.id);41for (const id of evmIds) {42  seed(id, 'known', ...EVM_KNOWN);43  seed(id, 'synthetic', '0x' + hex(h(`evm-${id}`, 20)), '0x' + hex(h('evm-shared', 20)));44  add(id, 'case', ...EVM_KNOWN.slice(0, 4).flatMap((a) => [a.toLowerCase(), '0x' + a.slice(2).toUpperCase(), a.toUpperCase(), '0X' + a.slice(2)]));45  add(id, 'reserved', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000001');46  add(id, 'shape', '0x', '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA9604', '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA960455', 'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA9604g',47    'vitalik.eth', '0x d8dA6BF26964aF9D7eEd9e03E53415D37aA96045', '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 extra', 'ethereum:0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',48    '0x' + hex(h('evm-32', 32)), '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045');49}5051// Bitcoin: genesis coinbase address, BIP-173 / BIP-350 vectors (valid and invalid), synthetic52const wit = (hrp: string, v: number, prog: Uint8Array, m = v !== 0) => (m ? bech32m : bech32).encode(hrp, [v, ...bech32.toWords(prog)]);53seed('bitcoin', 'known', '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',54  'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', 'bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3', 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0', 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq');55seed('bitcoin', 'synthetic', b58c.encode(cat([0x00], h('btc-pkh', 20))), b58c.encode(cat([0x05], h('btc-sh', 20))), wit('bc', 0, h('btc-wpkh', 20)), wit('bc', 0, h('btc-wsh', 32)), wit('bc', 1, h('btc-tr', 32)));56add('bitcoin', 'bip', 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4', 'BC1SW50QGDZ25J', 'bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs', 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y',57  'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7', 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx', 'tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c',58  'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd', 'BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL', 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh', 'bc1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47',59  'bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4', 'BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R', 'bc1pw5dgrnzv', 'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav',60  'bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf', 'bc1gmk9yu', 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5', 'bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90',61  'BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P', 'bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du', 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 ', 'Bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', 'bc1pfeessrawgf');62add('bitcoin', 'crafted', wit('bc', 0, h('btc-wpkh', 20), true), wit('bc', 1, h('btc-tr', 32), false), wit('bc', 1, h('x', 20)), wit('bc', 2, h('x', 32)), wit('bc', 16, h('x', 32)), 63  wit('tb', 0, h('btc-wpkh', 20)), wit('bcrt', 0, h('btc-wpkh', 20)), b58c.encode(cat([0x6f], h('btc-pkh', 20))), b58c.encode(cat([0xc4], h('btc-sh', 20))), b58c.encode(cat([0x80], h('x', 20))), b58c.encode(cat([0x00], h('x', 19))), b58c.encode(cat([0x00], h('x', 21))),64  base58.encode(cat([0x00], h('btc-pkh', 20), [1, 2, 3, 4])), b58c.encode(cat([0x00], new Uint8Array(20))), b58c.encode(cat([0x05], new Uint8Array(20))), wit('bc', 0, new Uint8Array(20)), wit('bc', 1, new Uint8Array(32)));6566// Litecoin67seed('litecoin', 'synthetic', b58c.encode(cat([0x30], h('ltc-pkh', 20))), b58c.encode(cat([0x32], h('ltc-sh', 20))), wit('ltc', 0, h('ltc-wpkh', 20)), wit('ltc', 0, h('ltc-wsh', 32)), wit('ltc', 1, h('ltc-tr', 32)),68  b58c.encode(cat([0x30], h('ltc-pkh2', 20))), wit('ltc', 0, h('ltc-wpkh2', 20)));69add('litecoin', 'crafted', wit('ltc', 0, h('ltc-wpkh', 20)).toUpperCase(), b58c.encode(cat([0x05], h('ltc-sh', 20))), b58c.encode(cat([0x00], h('ltc-pkh', 20))), wit('tltc', 0, h('ltc-wpkh', 20)), wit('ltc', 0, h('ltc-wpkh', 20), true), wit('ltc', 1, h('ltc-tr', 32), false),70  wit('ltc', 2, h('x', 32)), wit('ltc', 1, h('x', 20)), wit('bc', 0, h('ltc-wpkh', 20)), b58c.encode(cat([0x6f], h('ltc-pkh', 20))), b58c.encode(cat([0x3a], h('ltc-sh', 20))), 'ltc1', 'ltc1q', 'LTC1' + wit('ltc', 0, h('ltc-wpkh', 20)).slice(4));7172// Dogecoin73seed('dogecoin', 'synthetic', b58c.encode(cat([0x1e], h('doge-pkh', 20))), b58c.encode(cat([0x16], h('doge-sh', 20))), b58c.encode(cat([0x1e], h('doge-pkh2', 20))), b58c.encode(cat([0x16], h('doge-sh2', 20))),74  b58c.encode(cat([0x1e], h('doge-pkh3', 20))), b58c.encode(cat([0x1e], new Uint8Array(20))));75add('dogecoin', 'crafted', wit('none', 0, h('doge', 20)), wit('none', 1, h('doge', 32)), wit('doge', 0, h('doge', 20)), wit('bc', 0, h('doge', 20)), b58c.encode(cat([0x71], h('doge-pkh', 20))), b58c.encode(cat([0xc4], h('doge-sh', 20))),76  b58c.encode(cat([0x1e], h('doge', 19))), b58c.encode(cat([0x1e], h('doge', 32))), base58.encode(cat([0x1e], h('doge-pkh', 20), [0, 0, 0, 0])), 'D', 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD', 'none1', 'NONE1' + wit('none', 0, h('doge', 20)).slice(5));7778// Bitcoin Cash: CashAddr specification vectors + synthetic79seed('bitcoincash', 'known', 'bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a', 'bitcoincash:qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy', 'bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq', 'bitcoincash:pr95sy3j9xwd2ap32xkykttr4cvcu7as4yc93ky28e');80seed('bitcoincash', 'synthetic', cashaddrEncode('bitcoincash', 0, h('bch-pkh', 20)), cashaddrEncode('bitcoincash', 1, h('bch-sh', 20)), cashaddrEncode('bitcoincash', 0, h('bch-pkh2', 20)));81const bchA = cashaddrEncode('bitcoincash', 0, h('bch-pkh', 20));82add('bitcoincash', 'forms', bchA.split(':')[1]!, bchA.toUpperCase(), bchA.split(':')[1]!.toUpperCase(), 'BITCOINCASH:' + bchA.split(':')[1]!, 'bitcoincash:' + bchA.split(':')[1]!.toUpperCase(), 'BitcoinCash:' + bchA.split(':')[1]!,83  '1BpEi6DfDAUFd7GtittLSdBeYJvcoaVggu', '1KXrWXciRDZUpQwQmuM1DbwsKDLYAYsVLR', '3CWFddi6m4ndiGyKqzYvsFYagqDLPVMTzC', '16w1D5WRVKJuZUsSRzdLp9w3YGcgoxDXb', b58c.encode(cat([0x00], h('bch-pkh', 20))), b58c.encode(cat([0x05], h('bch-sh', 20))), b58c.encode(cat([0x00], new Uint8Array(20))),84  '1BpEi6DfDAUFd7GtittLSdBeYJvcoaVggv', b58c.encode(cat([0x00], h('bch', 19))), b58c.encode(cat([0x00], h('bch', 21))));85add('bitcoincash', 'crafted', cashaddrEncode('bitcoincash', 2, h('bch-pkh', 20)), cashaddrEncode('bitcoincash', 3, h('bch-sh', 20)), cashaddrEncode('bitcoincash', 0, h('bch', 32)), cashaddrEncode('bitcoincash', 1, h('bch', 32)), cashaddrEncode('bitcoincash', 15, h('bch', 20)), cashaddrEncode('bitcoincash', 0, h('bch', 24)),86  cashaddrEncode('bchtest', 0, h('bch-pkh', 20)), cashaddrEncode('bchreg', 0, h('bch-pkh', 20)), cashaddrEncode('ecash', 0, h('bch-pkh', 20)), cashaddrEncode('simpleledger', 0, h('bch-pkh', 20)), cashaddrEncode('bchtest', 0, h('bch-pkh', 20)).split(':')[1]!,87  bchA + ':', ':' + bchA.split(':')[1]!, 'bitcoincash::' + bchA.split(':')[1]!, 'bitcoincash:' + bchA, 'bitcoincash:', 'bitcoincash', 'bitcoincash:q', 'bitcoin cash:' + bchA.split(':')[1]!, 'bitcoincash1:' + bchA.split(':')[1]!, cashaddrEncode('bitcoincash', 0, new Uint8Array(20)));8889// Solana: programs and mints everyone can look up, synthetic90seed('solana', 'known', '11111111111111111111111111111111', 'So11111111111111111111111111111111111111112', 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 'Vote111111111111111111111111111111111111111', 'Stake11111111111111111111111111111111111111', 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');91seed('solana', 'synthetic', base58.encode(h('sol-1', 32)), base58.encode(h('sol-2', 32)), base58.encode(cat([0, 0, 0], h('sol-3', 29))));92add('solana', 'crafted', base58.encode(h('sol', 31)), base58.encode(cat(h('sol', 32), [1])), base58.encode(h('sol', 20)), base58.encode(cat(h('sol', 32), h('sol-b', 32))), '1111111111111111111111111111111', '111111111111111111111111111111111', '1' + base58.encode(h('sol-1', 32)),93  'So1111111111111111111111111111111111111111O', 'So1111111111111111111111111111111111111111l', 'So1111111111111111111111111111111111111111I', 'So11111111111111111111111111111111111111120', base58.encode(h('sol-1', 32)).toLowerCase(), base58.encode(h('sol-1', 32)).toUpperCase(), base58.encode(new Uint8Array(32).fill(255)), base58.encode(cat(new Uint8Array(32).fill(255), [0])).slice(0, 44));9495// Stellar: strkey forms96const strkey = (version: number, payload: Uint8Array) => { const body = cat([version], payload); const c = crc16(body); return base32nopad.encode(cat(body, [c & 0xff, c >> 8])); };97seed('stellar', 'synthetic', stellarEncodeAccount(h('xlm-1', 32)), stellarEncodeAccount(h('xlm-2', 32)), stellarEncodeAccount(h('xlm-3', 32)), stellarEncodeAccount(h('xlm-4', 32)), stellarEncodeAccount(h('xlm-5', 32)), stellarEncodeAccount(new Uint8Array(32)), stellarEncodeAccount(new Uint8Array(32).fill(255)));98const xlmA = stellarEncodeAccount(h('xlm-1', 32));99add('stellar', 'crafted', strkey(12 << 3, cat(h('xlm-1', 32), [0, 0, 0, 0, 0, 0, 0, 1])), strkey(2 << 3, h('xlm-1', 32)), strkey(19 << 3, h('xlm-1', 32)), strkey(6 << 3, h('xlm-1', 31)), strkey(6 << 3, h('xlm-1', 33)),100  xlmA.toLowerCase(), xlmA + '=', xlmA + '====', xlmA.slice(0, 55), xlmA + 'A', xlmA.slice(0, 55) + (xlmA.endsWith('A') ? 'B' : 'A'), xlmA.slice(0, 54) + '1' + xlmA.slice(55), xlmA.slice(0, 54) + '8' + xlmA.slice(55), 'G' + 'A'.repeat(55), base32nopad.encode(cat([6 << 3], h('xlm-1', 32), [0, 0])),101  (() => { const body = cat([6 << 3], h('xlm-1', 32)); const c = crc16(body); return base32nopad.encode(cat(body, [c >> 8, c & 0xff])); })(), xlmA.replace(/^G/, 'g'), xlmA.slice(0, 20) + ' ' + xlmA.slice(20));102103// XRP Ledger: genesis account, the two black-hole accounts, synthetic; X-addresses from the XRPL documentation104const xrp = (id: Uint8Array, version = 0) => { const body = cat([version], id); return base58xrp.encode(cat(body, sha256(sha256(body)).slice(0, 4))); };105seed('xrpl', 'known', 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', 'rrrrrrrrrrrrrrrrrrrrrhoLvTp', 'rrrrrrrrrrrrrrrrrrrrBZbvji', 'rHsMGQEkVNJmpGWs8XUBoTBiAAbwxZN5v3');106seed('xrpl', 'synthetic', xrp(h('xrp-1', 20)), xrp(h('xrp-2', 20)), xrp(h('xrp-3', 20)), xrp(cat([0, 0], h('xrp-4', 18))));107const xrpA = xrp(h('xrp-1', 20));108add('xrpl', 'crafted', 'X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ', 'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi', 'T719a5UwUCnEs54UsxG9CJYYDhwmFCqkr7wxCcNcfZ6p5GZ', xrp(h('xrp-1', 20), 0x05), xrp(h('xrp-1', 20), 0x23), xrp(h('xrp-1', 19)), xrp(h('xrp-1', 21)),109  base58xrp.encode(cat([0], h('xrp-1', 20), [0, 0, 0, 0])), base58.encode(cat([0], h('xrp-1', 20), sha256(sha256(cat([0], h('xrp-1', 20)))).slice(0, 4))), b58c.encode(cat([0], h('xrp-1', 20))), xrpA.toLowerCase(), xrpA.toUpperCase(), 'R' + xrpA.slice(1), xrpA.slice(0, -1), xrpA + 'r', 'r' + xrpA,110  'r', 'rrrrrrrrrrrrrrrrrrrrrrrrr', 'r'.repeat(35), xrpA.replace(/.$/, '0'), xrpA + ':12345', xrpA + '?dt=1');111112// TRON: USDT contract (repo test + public), synthetic113const trx = (id: Uint8Array, version = 0x41) => b58c.encode(cat([version], id));114seed('tron', 'known', 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', 'TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7', 'TNUC9Qb1rRpS5CbWLmNMxXBjyFoydXjWFR');115seed('tron', 'synthetic', trx(h('trx-1', 20)), trx(h('trx-2', 20)), trx(h('trx-3', 20)), trx(h('trx-4', 20)), trx(cat([0, 0, 0], h('trx-5', 17))));116const trxA = trx(h('trx-1', 20));117add('tron', 'crafted', trx(new Uint8Array(20)), trx(h('trx-1', 20), 0xa0), trx(h('trx-1', 20), 0x42), trx(h('trx-1', 20), 0x00), trx(h('trx-1', 19)), trx(h('trx-1', 21)), '41' + hex(h('trx-1', 20)), '0x41' + hex(h('trx-1', 20)), '0x' + hex(h('trx-1', 20)),118  base58.encode(cat([0x41], h('trx-1', 20), [9, 9, 9, 9])), trxA.toLowerCase(), trxA.toUpperCase(), 't' + trxA.slice(1), trxA.slice(0, -1), trxA + '1', 'T', 'T'.repeat(34), trxA.replace(/.$/, 'O'), trxA.replace(/.$/, 'l'), 'tron:' + trxA);119120// Aptos, Sui: 32-byte hexadecimal121for (const net of ['aptos', 'sui']) {122  const a = '0x' + hex(h(`${net}-1`, 32));123  seed(net, 'synthetic', a, '0x' + hex(h(`${net}-2`, 32)), '0x' + hex(h(`${net}-3`, 32)), '0x' + hex(h(`${net}-4`, 32)), '0x' + hex(cat([0, 0, 0, 0], h(`${net}-5`, 28))), '0x' + 'f'.repeat(64));124  add(net, 'case', '0x' + a.slice(2).toUpperCase(), '0X' + a.slice(2), a.toUpperCase(), '0x' + a.slice(2, 34).toUpperCase() + a.slice(34));125  add(net, 'reserved', ...['0', '1', '2', '5', '6', 'a', 'f', '10', '11', 'ff', '100', 'ffff', '10000', '10001', 'fffff'].map((t) => '0x' + t.padStart(64, '0')), '0x' + '0'.repeat(63) + 'A', '0x' + '0'.repeat(63) + 'F', '0x' + '0'.repeat(60) + 'FFFF', '0x' + '0'.repeat(59) + '1' + '0'.repeat(4));126  add(net, 'shape', '0x1', '0x2', '0xa', '0x', a.slice(2), a.slice(0, -1), a + '0', a.slice(0, -1) + 'g', '0x' + hex(h('x', 20)), '0x0' + a.slice(2), ' 0x' + a.slice(2, 40) + ' ' + a.slice(40), a + '::coin::Coin', '1x' + a.slice(2), '0×' + a.slice(2));127}128129// Cosmos Hub130const cos = (prefix: string, bytes: Uint8Array, m = false) => (m ? bech32m : bech32).encode(prefix, bech32.toWords(bytes));131seed('cosmos', 'known', 'cosmos19rl4cm2hmr8afy4kldpxz3fka4jguq0auqdal4');132seed('cosmos', 'synthetic', cos('cosmos', h('atom-1', 20)), cos('cosmos', h('atom-2', 20)), cos('cosmos', h('atom-3', 20)), cos('cosmos', h('atom-4', 20)), cos('cosmos', h('atom-5', 20)), cos('cosmos', new Uint8Array(20)), cos('cosmos', new Uint8Array(20).fill(255)));133const cosA = cos('cosmos', h('atom-1', 20));134add('cosmos', 'crafted', cos('osmo', h('atom-1', 20)), cos('cosmosvaloper', h('atom-1', 20)), cos('cosmosvalcons', h('atom-1', 20)), cos('cosmospub', h('atom-1', 20)), cos('cosmos', h('atom-1', 32)), cos('cosmos', h('atom-1', 19)), cos('cosmos', h('atom-1', 21)), cos('cosmos', h('atom-1', 20), true),135  cos('cosmo', h('atom-1', 20)), cos('cosmos1', h('atom-1', 20)), cos('cosmos1cosmos', h('atom-1', 20)), cos('kava', h('atom-1', 20)), cos('celestia', h('atom-1', 20)), cos('bc', h('atom-1', 20)), cosA.toUpperCase(), 'Cosmos' + cosA.slice(6), cosA.slice(0, 10) + cosA[10]!.toUpperCase() + cosA.slice(11),136  cosA.slice(0, -1), cosA + 'q', 'cosmos1', 'cosmos', cosA.replace(/.$/, 'b'), cosA.replace(/.$/, '1'), bech32.encode('cosmos', [...bech32.toWords(h('atom-1', 20)), 0]), 'cosmos1' + 'q'.repeat(38));137138// NEAR139seed('near', 'named', 'alice.near', 'near', 'aurora', 'wrap.near', 'a-b_c.d1', 'ab', 'app.nearcrowd.near', '100', 'a.b.c.d.e', 'x'.repeat(64), 'bridge.near', 'tkn.near');140seed('near', 'implicit', hex(h('near-1', 32)), hex(h('near-2', 32)), hex(h('near-3', 32)), '0'.repeat(64), 'f'.repeat(64));141add('near', 'crafted', 'a', 'A.near', 'alice.Near', 'ALICE.NEAR', 'a..b', '.near', 'near.', 'a--b', 'a__b', 'a.-b', '-a', 'a-', '_a', 'a_', 'system', 'system.near', 'system1', 'al ice', 'alice@near', 'alice.near/', 'x'.repeat(65), hex(h('near-1', 32)).toUpperCase(), hex(h('near-1', 32)) + '0', hex(h('near-1', 32)).slice(1),142  '0x' + hex(h('near', 20)), '0s' + hex(h('near', 20)), '0u' + hex(h('near', 20)), '0x' + hex(h('near', 20)).toUpperCase(), '0x' + hex(h('near', 19)), '0' + hex(h('near', 20)), '00' + hex(h('near', 20)), '0a' + hex(h('near', 20)).replace(/.$/, 'g'), 'é.near', 'alice.neaŕ', 'alice.near', '1', '', '..', 'a.b.', 'near:alice.near');143144// TON145const tonRaw = (tag: number, wc: number, id: Uint8Array, url = true, breakCrc = false) => { const body = cat([tag, wc & 0xff], id); const c = crc16(body) ^ (breakCrc ? 1 : 0); const s = Buffer.from(cat(body, [c >> 8, c & 0xff])).toString('base64'); return url ? s.replace(/\+/g, '-').replace(/\//g, '_') : s; };146const tonIds = [h('ton-1', 32), h('ton-2', 32), h('ton-3', 32), h('ton-4', 32), cat([0xfb, 0xff, 0xbf], h('ton-5', 29)), cat([0xff, 0xef, 0xbe], h('ton-6', 29))];147for (const id of tonIds) { seed('ton', 'synthetic', tonFormatAddress(id)); add('ton', 'forms', tonFormatAddress(id, { bounceable: true }), tonRaw(0x51, 0, id, false), tonRaw(0x11, 0, id, false), `0:${hex(id)}`, `0:${hex(id).toUpperCase()}`); }148seed('ton', 'known', 'UQAzWZa6nM5mJev91wGc7VCSfBoIsYRqKJpV78N8Add9-RKY');149const tonA = tonFormatAddress(tonIds[0]!);150add('ton', 'crafted', tonRaw(0x91, 0, tonIds[0]!), tonRaw(0xd1, 0, tonIds[0]!), tonRaw(0x51, 0xff, tonIds[0]!), tonRaw(0x11, 0xff, tonIds[0]!), tonRaw(0x51, 1, tonIds[0]!), tonRaw(0x00, 0, tonIds[0]!), tonRaw(0x50, 0, tonIds[0]!), tonRaw(0x31, 0, tonIds[0]!), tonRaw(0x51, 0, tonIds[0]!, true, true),151  tonRaw(0x51, 0, new Uint8Array(32)), tonRaw(0x11, 0, new Uint8Array(32)), `0:${'0'.repeat(64)}`, `-1:${hex(tonIds[0]!)}`, `1:${hex(tonIds[0]!)}`, `00:${hex(tonIds[0]!)}`, `-0:${hex(tonIds[0]!)}`, `+0:${hex(tonIds[0]!)}`, `0:${hex(tonIds[0]!).slice(1)}`, `0:${hex(tonIds[0]!)}0`, `0:0x${hex(tonIds[0]!)}`, `:${hex(tonIds[0]!)}`, `0: ${hex(tonIds[0]!)}`, `٠:${hex(tonIds[0]!)}`, hex(tonIds[0]!),152  tonA.slice(0, 47), tonA + 'A', tonA + '=', tonA.slice(0, 44) + '====', '-_'.repeat(12) + '+/'.repeat(12), tonA.slice(0, 20) + '+' + tonA.slice(21, 30) + '_' + tonA.slice(31), tonA.toLowerCase(), tonA.toUpperCase(), 'ton://transfer/' + tonA, tonA.slice(0, 10) + ' ' + tonA.slice(11), tonA.slice(0, 10) + '.' + tonA.slice(11));153154// Cardano: CIP-19 specification vectors (also in the repo's tests) + synthetic155const ada = (header: number, len: number, label: string, prefix = 'addr') => bech32.encode(prefix, bech32.toWords(cat([header], h(label, len))), 128);156seed('cardano', 'known', 'addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x', 'addr1yx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzerkr0vd4msrxnuwnccdxlhdjar77j6lg0wypcc9uar5d2shs2z78ve', 'addr1vx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzers66hrl8');157seed('cardano', 'synthetic', ada(0x01, 56, 'ada-0'), ada(0x21, 56, 'ada-2'), ada(0x61, 28, 'ada-6'), ada(0x01, 56, 'ada-0b'), ada(0x61, 28, 'ada-6b'));158const adaA = ada(0x01, 56, 'ada-0'), adaE = ada(0x61, 28, 'ada-6');159add('cardano', 'cip19', 'addr1z8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gten0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgs9yc0hh', 'addr1x8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gt7r0vd4msrxnuwnccdxlhdjar77j6lg0wypcc9uar5d2shskhj42g', 'addr1gx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer5pnz75xxcrzqf96k', 'addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx',160  'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgs68faae', 'stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw', 'Ae2tdPwUPEZFRbyhz3cpfC2CumGzNkFBN2L42rcUc2yjQpEkxDbkPodpMAi');161add('cardano', 'crafted', adaA.toUpperCase(), adaE.toUpperCase(), 'ADDR1' + adaA.slice(5), 'addr1' + adaA.slice(5).toUpperCase(), adaA.slice(0, 30) + adaA[30]!.toUpperCase() + adaA.slice(31), ada(0x11, 56, 'ada-1'), ada(0x31, 56, 'ada-3'), ada(0x71, 28, 'ada-7'), ada(0x41, 31, 'ada-4'), ada(0x51, 31, 'ada-5'), ada(0x81, 56, 'ada-8'), ada(0xe1, 28, 'ada-14', 'stake'), ada(0xe1, 28, 'ada-14'), ada(0xf1, 28, 'ada-15'),162  ada(0x00, 56, 'ada-0'), ada(0x60, 28, 'ada-6'), ada(0x02, 56, 'ada-0'), ada(0x0f, 56, 'ada-0'), ada(0x00, 56, 'ada-0', 'addr_test'), ada(0x01, 56, 'ada-0', 'addr_test'), ada(0x01, 28, 'ada-0'), ada(0x61, 56, 'ada-6'), ada(0x01, 55, 'ada-0'), ada(0x01, 57, 'ada-0'), ada(0x61, 27, 'ada-6'), ada(0x61, 29, 'ada-6'), ada(0x01, 60, 'ada-0'),163  bech32m.encode('addr', bech32.toWords(cat([0x01], h('ada-0', 56))), 128), bech32.encode('addr', [...bech32.toWords(cat([0x61], h('ada-6', 28))), 0], 128), ada(0x01, 56, 'ada-0', 'addr1'), ada(0x01, 56, 'ada-0', 'ada'), adaA.slice(0, -1), adaA + 'q', adaA.replace(/.$/, 'b'), 'addr1', 'addr', adaA.slice(0, 50) + ' ' + adaA.slice(50), 'web+cardano:' + adaA);164165// Polkadot: public addresses used in the repo's tests + synthetic166seed('polkadot', 'known', '13nN6BGAoJwd7Nw1XxeBCx5YcBXuYnL94Mh7i3xBprqVSsFk', '15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5');167seed('polkadot', 'synthetic', ss58Encode(0, h('dot-1', 32)), ss58Encode(0, h('dot-2', 32)), ss58Encode(0, h('dot-3', 32)), ss58Encode(0, h('dot-4', 32)), ss58Encode(0, new Uint8Array(32).fill(255)), ss58Encode(0, cat([0, 0], h('dot-5', 30))));168const dotA = ss58Encode(0, h('dot-1', 32));169const ss58x = (bytes: Uint8Array) => base58.encode(bytes);170add('polkadot', 'crafted', ss58Encode(2, h('dot-1', 32)), ss58Encode(42, h('dot-1', 32)), ss58Encode(1, h('dot-1', 32)), ss58Encode(63, h('dot-1', 32)), ss58Encode(0, new Uint8Array(32)), ss58x(cat([0], h('dot-1', 32), [0, 0])), ss58x(cat([0], h('dot-1', 32), sha256(cat([0], h('dot-1', 32))).slice(0, 2))),171  ss58x(cat([0], h('dot-1', 31), [1, 2])), ss58x(cat([0], h('dot-1', 33), [1, 2])), ss58x(cat([0], h('dot-1', 32))), b58c.encode(cat([0], h('dot-1', 31))), dotA.toLowerCase(), dotA.toUpperCase(), dotA.slice(0, -1), dotA + '1', '1' + dotA, dotA.slice(1), dotA.replace(/.$/, '0'), dotA.replace(/.$/, 'l'), '1', '1'.repeat(35), '1'.repeat(48), '1'.repeat(49), dotA.slice(0, 20) + ' ' + dotA.slice(20), '0x' + hex(h('dot-1', 32)));172173// ---------------------------------------------------------------- systematic mutations of each network's own addresses174const B58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';175const other = (c: string) => { if (/[0-9]/.test(c)) return c === '7' ? '2' : '7'; if (/[a-z]/.test(c)) return c === 'q' ? 'p' : 'q'; if (/[A-Z]/.test(c)) return c === 'Q' ? 'P' : 'Q'; return 'x'; };176for (const [net, list] of Object.entries(seeds)) {177  const uniq = [...new Set(list)];178  uniq.forEach((a, idx) => {179    add(net, 'whitespace', ` ${a}`, `${a} `, `\t${a}\r\n`, ` ${a} `, `${a}`, `
${a} `, `​${a}`, `${a}​`, `${a}\u0085`, `᠎${a}`, `${a}\n${a}`, `${a.slice(0, a.length >> 1)} ${a.slice(a.length >> 1)}`, `${a}\u0000`);180    if (idx < 5) {181      const pos = [...new Set([0, 1, 2, 3, a.length >> 2, a.length >> 1, a.length - 3, a.length - 2, a.length - 1])].filter((p) => p >= 0 && p < a.length);182      for (const p of pos) add(net, 'one-char', a.slice(0, p) + other(a[p]!) + a.slice(p + 1));183      add(net, 'length', a.slice(0, -1), a.slice(1), a + a[a.length - 1], a + B58[idx]!, a.slice(0, a.length >> 1) + a.slice((a.length >> 1) + 1), a + a);184      add(net, 'case', a.toUpperCase(), a.toLowerCase(), a.slice(0, a.length >> 1).toUpperCase() + a.slice(a.length >> 1).toLowerCase(), a.slice(0, a.length >> 1).toLowerCase() + a.slice(a.length >> 1).toUpperCase());185      for (const p of pos.slice(4)) { const c = a[p]!; const f = c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase(); if (f !== c) add(net, 'case', a.slice(0, p) + f + a.slice(p + 1)); }186      // Unicode look-alikes: KELVIN SIGN (lower-cases to "k"), LONG S (upper-cases to "S"), DOTLESS I, fullwidth, combining mark, Cyrillic187      add(net, 'unicode', a.toUpperCase().replace(/K/, 'K'), a.toUpperCase().replace(/K/g, 'K'), a.replace(/[kK]/, 'K'), a.replace(/s/, 'ſ'), a.toUpperCase().replace(/I/, 'ı'), a.toUpperCase().replace(/I/, 'İ'), a.replace(/a/, 'а'), a.replace(/e/, 'е'),188        a + '́', String.fromCodePoint(a.codePointAt(0)! + 0xfee0) + a.slice(1), a.toUpperCase() + 'ß', a.toUpperCase().replace(/S/, 'ẞ'), a.replace(/1/, '١'), a.replace(/0/, '٠'), a + '\u{1f600}');189    }190  });191}192// every network also sees one or two addresses of every other network, and generic junk193const foreign = Object.entries(seeds).flatMap(([net, list]) => [...new Set(list)].slice(0, net.startsWith('bitcoin') || net === 'litecoin' || net === 'cardano' || net === 'ton' ? 8 : 3).map((a) => ({ net, a })));194const junk = ['', ' ', '\n', '0', '1', '0x', 'null', 'undefined', 'address', 'abc', 'q'.repeat(42), '1'.repeat(34), 'a'.repeat(64), '0'.repeat(64), 'A'.repeat(56), 'z'.repeat(200), 'https://example.com/', 'javascript:alert(1)', '<script>', '%00', '\u0000', 'K', 'ſ', 'İ', '😀', '0x' + 'g'.repeat(40), '-1', '0:0', ':', '::', 'bitcoin:', 'ethereum:', '../../etc/passwd', "' OR 1=1 --"];195for (const net of Object.keys(NETWORKS)) {196  for (const f of foreign) if (f.net !== net) add(net, `foreign:${f.net}`, f.a);197  add(net, 'junk', ...junk);198}199200// ---------------------------------------------------------------- backend verdicts201const cases: { network: string; kind: string; input: string; valid: boolean; normalized?: string; error?: string }[] = [];202const summary: Record<string, { valid: number; invalid: number }> = {};203for (const net of Object.keys(NETWORKS)) {204  const seen = new Set<string>();205  summary[net] = { valid: 0, invalid: 0 };206  for (const c of per[net] ?? []) {207    if (seen.has(c.input) || !c.input.isWellFormed()) continue; // a Swift String cannot hold a lone surrogate208    seen.add(c.input);209    try { const normalized = normalizeAddress(net, c.input); cases.push({ network: net, kind: c.kind, input: c.input, valid: true, normalized }); summary[net]!.valid++; }210    catch (e) { cases.push({ network: net, kind: c.kind, input: c.input, valid: false }); summary[net]!.invalid++; }211  }212}213// an id the registry does not know: the backend throws "unknown network"214writeFileSync(`${out}/address-vectors.json`, JSON.stringify({ source: 'normalizeAddress of packages/core/src/address.ts (sp-wallet), run by Fixtures/address-vectors.gen.mts', networks: Object.keys(NETWORKS), cases }) + '\n');215console.log(JSON.stringify(summary), cases.length);216
Files main
Menu

Theme