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 Answers 5

14

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

1
  • why does this work?
    – lhay86
    Commented Aug 18, 2020 at 9:54
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.

2

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

3
  • i am already using privatekey in buffer Commented Mar 4, 2020 at 6:13
  • @IshwarChandra: Did this method not work for you? If yes, then please elaborate on what error it gives you. Commented Mar 4, 2020 at 7:51
  • i have used your given solution but i am still getting same error Commented Mar 4, 2020 at 8:16
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.

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 
    }   

     

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.