|
1 | 1 | import { |
| 2 | + Hex, |
2 | 3 | add0x, |
3 | 4 | assertIsHexString, |
4 | 5 | assertIsStrictHexString, |
| 6 | + isValidChecksumAddress, |
5 | 7 | isHexString, |
6 | 8 | isStrictHexString, |
| 9 | + isValidHexAddress, |
7 | 10 | remove0x, |
| 11 | + getChecksumAddress, |
8 | 12 | } from './hex'; |
9 | 13 |
|
10 | 14 | describe('isHexString', () => { |
@@ -151,6 +155,92 @@ describe('assertIsStrictHexString', () => { |
151 | 155 | }); |
152 | 156 | }); |
153 | 157 |
|
| 158 | +describe('isValidHexAddress', () => { |
| 159 | + it.each([ |
| 160 | + '0x0000000000000000000000000000000000000000' as Hex, |
| 161 | + '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' as Hex, |
| 162 | + ])('returns true for a valid prefixed hex address', (hexString) => { |
| 163 | + expect(isValidHexAddress(hexString)).toBe(true); |
| 164 | + }); |
| 165 | + |
| 166 | + it.each([ |
| 167 | + '0000000000000000000000000000000000000000', |
| 168 | + 'd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', |
| 169 | + ])('returns false for a valid non-prefixed hex address', (hexString) => { |
| 170 | + // @ts-expect-error - testing invalid input |
| 171 | + expect(isValidHexAddress(hexString)).toBe(false); |
| 172 | + }); |
| 173 | + |
| 174 | + it.each([ |
| 175 | + '12345g', |
| 176 | + '1234567890abcdefg', |
| 177 | + '1234567890abcdefG', |
| 178 | + '1234567890abcdefABCDEFg', |
| 179 | + '1234567890abcdefABCDEF1234567890abcdefABCDEFg', |
| 180 | + '0x', |
| 181 | + '0x0', |
| 182 | + '0x12345g', |
| 183 | + '0x1234567890abcdefg', |
| 184 | + '0x1234567890abcdefG', |
| 185 | + '0x1234567890abcdefABCDEFg', |
| 186 | + '0x1234567890abcdefABCDEF1234567890abcdefABCDEFg', |
| 187 | + '0xD8DA6BF26964AF9D7EED9E03E53415D37AA96045', |
| 188 | + '0xCF5609B003B2776699EEA1233F7C82D5695CC9AA', |
| 189 | + '0Xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', |
| 190 | + ])('returns false for an invalid hex address', (hexString) => { |
| 191 | + // @ts-expect-error - testing invalid input |
| 192 | + expect(isValidHexAddress(hexString)).toBe(false); |
| 193 | + }); |
| 194 | +}); |
| 195 | + |
| 196 | +describe('getChecksumAddress', () => { |
| 197 | + it('returns the checksum address for a valid hex address', () => { |
| 198 | + expect( |
| 199 | + getChecksumAddress('0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed'), |
| 200 | + ).toBe('0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed'); |
| 201 | + |
| 202 | + expect( |
| 203 | + getChecksumAddress('0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359'), |
| 204 | + ).toBe('0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'); |
| 205 | + |
| 206 | + expect( |
| 207 | + getChecksumAddress('0x52908400098527886e0f7030069857d2e4169ee7'), |
| 208 | + ).toBe('0x52908400098527886E0F7030069857D2E4169EE7'); |
| 209 | + |
| 210 | + expect( |
| 211 | + getChecksumAddress('0xde709f2102306220921060314715629080e2fb77'), |
| 212 | + ).toBe('0xde709f2102306220921060314715629080e2fb77'); |
| 213 | + |
| 214 | + expect( |
| 215 | + getChecksumAddress('0x0000000000000000000000000000000000000000'), |
| 216 | + ).toBe('0x0000000000000000000000000000000000000000'); |
| 217 | + }); |
| 218 | + |
| 219 | + it('throws for an invalid hex address', () => { |
| 220 | + expect(() => getChecksumAddress('0x')).toThrow('Invalid hex address.'); |
| 221 | + }); |
| 222 | +}); |
| 223 | + |
| 224 | +describe('isValidChecksumAddress', () => { |
| 225 | + it.each([ |
| 226 | + '0x0000000000000000000000000000000000000000' as Hex, |
| 227 | + '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' as Hex, |
| 228 | + '0xCf5609B003B2776699eEA1233F7C82D5695cC9AA' as Hex, |
| 229 | + '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' as Hex, |
| 230 | + '0x8617E340B3D01FA5F11F306F4090FD50E238070D' as Hex, |
| 231 | + ])('returns true for a valid checksum address', (hexString) => { |
| 232 | + expect(isValidChecksumAddress(hexString)).toBe(true); |
| 233 | + }); |
| 234 | + |
| 235 | + it.each([ |
| 236 | + '0xz' as Hex, |
| 237 | + '0xD8DA6BF26964AF9D7EED9E03E53415D37AA96045' as Hex, |
| 238 | + '0xCF5609B003B2776699EEA1233F7C82D5695CC9AA' as Hex, |
| 239 | + ])('returns false for an invalid checksum address', (hexString) => { |
| 240 | + expect(isValidChecksumAddress(hexString)).toBe(false); |
| 241 | + }); |
| 242 | +}); |
| 243 | + |
154 | 244 | describe('add0x', () => { |
155 | 245 | it('adds a 0x-prefix to a string', () => { |
156 | 246 | expect(add0x('12345')).toBe('0x12345'); |
|
0 commit comments