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