web3.eth.sendRawTransaction: Hex-String ohne 0x-Präfix kann nicht entpackt werden

Wenn ich sendRawTransaction anrufe, bekam ich

"[Fehler: ungültiges Argument 0: json: Hex-String ohne 0x-Präfix kann nicht in Go-Wert vom Typ hexutil.Bytes entpackt werden]"

Ich denke, alle Parameter haben das Präfix "0x". Könnten Sie Ihre Ideen teilen, wie man es beheben kann?

Ich verwende 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]
  }
});

Aktualisierung 1

Es funktionierte nach dem Hinzufügen von '0x' nach dem Spruch von Joël.

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

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

Sieht so aus, serializedTx.toString('hex')als ob dir eins fehlt 0x. Versuchen Sie, dort einen hinzuzufügen?
@Joël Schreiben Sie vielleicht die Antwort auf, damit diese Frage sie als akzeptiert markieren kann. Vielen Dank

Antworten (2)

Wie in den Kommentaren erwähnt, fügen 0xSie Ihrer serializedTx.toString('hex').

Neuester Code mit web3 1.0 zum Bereitstellen einer Vertragsadresse (nicht an 0x0000000 senden).

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

})