| 1 | import 'package:cake_wallet/core/address_validator.dart'; |
| 2 | import 'package:cake_wallet/generated/i18n.dart'; |
| 3 | import 'package:cw_core/crypto_currency.dart'; |
| 4 | import 'package:flutter_test/flutter_test.dart'; |
| 5 | |
| 6 | void main() { |
| 7 | group('AddressValidator', () { |
| 8 | setUpAll(() { |
| 9 | S.current = S(); |
| 10 | }); |
| 11 | group('getPattern', () { |
| 12 | test('returns correct pattern for Bitcoin', () { |
| 13 | final pattern = AddressValidator.getPattern(CryptoCurrency.btc); |
| 14 | expect(pattern, isNotEmpty); |
| 15 | expect(pattern, contains('(bc|tb)1q')); |
| 16 | }); |
| 17 | |
| 18 | test('returns correct pattern for Ethereum', () { |
| 19 | final pattern = AddressValidator.getPattern(CryptoCurrency.eth); |
| 20 | expect(pattern, isNotEmpty); |
| 21 | expect(pattern, contains('0x[0-9a-zA-Z]+')); |
| 22 | }); |
| 23 | |
| 24 | test('returns correct pattern for Monero', () { |
| 25 | final pattern = AddressValidator.getPattern(CryptoCurrency.xmr); |
| 26 | expect(pattern, isNotEmpty); |
| 27 | expect(pattern, contains('4[0-9a-zA-Z]{94}|8[0-9a-zA-Z]{94}|[0-9a-zA-Z]{106}')); |
| 28 | }); |
| 29 | |
| 30 | test('returns correct pattern for Litecoin', () { |
| 31 | final pattern = AddressValidator.getPattern(CryptoCurrency.ltc); |
| 32 | expect(pattern, isNotEmpty); |
| 33 | expect(pattern, |
| 34 | contains('(bc|tb|ltc)1q[ac-hj-np-z02-9]{25,39}|(ltc|t)mweb1q[ac-hj-np-z02-9]{90,120}')); |
| 35 | }); |
| 36 | |
| 37 | test('returns empty string for unknown currency', () { |
| 38 | final pattern = AddressValidator.getPattern(CryptoCurrency.btcln); |
| 39 | expect(pattern, isNotEmpty); |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | group('getLength', () { |
| 44 | test('returns correct length for Bitcoin', () { |
| 45 | final length = AddressValidator.getLength(CryptoCurrency.btc); |
| 46 | expect(length, isNull); |
| 47 | }); |
| 48 | |
| 49 | test('returns correct length for Ethereum', () { |
| 50 | final length = AddressValidator.getLength(CryptoCurrency.eth); |
| 51 | expect(length, equals([42])); |
| 52 | }); |
| 53 | |
| 54 | test('returns correct length for Monero', () { |
| 55 | final length = AddressValidator.getLength(CryptoCurrency.xmr); |
| 56 | expect(length, isNull); |
| 57 | }); |
| 58 | |
| 59 | test('returns correct length for Dash', () { |
| 60 | final length = AddressValidator.getLength(CryptoCurrency.dash); |
| 61 | expect(length, equals([34])); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | group('getAddressFromStringPattern', () { |
| 66 | test('returns correct pattern for Bitcoin', () { |
| 67 | final pattern = AddressValidator.getAddressFromStringPattern(CryptoCurrency.btc); |
| 68 | expect(pattern, isNotNull); |
| 69 | expect(pattern, contains('(bc|tb)1q')); |
| 70 | }); |
| 71 | |
| 72 | test('returns correct pattern for Ethereum', () { |
| 73 | final pattern = AddressValidator.getAddressFromStringPattern(CryptoCurrency.eth); |
| 74 | expect(pattern, isNotNull); |
| 75 | expect(pattern, contains('0x[0-9a-zA-Z]+')); |
| 76 | }); |
| 77 | |
| 78 | test('returns correct pattern for Monero', () { |
| 79 | final pattern = AddressValidator.getAddressFromStringPattern(CryptoCurrency.xmr); |
| 80 | expect(pattern, isNotNull); |
| 81 | expect(pattern, contains('(4[0-9a-zA-Z]{94})')); |
| 82 | }); |
| 83 | |
| 84 | test('returns null for unsupported currency', () { |
| 85 | final pattern = AddressValidator.getAddressFromStringPattern(CryptoCurrency.dash); |
| 86 | expect(pattern, isNull); |
| 87 | }); |
| 88 | }); |
| 89 | // 0.000058158099999999995 BTC |
| 90 | group('validation', () { |
| 91 | test('validates valid Bitcoin address', () { |
| 92 | final validator = AddressValidator(type: CryptoCurrency.btc); |
| 93 | expect(validator.isValid('bc1qhg4l43pmq5v5atmtlr7gnwyuxs043cvrut5hkq'), isTrue); |
| 94 | expect(validator.isValid('3AD1Btx1MzYGmdpNpeujCfuvU5SsU2LX88'), isTrue); |
| 95 | expect(validator.isValid('1HARAhFcvz8ZQp5MhnLFeUynC4bkha3Hv8'), isTrue); |
| 96 | }); |
| 97 | |
| 98 | test('rejects invalid Bitcoin address', () { |
| 99 | final validator = AddressValidator(type: CryptoCurrency.btc); |
| 100 | expect(validator.isValid('invalid_address'), isFalse); |
| 101 | expect(validator.isValid('bc1qhg4l43pmq5v5atmtlr7gnwyuxs043CakeWallet'), isFalse); |
| 102 | }); |
| 103 | |
| 104 | test('validates valid Ethereum address', () { |
| 105 | final validator = AddressValidator(type: CryptoCurrency.eth); |
| 106 | expect(validator.isValid('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'), |
| 107 | isTrue); // WETH contract |
| 108 | }); |
| 109 | |
| 110 | test('rejects invalid Ethereum address', () { |
| 111 | final validator = AddressValidator(type: CryptoCurrency.eth); |
| 112 | expect(validator.isValid('invalid_address'), isFalse); |
| 113 | expect( |
| 114 | validator.isValid('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc'), isFalse); // Too short |
| 115 | expect(validator.isValid('C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'), |
| 116 | isFalse); // Missing 0x prefix |
| 117 | }); |
| 118 | |
| 119 | test('validates valid Monero address', () { |
| 120 | final validator = AddressValidator(type: CryptoCurrency.xmr); |
| 121 | expect( |
| 122 | validator.isValid( |
| 123 | '85s6zfxGAkdCN21h566R8EFDSfThxCrFiEkhw3JEtaXN2DDfahABLXTjRj385Ro7om5saGWJG7iuE6EyW5MYcoz93DLvNqh'), |
| 124 | isTrue); |
| 125 | }); |
| 126 | |
| 127 | test('rejects invalid Monero address', () { |
| 128 | final validator = AddressValidator(type: CryptoCurrency.xmr); |
| 129 | expect(validator.isValid('invalid_address'), isFalse); |
| 130 | expect( |
| 131 | validator.isValid( |
| 132 | '85s6zfxGAkdCN21h566R8EFDSfThxCrFiEkhw3JEtaXN2DDfahABLXTjRj385Ro7om5saGWJG7iuE6EyW5MYcoz93DLvNq'), |
| 133 | isFalse); // Too short |
| 134 | }); |
| 135 | |
| 136 | test('validates valid Litecoin address', () { |
| 137 | final validator = AddressValidator(type: CryptoCurrency.ltc); |
| 138 | expect(validator.isValid('ltc1qzvxlvlk8wsmue0np20eh3d3qxsusx9jstf8qw8'), isTrue); |
| 139 | expect( |
| 140 | validator.isValid( |
| 141 | 'ltcmweb1qqt9hqch2d0vfdsvt4tf27gullem2tcd57xxrvta9xwvfmwdkn4927q6d8sq6ftw7lkqdkr5g36eqn7w06edgq8tz7gy0nv5d4lhajctkzuath23a'), |
| 142 | isTrue); |
| 143 | }); |
| 144 | |
| 145 | test('rejects invalid Litecoin address', () { |
| 146 | final validator = AddressValidator(type: CryptoCurrency.ltc); |
| 147 | expect(validator.isValid('invalid_address'), isFalse); |
| 148 | expect(validator.isValid('ltc1qzvxlvlk8wsmue0np20eh3d3qxsusxCakeWallet'), isFalse); |
| 149 | }); |
| 150 | }); |
| 151 | |
| 152 | group('silentPaymentAddressPatternMainnet', () { |
| 153 | test('returns a non-empty pattern', () { |
| 154 | final pattern = AddressValidator.silentPaymentAddressPatternMainnet; |
| 155 | expect(pattern, isNotEmpty); |
| 156 | }); |
| 157 | }); |
| 158 | |
| 159 | group('silentPaymentAddressPatternTestnet', () { |
| 160 | test('returns a non-empty pattern', () { |
| 161 | final pattern = AddressValidator.silentPaymentAddressPatternTestnet; |
| 162 | expect(pattern, isNotEmpty); |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | group('mWebAddressPattern', () { |
| 167 | test('returns a non-empty pattern', () { |
| 168 | final pattern = AddressValidator.mWebAddressPattern; |
| 169 | expect(pattern, isNotEmpty); |
| 170 | }); |
| 171 | }); |
| 172 | }); |
| 173 | } |