|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { decodeErc7930Address } from './decodeErc7930Address' |
| 4 | + |
| 5 | +describe('decodeErc7930Address', () => { |
| 6 | + it('should decode a valid ERC-7930 address for Ethereum mainnet', () => { |
| 7 | + // Version: 0001, ChainType: 0000 (EVM), ChainRefLength: 01, ChainRef: 01 (mainnet) |
| 8 | + // AddressLength: 14 (20 bytes), Address: 8004a169fb4a3325136eb29fa0ceb6d2e539a432 |
| 9 | + const hex = '0x00010000010114' + '8004a169fb4a3325136eb29fa0ceb6d2e539a432' |
| 10 | + const result = decodeErc7930Address(hex) |
| 11 | + |
| 12 | + expect(result).toEqual({ |
| 13 | + chainId: 1, |
| 14 | + address: '0x8004a169fb4a3325136eb29fa0ceb6d2e539a432', |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should decode a valid ERC-7930 address for Base (chain ID 8453)', () => { |
| 19 | + // Version: 0001, ChainType: 0000 (EVM), ChainRefLength: 02, ChainRef: 2105 (8453 in hex) |
| 20 | + // AddressLength: 14 (20 bytes), Address: 1234567890abcdef1234567890abcdef12345678 |
| 21 | + const hex = '0x0001000002210514' + '1234567890abcdef1234567890abcdef12345678' |
| 22 | + const result = decodeErc7930Address(hex) |
| 23 | + |
| 24 | + expect(result).toEqual({ |
| 25 | + chainId: 8453, |
| 26 | + address: '0x1234567890abcdef1234567890abcdef12345678', |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should handle hex without 0x prefix', () => { |
| 31 | + const hex = '00010000010114' + '8004a169fb4a3325136eb29fa0ceb6d2e539a432' |
| 32 | + const result = decodeErc7930Address(hex) |
| 33 | + |
| 34 | + expect(result).toEqual({ |
| 35 | + chainId: 1, |
| 36 | + address: '0x8004a169fb4a3325136eb29fa0ceb6d2e539a432', |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should return null for invalid version', () => { |
| 41 | + // Version: 0002 (invalid) |
| 42 | + const hex = '0x00020000010114' + '8004a169fb4a3325136eb29fa0ceb6d2e539a432' |
| 43 | + expect(decodeErc7930Address(hex)).toBeNull() |
| 44 | + }) |
| 45 | + |
| 46 | + it('should return null for non-EVM chain type', () => { |
| 47 | + // ChainType: 0001 (non-EVM) |
| 48 | + const hex = '0x00010001010114' + '8004a169fb4a3325136eb29fa0ceb6d2e539a432' |
| 49 | + expect(decodeErc7930Address(hex)).toBeNull() |
| 50 | + }) |
| 51 | + |
| 52 | + it('should return null for invalid address length', () => { |
| 53 | + // AddressLength: 15 (21 bytes, should be 20) |
| 54 | + const hex = '0x00010000010115' + '8004a169fb4a3325136eb29fa0ceb6d2e539a432' |
| 55 | + expect(decodeErc7930Address(hex)).toBeNull() |
| 56 | + }) |
| 57 | + |
| 58 | + it('should return null for too short hex', () => { |
| 59 | + expect(decodeErc7930Address('0x0001')).toBeNull() |
| 60 | + expect(decodeErc7930Address('')).toBeNull() |
| 61 | + }) |
| 62 | + |
| 63 | + it('should return null for zero chain reference length', () => { |
| 64 | + const hex = '0x00010000000014' + '8004a169fb4a3325136eb29fa0ceb6d2e539a432' |
| 65 | + expect(decodeErc7930Address(hex)).toBeNull() |
| 66 | + }) |
| 67 | + |
| 68 | + it('should return null if address is truncated', () => { |
| 69 | + // Only 10 bytes of address instead of 20 |
| 70 | + const hex = '0x00010000010114' + '8004a169fb4a33251' |
| 71 | + expect(decodeErc7930Address(hex)).toBeNull() |
| 72 | + }) |
| 73 | +}) |
0 commit comments