web3.eth.sendRawTransaction: no se puede desarmar la cadena hexadecimal sin el prefijo 0x

Cuando llamo a sendRawTransaction, obtuve

"[Error: argumento no válido 0: json: no se puede desarmar la cadena hexadecimal sin el prefijo 0x en el valor Go de tipo hexutil.Bytes]"

Creo que todos los parámetros tienen el prefijo "0x". ¿Podría compartir sus ideas sobre cómo solucionarlo?

Estoy usando 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]
  }
});

Actualización 1

Funcionó después de agregar '0x' siguiendo el dicho de joël.

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

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

Parece que serializedTx.toString('hex')le falta un 0xpensamiento. ¿Intentar agregar uno allí?
@Joël Tal vez escriba la respuesta para que esta pregunta pueda marcarla como aceptada. Gracias

Respuestas (2)

Como se menciona en los comentarios, agregue un 0xa su archivo serializedTx.toString('hex').

Código más reciente con web3 1.0 para implementar una dirección de contrato (no enviar a 0x0000000)

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

})