5

Running parity private PoA chain, using latest parity client + nodejs + web3.js 1.0

When I try to sign a transaction and then send it following web3.js 1.0 documentation (sendTransaction and sendSignedTransaction) I get the following error:

Unhandled rejection Error: Returned error: Invalid RLP. at Object.ErrorResponse (/home/simoh/node_modules/web3-core-helpers/src/errors.js:29:16) at /home/simoh/node_modules/web3-core-requestmanager/src/index.js:137:36 at XMLHttpRequest.request.onreadystatechange (/home/simoh/node_modules/web3-providers-http/src/index.js:64:13) at XMLHttpRequestEventTarget.dispatchEvent (/home/simoh/node_modules/xhr2/lib/xhr2.js:64:18) at XMLHttpRequest._setReadyState (/home/simoh/node_modules/xhr2/lib/xhr2.js:354:12) at XMLHttpRequest._onHttpResponseEnd (/home/simoh/node_modules/xhr2/lib/xhr2.js:509:12) at IncomingMessage. (/home/simoh/node_modules/xhr2/lib/xhr2.js:469:24) at emitNone (events.js:110:20) at IncomingMessage.emit (events.js:207:7) at endReadableNT (_stream_readable.js:1059:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)

This is my code:

web3.eth.accounts.signTransaction({
  nonce: 11377,
  gasPrice: '0x00',
  gasLimit: '0x2DC6C0',
  to: '0x00d4dc44dfbbcb7d8369ddcd261bdaad1872d652',
  from: account.address,
  value: '0x16345785D8A0000',
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}, account.privateKey)
.then(function(result) {
  console.log("Results: ", result)

  web3.eth.sendSignedTransaction(result.rawTransaction)
    .on('receipt', console.log);
})

The account is defined in the following way:

var account = web3.eth.accounts.privateKeyToAccount(privateKey);

I cannot figure out what I am doing wrong.

In addition it is not very clear to me why as example for sendSignedTransaction, it is shown a method that uses 'ethereumjs-tx' which is different than the one proposed in the documentation. Using this second method with 'ethereumjs-tx' the same transaction as the code above is succesfully signed and sent.

var Tx = require('ethereumjs-tx');

var privateKey = new Buffer('private key', 'hex');

    var rawTx = {
          nonce: 11376,
          gasPrice: '0x00',
          gasLimit: '0x2DC6C0',
          to: '0x00d4dc44dfbbcb7d8369ddcd261bdaad1872d652',
          from: '0x0013a861865d784d97c57e70814b13ba94713d4e',
          value: '0x16345785D8A0000',
          data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
        }

    var tx = new Tx(rawTx);
    tx.sign(privateKey);
    var myString = "0x"
    var serializedTx = (tx.serialize()).toString('hex');
    myString += serializedTx
    console.log(myString);

    web3.eth.sendSignedTransaction(myString)
    .on('receipt', console.log);

Also when I use web3.eth.accounts.recoverTransaction(result.rawTransaction‌​) it is showing different public key than what's expected...

1
  • 1
    I'm interested in the answer for this as well as how to get the data for the signTransaction call. In the earlier web3.js 0.x versions, there was getData methods on web3.eth.contract.new and web3.eth.contract.myMethod. getData has been dropped in web3 version 1.0
    – naddison
    Commented Oct 13, 2017 at 15:21

1 Answer 1

3
let send_add = send_public_address;

let recv_add = rec_public_address;

let transfer_amount = web3.toWei(data.amount, 'ether');

let nonce = web3.toHex(web3.eth.getTransactionCount(send_public_address));

var private_key = privatekey_sender.slice(2);

let gas = web3.toHex(data.gas_limit);

let gasPrice = web3.toHex(data.gas_price);

var rawTx = {

        from: send_add,

        nonce: nonce,

        gasLimit: gas,

        gasPrice: gasPrice,

        value: web3.toHex(transfer_amount),

        to: recv_add,

    };

var transaction = new tx(rawTx);

var txData = new Buffer(private_key, 'hex');

transaction.sign(txData);

var serializedTx = transaction.serialize().toString('hex');

web3.eth.sendRawTransaction('0x' + serializedTx, function (err, txHash) {

  if (txHash) {

            next(null, txHash);

        }

        else if (err && err.message) {

            next(err.message, null);

        }

        else {

            next('Unable to sendRawTransaction', null);

        }

    });
2
  • Your code uses web3 v0.20.x and the question is about web3 v1.0
    – Ismael
    Commented Sep 4, 2018 at 16:15
  • @puneet please add some explanation to your answer...
    – Ahmed Imam
    Commented Aug 19, 2021 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.