[Ethereum] Sending a raw transaction

go-ethereumraw-transactionsendrawtransactionsignature

I'm trying to send ether from one address to another. I have a valid generated address and a private key for that. I have a node is working on Linux in fullmode and RPC server listening on the default port. According to JSON RPC Wiki I'm trying to create the following transaction:

String transaction = "
{
  "from": "my address",
  "to": "an address of a recipient",
  "value": "a value in Wei converted to Hex String",
  "chainId": 1,
  "nonce": 0
}"

And then I'm creating a signature like

String txData = makeKeccak256(privateKey + transaction.toHex());

And sending this string as Hex to RPC server. But I'm getting the following error:

rlp: expected input list for types.txdata

I've read that the gasPrice and gas are calculating automatically and are optional. So please help me to find out how to send the transaction correctly. Thank you

Best Answer

You can also sign a transaction using ethereumjs-tx. Give this try,

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

const rawTx = {
  from: 'Your account address,
  to: 'Recipient account address',
  value: 'Values in wei
}

var tx = new Tx(rawTx);

tx.sign('Your private key in buffer');

var stx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + stx.toString('hex'), (err, hash) => {
  if (err) { 
    console.log(err);
    return; 
  }else{
    console.log(hash);
});

To send exact values of nonce and gasPrice use web3.eth.estimateGas and web3.eth.getTransactionCount methods.

Related Topic