18

When I call sendRawTransaction, I got

"[Error: invalid argument 0: json: cannot unmarshal hex string without 0x prefix into Go value of type hexutil.Bytes]"

I think all of params have "0x" prefix. Could you share your ideas how to fix it?

I'm using testnet(ropsten).

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://xxxxxx:xxxx"));

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('xxx', 'hex')

var rawTx = {
  nonce: '0x00',
  gasPrice: '0x5209', // eth_estimateGas rpc result
  gasLimit: '0x5208', // 21,000 in decimal
  to: '0x8005ceb675d2ff8c989cc95354438b9fab568681',
  value: '0x01'
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

console.log(serializedTx.toString('hex')); // f86180825208825208948005ceb675d2ff8c989cc95354438b9fab56868101801ca096e0cb2e633f07a7ee1f761dba1109c18a44550692305c03c72403ffa0b8fc12a012482fd916fa0a05396fadbf13b39619193e9f80dd5a0fd32086257cc3a11796

web3.eth.sendRawTransaction(serializedTx.toString('hex'), function(err, hash) {
  if (!err) {
    console.log(hash);
  } else {
    console.log(err); // [Error: invalid argument 0: json: cannot unmarshal hex string without 0x prefix into Go value of type hexutil.Bytes]
  }
});

Update 1

It worked after adding '0x' following joël saying.

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {

https://ropsten.etherscan.io/tx/0xa94a4a928ac8b84e6f61eabafe59737a7a220ba0fd595aa92217f2a2e0f5d37d

eth
  • 85,679
  • 53
  • 285
  • 406
zono
  • 1,473
  • 5
  • 17
  • 30
  • 3
    Looks like your serializedTx.toString('hex') lacks a 0x though. Try adding one there? – Joël Jul 16 '17 at 12:17
  • 1
    @Joël Perhaps write down the answer so that this question can mark it accepted. Thanks – eth Jul 16 '17 at 21:38

2 Answers2

10

As mentioned in the comments, add a 0x to your serializedTx.toString('hex').

Joël
  • 1,720
  • 2
  • 17
  • 35
4

Latest code with web3 1.0 to deploy a contract(don't send to 0x0000000) address

var Tx = require('ethereumjs-tx');
var Web3 = require('web3');
let KOVAN_RPC_URL = 'http://localhost:8549';
let provider = new Web3.providers.HttpProvider(KOVAN_RPC_URL);
let web3 = new Web3(provider);
var privateKey = new Buffer('1927bed0b6839d1e247925232bef7367c1a4508a112b4f1d91f7331d16cc3cab', 'hex');
web3.eth.getTransactionCount('0x040B90762Cee7a87ee4f51e715a302177043835e').then((txcount) => {
    var rawTx = {
      nonce: web3.utils.toHex(txcount),
      // 1 gwei
      gasPrice: web3.utils.toHex(1000000000),
      gasLimit: web3.utils.toHex(6100500),
      data: '0x00'
    }

    var tx = new Tx(rawTx);
    tx.sign(privateKey);

    var serializedTx = tx.serialize();

    web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
    .on('receipt', console.log);

})
rstormsf
  • 4,337
  • 2
  • 25
  • 42