|
| 1 | +package utils_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/ethereum/go-ethereum/common" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/cosmos/evm/utils" |
| 10 | + |
| 11 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 12 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 13 | +) |
| 14 | + |
| 15 | +func TestStringToBytes(t *testing.T) { |
| 16 | + config := sdk.GetConfig() |
| 17 | + config.SetBech32PrefixForAccount("cosmos", "cosmospub") |
| 18 | + addrBz := common.HexToAddress(hex).Bytes() |
| 19 | + |
| 20 | + testCases := []struct { |
| 21 | + name string |
| 22 | + cdcPrefix string |
| 23 | + input string |
| 24 | + expBz []byte |
| 25 | + expErr error |
| 26 | + }{ |
| 27 | + { |
| 28 | + "success: valid bech32 address", |
| 29 | + "cosmos", |
| 30 | + bech32, |
| 31 | + addrBz, |
| 32 | + nil, |
| 33 | + }, |
| 34 | + { |
| 35 | + "success: valid hex address", |
| 36 | + "cosmos", |
| 37 | + hex, |
| 38 | + addrBz, |
| 39 | + nil, |
| 40 | + }, |
| 41 | + { |
| 42 | + "failure: invalid bech32 address (wrong prefix)", |
| 43 | + "evmos", |
| 44 | + bech32, |
| 45 | + nil, |
| 46 | + sdkerrors.ErrLogic.Wrapf("hrp does not match bech32 prefix: expected '%s' got '%s'", "evmos", "cosmos"), |
| 47 | + }, |
| 48 | + { |
| 49 | + "failure: invalid bech32 address (too long)", |
| 50 | + "cosmos", |
| 51 | + "cosmos10jmp6sgh4cc6zt3e8gw05wavvejgr5pwsjskvvv", // extra char at the end |
| 52 | + nil, |
| 53 | + sdkerrors.ErrUnknownAddress, |
| 54 | + }, |
| 55 | + { |
| 56 | + "failure: invalid bech32 address (invalid format)", |
| 57 | + "cosmos", |
| 58 | + "cosmos10jmp6sgh4cc6zt3e8gw05wavvejgr5pwsjskv", // missing last char |
| 59 | + nil, |
| 60 | + sdkerrors.ErrUnknownAddress, |
| 61 | + }, |
| 62 | + { |
| 63 | + "failure: invalid hex address (odd length)", |
| 64 | + "cosmos", |
| 65 | + "0x7cB61D4117AE31a12E393a1Cfa3BaC666481D02", // missing last char |
| 66 | + nil, |
| 67 | + sdkerrors.ErrUnknownAddress, |
| 68 | + }, |
| 69 | + { |
| 70 | + "failure: invalid hex address (even length)", |
| 71 | + "cosmos", |
| 72 | + "0x7cB61D4117AE31a12E393a1Cfa3BaC666481D0", // missing last 2 char |
| 73 | + nil, |
| 74 | + sdkerrors.ErrUnknownAddress, |
| 75 | + }, |
| 76 | + { |
| 77 | + "failure: invalid hex address (too long)", |
| 78 | + "cosmos", |
| 79 | + "0x7cB61D4117AE31a12E393a1Cfa3BaC666481D02E00", // extra 2 char at the end |
| 80 | + nil, |
| 81 | + sdkerrors.ErrUnknownAddress, |
| 82 | + }, |
| 83 | + { |
| 84 | + "failure: empty string", |
| 85 | + "cosmos", |
| 86 | + "", |
| 87 | + nil, |
| 88 | + sdkerrors.ErrInvalidAddress.Wrap("empty address string is not allowed"), |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tc := range testCases { |
| 93 | + t.Run(tc.name, func(t *testing.T) { |
| 94 | + cdc := utils.NewEvmCodec(tc.cdcPrefix) |
| 95 | + bz, err := cdc.StringToBytes(tc.input) |
| 96 | + if tc.expErr == nil { |
| 97 | + require.NoError(t, err) |
| 98 | + require.Equal(t, tc.expBz, bz) |
| 99 | + } else { |
| 100 | + require.ErrorContains(t, err, tc.expErr.Error()) |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestBytesToString(t *testing.T) { |
| 107 | + // Keep the same fixtures as your StringToBytes test |
| 108 | + config := sdk.GetConfig() |
| 109 | + config.SetBech32PrefixForAccount("cosmos", "cosmospub") |
| 110 | + |
| 111 | + addrBz := common.HexToAddress(hex).Bytes() // 20 bytes |
| 112 | + zeroAddr := common.Address{}.Hex() // "0x000..." |
| 113 | + |
| 114 | + // Helper codec (used only where we want to derive bytes from the bech32 string) |
| 115 | + cdc := utils.NewEvmCodec("cosmos") |
| 116 | + |
| 117 | + type tc struct { |
| 118 | + name string |
| 119 | + input func() []byte |
| 120 | + expHex string |
| 121 | + } |
| 122 | + |
| 123 | + testCases := []tc{ |
| 124 | + { |
| 125 | + name: "success: from 20-byte input (hex-derived)", |
| 126 | + input: func() []byte { |
| 127 | + return addrBz |
| 128 | + }, |
| 129 | + expHex: common.HexToAddress(hex).Hex(), // checksummed |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "success: from bech32-derived bytes", |
| 133 | + input: func() []byte { |
| 134 | + bz, err := cdc.StringToBytes(bech32) |
| 135 | + require.NoError(t, err) |
| 136 | + require.Len(t, bz, 20) |
| 137 | + return bz |
| 138 | + }, |
| 139 | + expHex: common.HexToAddress(hex).Hex(), // same address as above |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "success: empty slice -> zero address", |
| 143 | + input: func() []byte { |
| 144 | + return []byte{} |
| 145 | + }, |
| 146 | + expHex: zeroAddr, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "success: shorter than 20 bytes -> left-padded to zeroes", |
| 150 | + input: func() []byte { |
| 151 | + // Drop first byte -> 19 bytes; common.BytesToAddress pads on the left |
| 152 | + return addrBz[1:] |
| 153 | + }, |
| 154 | + expHex: common.BytesToAddress(addrBz[1:]).Hex(), |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "success: longer than 20 bytes -> rightmost 20 used", |
| 158 | + input: func() []byte { |
| 159 | + // Prepend one byte; common.BytesToAddress will use last 20 bytes (which are addrBz) |
| 160 | + return append([]byte{0xAA}, addrBz...) |
| 161 | + }, |
| 162 | + expHex: common.BytesToAddress(append([]byte{0xAA}, addrBz...)).Hex(), |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "success: much longer (32 bytes) -> rightmost 20 used", |
| 166 | + input: func() []byte { |
| 167 | + prefix := make([]byte, 12) // 12 + 20 = 32 |
| 168 | + return append(prefix, addrBz...) |
| 169 | + }, |
| 170 | + expHex: common.BytesToAddress(append(make([]byte, 12), addrBz...)).Hex(), |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + for _, tc := range testCases { |
| 175 | + t.Run(tc.name, func(t *testing.T) { |
| 176 | + codec := utils.NewEvmCodec("cosmos") |
| 177 | + |
| 178 | + got, err := codec.BytesToString(tc.input()) |
| 179 | + require.NoError(t, err) |
| 180 | + require.Equal(t, tc.expHex, got) |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments