From 3d537bd8b4a561f23bccd73c0b6120ba457fc0cb Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Thu, 19 Dec 2024 09:13:32 -0500 Subject: [PATCH] wip --- ethcoder/abi_helpers.go | 8 +++++++- ethcoder/abi_test.go | 28 ++++++++++++++++++++++++++++ ethcoder/typed_data_test.go | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/ethcoder/abi_helpers.go b/ethcoder/abi_helpers.go index 81e737b..0fdd691 100644 --- a/ethcoder/abi_helpers.go +++ b/ethcoder/abi_helpers.go @@ -414,8 +414,14 @@ func ABIUnmarshalStringValues(argTypes []string, stringValues []string) ([]any, return nil, fmt.Errorf("ethcoder: value at position %d is invalid. invalid number type '%s'", i, typ) } + base := 10 + if strings.HasPrefix(s, "0x") { + base = 16 + s = s[2:] + } + num := big.NewInt(0) - num, ok := num.SetString(s, 10) + num, ok := num.SetString(s, base) if !ok { return nil, fmt.Errorf("ethcoder: value at position %d is invalid. expecting number. unable to set value of '%s'", i, s) } diff --git a/ethcoder/abi_test.go b/ethcoder/abi_test.go index c1ac2a2..edb492a 100644 --- a/ethcoder/abi_test.go +++ b/ethcoder/abi_test.go @@ -226,6 +226,20 @@ func TestABIUnmarshalStringValuesAny(t *testing.T) { assert.Equal(t, int64(2), v2.Int64()) } + { + values, err := ABIUnmarshalStringValuesAny([]string{"address", "uint256"}, []any{"0x6615e4e985bf0d137196897dfa182dbd7127f54f", "0x123456"}) + assert.NoError(t, err) + assert.Len(t, values, 2) + + v1, ok := values[0].(common.Address) + assert.True(t, ok) + assert.Equal(t, "0x6615e4e985BF0D137196897Dfa182dBD7127f54f", v1.String()) + + v2, ok := values[1].(*big.Int) + assert.True(t, ok) + assert.Equal(t, int64(1193046), v2.Int64()) + } + { values, err := ABIUnmarshalStringValuesAny([]string{"address", "bytes8"}, []any{"0x6615e4e985bf0d137196897dfa182dbd7127f54f", "0xaabbccddaabbccdd"}) assert.NoError(t, err) @@ -360,6 +374,20 @@ func TestABIUnmarshalStringValues(t *testing.T) { assert.Equal(t, int64(2), v2.Int64()) } + { + values, err := ABIUnmarshalStringValues([]string{"address", "uint256"}, []string{"0x6615e4e985bf0d137196897dfa182dbd7127f54f", "0x123456"}) + assert.NoError(t, err) + assert.Len(t, values, 2) + + v1, ok := values[0].(common.Address) + assert.True(t, ok) + assert.Equal(t, "0x6615e4e985BF0D137196897Dfa182dBD7127f54f", v1.String()) + + v2, ok := values[1].(*big.Int) + assert.True(t, ok) + assert.Equal(t, int64(1193046), v2.Int64()) + } + { values, err := ABIUnmarshalStringValues([]string{"address", "bytes8"}, []string{"0x6615e4e985bf0d137196897dfa182dbd7127f54f", "0xaabbccddaabbccdd"}) assert.NoError(t, err) diff --git a/ethcoder/typed_data_test.go b/ethcoder/typed_data_test.go index 3e5d94a..59dab73 100644 --- a/ethcoder/typed_data_test.go +++ b/ethcoder/typed_data_test.go @@ -443,7 +443,7 @@ func TestTypedDataFromJSONPart5(t *testing.T) { }, "message": { "message": "Test message", - "value": "0x634abebe1d4da48b00000000000000000cde63753dad4f0f42f79ebef71ee924, + "value": "0x634abebe1d4da48b00000000000000000cde63753dad4f0f42f79ebef71ee924", "from": "0xc0ffee254729296a45a3885639AC7E10F9d54979", "to": "0xc0ffee254729296a45a3885639AC7E10F9d54979" }