5

Getting error while creating ethereum raw transaction

var rawTx = {
  nonce: '0x2a',
  gasPrice: '0x4a717c800',
  gasLimit: '0xc340',
  to: '0x73F7Ced8cc9D27DC426210c32fc6d0a40f941eE1',
  value: '0x2386f26fc10000',
  data: '',
  chainID: 4 
}      
var cTx = await new EthereumTx(rawTx, { 'chain': 'rinkeby' });
cTx.sign(privateKeyInBuffer);

5 Answers5

14

try change data :'' to data:"0x"

曹海涛
  • 156
  • 1
  • 3
4

ethereum-utils checks string fields if they are hex. When you send data field as empty, then it throws the error. You can send data as '0x' like they said above.

ikc1903
  • 41
  • 1
2

Replace privateKey with Buffer.from(privateKey.slice(2), "hex").

goodvibration
  • 26,003
  • 5
  • 46
  • 86
2

This might be happening, because you are trying to sign the raw transaction with private key in string format, but it has to be hex. Change:

cTx.sign(privateKey)

To:

cTx.sign(new Buffer(privateKey, 'hex'))

And on a side note, here var cTx = await new EthereumTx(rawTx, { 'chain': 'rinkeby' }); you don't have to pass chain parameter, you already specified to which blockchain network the transaction should be sent in rawTx where you placed this parameter chainID: 4. Full list with blockchain networks can be found here.

Miroslav Nedelchev
  • 3,519
  • 2
  • 10
  • 12
1

Simple solution

 var rawTx = {
      nonce: '0x2a',
      gasPrice: '0x4a717c800',
      gasLimit: '0xc340',
      to: '0x73F7Ced8cc9D27DC426210c32fc6d0a40f941eE1',
      value: '0x2386f26fc10000',
      data: '0x',=====================>>**change empty data string to 0x**
      chainID: 4 
    }

DeV
  • 111
  • 3