|
| 1 | +/* eslint-disable no-magic-numbers, no-loop-func */ |
| 2 | +import * as bitcoin from 'bitcoinjs-lib'; |
| 3 | +import * as ecc from '@bitcoinerlab/secp256k1'; |
| 4 | +import { Network } from '../src/wallet/lib/common/network'; |
| 5 | +import { |
| 6 | + AddressType, |
| 7 | + deriveAddressByType, |
| 8 | + validateBitcoinAddress, |
| 9 | + AddressValidationResult, |
| 10 | + isP2trAddress |
| 11 | +} from '../src/wallet/lib/common/address'; |
| 12 | + |
| 13 | +bitcoin.initEccLib(ecc); |
| 14 | + |
| 15 | +const networkMap: Record<string, bitcoin.Network> = { |
| 16 | + mainnet: bitcoin.networks.bitcoin, |
| 17 | + testnet: bitcoin.networks.testnet |
| 18 | +}; |
| 19 | + |
| 20 | +const addressTypeMap: Record<string, AddressType> = { |
| 21 | + p2pkh: AddressType.Legacy, |
| 22 | + 'p2sh-p2wpkh': AddressType.SegWit, |
| 23 | + p2wpkh: AddressType.NativeSegWit, |
| 24 | + p2tr: AddressType.Taproot |
| 25 | +}; |
| 26 | + |
| 27 | +type Vector = { |
| 28 | + privateKey?: string; |
| 29 | + publicKey: string; |
| 30 | + address: string; |
| 31 | + network: string; |
| 32 | + format: string; |
| 33 | +}; |
| 34 | + |
| 35 | +// Test vectors taken from https://github.com/hirosystems/stacks-blockchain-api/blob/ae0eb65c4d6901db172f2b4ea751817d8ef8c05c/src/tests/helpers-tests.ts#L193-L530 |
| 36 | +const TEST_VECTORS: Vector[] = [ |
| 37 | + { |
| 38 | + publicKey: '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', |
| 39 | + address: '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', |
| 40 | + network: 'mainnet', |
| 41 | + format: 'p2pkh' |
| 42 | + }, |
| 43 | + { |
| 44 | + publicKey: '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', |
| 45 | + address: '3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN', |
| 46 | + network: 'mainnet', |
| 47 | + format: 'p2sh-p2wpkh' |
| 48 | + }, |
| 49 | + { |
| 50 | + publicKey: '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', |
| 51 | + address: 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', |
| 52 | + network: 'mainnet', |
| 53 | + format: 'p2wpkh' |
| 54 | + }, |
| 55 | + { |
| 56 | + publicKey: '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', |
| 57 | + address: 'bc1pmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sspknck9', |
| 58 | + network: 'mainnet', |
| 59 | + format: 'p2tr' |
| 60 | + }, |
| 61 | + { |
| 62 | + publicKey: '03797dd653040d344fd048c1ad05d4cbcb2178b30c6a0c4276994795f3e833da41', |
| 63 | + address: '2NEb2fNbJXdwi7EC6vKCjWUTA12PABNniQM', |
| 64 | + network: 'testnet', |
| 65 | + format: 'p2sh-p2wpkh' |
| 66 | + }, |
| 67 | + { |
| 68 | + publicKey: '03797dd653040d344fd048c1ad05d4cbcb2178b30c6a0c4276994795f3e833da41', |
| 69 | + address: 'tb1qzepy04hjksj6c4m3ggawdjqvw48hzu4swvwmvt', |
| 70 | + network: 'testnet', |
| 71 | + format: 'p2wpkh' |
| 72 | + }, |
| 73 | + { |
| 74 | + publicKey: '03797dd653040d344fd048c1ad05d4cbcb2178b30c6a0c4276994795f3e833da41', |
| 75 | + address: 'tb1p8dlmzllfah294ntwatr8j5uuvcj7yg0dete94ck2krrk0ka2c9qqex96hv', |
| 76 | + network: 'testnet', |
| 77 | + format: 'p2tr' |
| 78 | + } |
| 79 | +]; |
| 80 | + |
| 81 | +describe('Addresses', () => { |
| 82 | + describe('Bitcoin address derivation (from test vectors)', () => { |
| 83 | + for (const vector of TEST_VECTORS) { |
| 84 | + const { publicKey, address, network, format } = vector; |
| 85 | + |
| 86 | + const testName = `${format.toUpperCase()} | ${network} | ${publicKey.slice(0, 10)}...`; |
| 87 | + const addressType = addressTypeMap[format]; |
| 88 | + |
| 89 | + it(`produces correct address: ${testName}`, () => { |
| 90 | + const derived = deriveAddressByType(Buffer.from(publicKey, 'hex'), addressType, networkMap[network]); |
| 91 | + expect(derived).toBe(address); |
| 92 | + }); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + describe('validateBitcoinAddress', () => { |
| 97 | + it('validates a correct legacy address (Mainnet)', () => { |
| 98 | + expect(validateBitcoinAddress('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', Network.Mainnet)).toBe( |
| 99 | + AddressValidationResult.Valid |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('validates a correct native segwit address (Mainnet)', () => { |
| 104 | + expect(validateBitcoinAddress('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq', Network.Mainnet)).toBe( |
| 105 | + AddressValidationResult.Valid |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('validates a correct taproot address (Mainnet)', () => { |
| 110 | + expect( |
| 111 | + validateBitcoinAddress('bc1pemwrzunpf5tj70s6vkxysf2v8njg60kpc2mvuccath5kfw3zd6jqv9lks9', Network.Mainnet) |
| 112 | + ).toBe(AddressValidationResult.Valid); |
| 113 | + }); |
| 114 | + |
| 115 | + it('returns InvalidNetwork for valid testnet address on mainnet', () => { |
| 116 | + expect( |
| 117 | + validateBitcoinAddress('tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c', Network.Mainnet) |
| 118 | + ).toBe(AddressValidationResult.InvalidNetwork); |
| 119 | + }); |
| 120 | + |
| 121 | + it('validates a correct legacy address (Testnet)', () => { |
| 122 | + expect(validateBitcoinAddress('mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn', Network.Testnet)).toBe( |
| 123 | + AddressValidationResult.Valid |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('validates a correct native segwit address (Testnet)', () => { |
| 128 | + expect(validateBitcoinAddress('tb1q597d0yvt3mg3k9p5qtkz8lh3j53nsssr57wnfr', Network.Testnet)).toBe( |
| 129 | + AddressValidationResult.Valid |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('validates a correct taproot address (Testnet)', () => { |
| 134 | + expect( |
| 135 | + validateBitcoinAddress('tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c', Network.Testnet) |
| 136 | + ).toBe(AddressValidationResult.Valid); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns InvalidNetwork for valid mainnet address on testnet', () => { |
| 140 | + expect( |
| 141 | + validateBitcoinAddress('bc1pemwrzunpf5tj70s6vkxysf2v8njg60kpc2mvuccath5kfw3zd6jqv9lks9', Network.Testnet) |
| 142 | + ).toBe(AddressValidationResult.InvalidNetwork); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns InvalidAddress for malformed address', () => { |
| 146 | + expect(validateBitcoinAddress('notARealAddress', Network.Mainnet)).toBe(AddressValidationResult.InvalidAddress); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('isP2trAddress', () => { |
| 151 | + it('identifies P2TR address correctly', () => { |
| 152 | + expect(isP2trAddress('bc1pmyrcn4jl9x8gtz8wjyqchghzq39kech5xg99snm4a3p2l3thf4esfp20h9')).toBe(true); |
| 153 | + expect(isP2trAddress('tb1pmyrcn4jl9x8gtz8wjyqchghzq39kech5xg99snm4a3p2l3thf4esfp20h9')).toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + it('rejects non-taproot address', () => { |
| 157 | + expect(isP2trAddress('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq')).toBe(false); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments