'How to fix Invalid sender error when sending a signed transaction with ethereumjs-tx?

I am getting invalid sender when trying to send a signed transaction. Here is the error:

Uncaught (in promise) Error: Returned error: invalid sender

and this is the code:

  var privateKey = Buffer.from('private key without 0x', 'hex');

  var rawTx = {
    nonce: await web3.utils.toHex(web3.eth.getTransactionCount(account1)),
    to: account2,
    value: web3.utils.toHex(web3.utils.toWei('1', 'ether')),
    gasLimit: web3.utils.toHex(21000),
    gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
  }

  var tx = new Transaction(rawTx, {'chain':'ropsten'});
  tx.sign(privateKey);

  var serializedTx = tx.serialize();
  console.log('serializedTx', serializedTx)

  web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
  .on('receipt', console.log);

What could be the problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source