question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compatibility: cannot unmarshal hex number with leading zero digits into Go struct

See original GitHub issue

The 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)

  1. Start a ganache on port 9545 (ganache-cli -p 9545).
  2. Connect using web3 (npx truffle console).
  3. web3.eth.sendTransaction({from: web3.eth.accounts[0], to: web3.eth.accounts[1], value: 0}) (nonce 0 will be encoded correctly as 0x0).
  4. web3.eth.sendTransaction({from: web3.eth.accounts[0], to: web3.eth.accounts[1], value: 0}) (nonce 1 will be encoded incorrectly as 0x01).
  5. Use the attached go program to lookup the tx hex returned from step 4.
  6. 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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kaidirencommented, Oct 23, 2018

+1

ganache version 1.2.2 mac os geth version 1.8.17

json: cannot unmarshal hex number with leading zero digits into Go value of type *hexutil.Big
0reactions
fjlcommented, Jun 11, 2019
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found