Compatibility: cannot unmarshal hex number with leading zero digits into Go struct
See original GitHub issueThe ethclient fails parsing a transaction received from ganache over RPC.
Expected Behavior
It should parse the JSON response.
Current Behavior
json: cannot unmarshal hex number with leading zero digits into Go struct field txdata.nonce of type hexutil.Uint64
Possible Solution
ethclient expects the following transaction fields to hex encodings of a number without any leading zeros:
nonce
gasPrice
gasLimit
value
input
v
r
s
Therefore a nonce
of 0x02 will fail to parse but 0x2 will succeed.
Steps to Reproduce (for bugs)
- Start a ganache on port 9545 (
ganache-cli -p 9545
). - Connect using web3 (
npx truffle console
). web3.eth.sendTransaction({from: web3.eth.accounts[0], to: web3.eth.accounts[1], value: 0})
(nonce 0 will be encoded correctly as0x0
).web3.eth.sendTransaction({from: web3.eth.accounts[0], to: web3.eth.accounts[1], value: 0})
(nonce 1 will be encoded incorrectly as0x01
).- Use the attached go program to lookup the tx hex returned from step 4.
- Program crashes.
Context
According to the JSON-RPC specification hex quantity values should be encoded as follows:
When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0"). Examples:
0x41 (65 in decimal)
0x400 (1024 in decimal)
WRONG: 0x (should always have at least one digit - zero is "0x0")
WRONG: 0x0400 (no leading zeroes allowed)
WRONG: ff (must be prefixed 0x)
ganache should adhere to the spec.
package main
import (
"context"
"log"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: ./transaction <tx-hex>")
}
h := os.Args[1]
conn, err := ethclient.Dial("http://localhost:9545")
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
t, _, err := conn.TransactionByHash(context.Background(), common.HexToHash(h))
if err != nil {
log.Fatal(err)
}
log.Printf("%#v\n", t)
}
Your Environment
- Version used: All versions, including
develop
branch. - Operating System and version: Linux e 4.15.12-x86_64-linode105 SMP Thu Mar 22 02:13:40 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
How to fix "invalid argument 0: json: cannot unmarshal hex ...
How to fix "invalid argument 0: json: cannot unmarshal hex number with leading zero digits into Go struct field CallArgs.value of type *hexutil....
Read more >Error: invalid argument 0: json: cannot unmarshal hex string ...
Error: invalid argument 0: json: cannot unmarshal hex string without 0x prefix into Go struct field TransactionArgs.data of type hexutil.Bytes.
Read more >decode.go - Google Git
To unmarshal JSON into a pointer, Unmarshal first handles the case of ... the additional Go array elements are set to zero values....
Read more >package json - - The Go Programming Language
70 // 71 // To unmarshal a JSON object into a map, Unmarshal first ... 125 type UnmarshalTypeError struct { 126 Value string...
Read more >Saturn Incoming: Wanchain Node Upgrade Guide - Medium
... an error like “cannot unmarshal hex number with leading zero digits into Go struct field TransactionArgs.chainId of type *hexutil.Big”.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
+1
ganache version 1.2.2 mac os geth version 1.8.17
Xref: https://github.com/trufflesuite/ganache-core/pull/432