[Ethereum] web3.eth.sendRawTransaction: cannot unmarshal hex string without 0x prefix

go-ethereumraw-transactionweb3js

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

Best Answer

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