From d34f03ee965b6ccbde07f4e4aad67ec91771d87b Mon Sep 17 00:00:00 2001 From: Patryk Kalinowski Date: Thu, 19 Dec 2024 13:10:59 +0100 Subject: [PATCH] ethcoder: allow hex representation of numbers --- ethcoder/abi_helpers.go | 8 +++++++- ethcoder/typed_data_test.go | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/ethcoder/abi_helpers.go b/ethcoder/abi_helpers.go index fa1a7a3..81e737b 100644 --- a/ethcoder/abi_helpers.go +++ b/ethcoder/abi_helpers.go @@ -196,8 +196,14 @@ func ABIUnmarshalStringValuesAny(argTypes []string, stringValues []any) ([]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/typed_data_test.go b/ethcoder/typed_data_test.go index 28eee59..75c2a32 100644 --- a/ethcoder/typed_data_test.go +++ b/ethcoder/typed_data_test.go @@ -416,3 +416,41 @@ func TestTypedDataFromJSONPart4(t *testing.T) { require.NoError(t, err) require.True(t, valid) } + +func TypedDataFromJSONPart5(t *testing.T) { + typedDataJson := `{ + "types": { + "EIP712Domain": [ + { "name": "name", "type": "string" }, + { "name": "version", "type": "string" }, + { "name": "chainId", "type": "uint256" }, + { "name": "verifyingContract", "type": "address" }, + { "name": "salt", "type": "bytes32" } + ], + "ExampleMessage": [ + { "name": "message", "type": "string" }, + { "name": "value", "type": "uint256" }, + { "name": "from", "type": "address" }, + { "name": "to", "type": "address" } + ] + }, + "domain": { + "name": "EIP712Example", + "version": "1", + "chainId": "0x0f", + "verifyingContract": "0xc0ffee254729296a45a3885639AC7E10F9d54979", + "salt": "0x70736575646f2d72616e646f6d2076616c756500000000000000000000000000" + }, + "message": { + "message": "Test message", + "value": "0x634abebe1d4da48b00000000000000000cde63753dad4f0f42f79ebef71ee924, + "from": "0xc0ffee254729296a45a3885639AC7E10F9d54979", + "to": "0xc0ffee254729296a45a3885639AC7E10F9d54979" + } + }` + + typedData, err := ethcoder.TypedDataFromJSON(typedDataJson) + require.NoError(t, err) + + require.Equal(t, typedData.Domain.ChainID.Int64(), int64(15)) +}