Skip to content

Commit

Permalink
ethcoder: allow hex representation of numbers (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav authored Dec 19, 2024
1 parent d841cfe commit 0d8f8a3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ethcoder/abi_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
38 changes: 38 additions & 0 deletions ethcoder/typed_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 0d8f8a3

Please sign in to comment.