Skip to content

Commit 0d8f8a3

Browse files
authored
ethcoder: allow hex representation of numbers (#153)
1 parent d841cfe commit 0d8f8a3

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

ethcoder/abi_helpers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,14 @@ func ABIUnmarshalStringValuesAny(argTypes []string, stringValues []any) ([]any,
196196
return nil, fmt.Errorf("ethcoder: value at position %d is invalid. invalid number type '%s'", i, typ)
197197
}
198198

199+
base := 10
200+
if strings.HasPrefix(s, "0x") {
201+
base = 16
202+
s = s[2:]
203+
}
204+
199205
num := big.NewInt(0)
200-
num, ok = num.SetString(s, 10)
206+
num, ok = num.SetString(s, base)
201207
if !ok {
202208
return nil, fmt.Errorf("ethcoder: value at position %d is invalid. expecting number. unable to set value of '%s'", i, s)
203209
}

ethcoder/typed_data_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,41 @@ func TestTypedDataFromJSONPart4(t *testing.T) {
416416
require.NoError(t, err)
417417
require.True(t, valid)
418418
}
419+
420+
func TypedDataFromJSONPart5(t *testing.T) {
421+
typedDataJson := `{
422+
"types": {
423+
"EIP712Domain": [
424+
{ "name": "name", "type": "string" },
425+
{ "name": "version", "type": "string" },
426+
{ "name": "chainId", "type": "uint256" },
427+
{ "name": "verifyingContract", "type": "address" },
428+
{ "name": "salt", "type": "bytes32" }
429+
],
430+
"ExampleMessage": [
431+
{ "name": "message", "type": "string" },
432+
{ "name": "value", "type": "uint256" },
433+
{ "name": "from", "type": "address" },
434+
{ "name": "to", "type": "address" }
435+
]
436+
},
437+
"domain": {
438+
"name": "EIP712Example",
439+
"version": "1",
440+
"chainId": "0x0f",
441+
"verifyingContract": "0xc0ffee254729296a45a3885639AC7E10F9d54979",
442+
"salt": "0x70736575646f2d72616e646f6d2076616c756500000000000000000000000000"
443+
},
444+
"message": {
445+
"message": "Test message",
446+
"value": "0x634abebe1d4da48b00000000000000000cde63753dad4f0f42f79ebef71ee924,
447+
"from": "0xc0ffee254729296a45a3885639AC7E10F9d54979",
448+
"to": "0xc0ffee254729296a45a3885639AC7E10F9d54979"
449+
}
450+
}`
451+
452+
typedData, err := ethcoder.TypedDataFromJSON(typedDataJson)
453+
require.NoError(t, err)
454+
455+
require.Equal(t, typedData.Domain.ChainID.Int64(), int64(15))
456+
}

0 commit comments

Comments
 (0)