'sendSignedTransaction: Invalid sender error on Quorum

I am trying to call a method on a Smart Contract on a node app using an account that I create from a private key using web3 on a private quorum blockchain. I am using the code below, but I keep getting the error "Error: Returned error: invalid sender". Here's the code:

const Web3 = require("web3")
const web3 = new Web3(RPC_NODE);

var contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADRESS);
var myMethod = contract.methods.myMethod(param1, param2);
var encodedABI = myMethod.encodeABI()

const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);

var tx = {
  from: account.address,
  to: CONTRACT_ADRESS,
  gas: 2000000,
  data: encodedABI
};

console.log("Account Address:", account.address);

account.signTransaction(tx).then(signed => {
  var tran = web3.eth.sendSignedTransaction(signed.rawTransaction);
  console.log("Raw Signed Transaction:", signed.rawTransaction);
});

The error makes sense, since if I have a look at the logs, this is what I get:

Account Address: 0xBd55e32CB2559b06511D03E9B37a1c3bfF0f35Cd

Raw Signed Transaction: '0xf901050480831e84809473341607498eb0648db01ed55c378c3ea227bf6980b8a47d8febab00000000000000000000000000000000000000000000000000000000000000401e4008f33fa1479a536072f9b06e978689c47527593b9a29853e2c708ab75293000000000000000000000000000000000000000000000000000000000000002463663038646139362d363665332d343034312d393630652d3030393838636433383865320000000000000000000000000000000000000000000000000000000078a0700c0b30a6a58016a9621033fe71974eb0cc8721841353b7aa5e42f96d122a7aa023fa6bf121c3c83de1c03674edf90c5bf9ad55165ac10a43965318cdde65e438'

And if I decode the raw signed transaction I see:

{
  "nonce": 4,
  "gasPrice": null,
  "gasLimit": 2000000,
  "to": "0x73341607498eb0648db01ed55c378c3ea227bf69",
  "value": 0,
  "data": "7d8febab00000000000000000000000000000000000000000000000000000000000000401e4008f33fa1479a536072f9b06e978689c47527593b9a29853e2c708ab75293000000000000000000000000000000000000000000000000000000000000002463343866323863322d353932622d343930622d616663322d62323262366164303035653900000000000000000000000000000000000000000000000000000000",
  "from": "0xbd55e32cb2559b06511d03e9b37a1c3bff0f35cd",
  "r": "d423912021ff81d53c63da08374f9543df1fca3b1d76f085148310d51afb64f1",
  "v": "77",
  "s": "0a212017b256986ca24f151bd920817ae106624b54c04ea702ffd03ab288205c"
}

As you can see, the "from" field from the signed raw transaction does not match the address of the account I created by using the private key. Can somebody explain this behaviour?



Solution 1:[1]

I solved it by adding the correct chainId to the tx Object

Solution 2:[2]

Last time I came across a similar issue, I struggled with the format of the privateKey.

Depending on which version of web3 you use, this formatting might have an impact on the account created via privateKeyToAccount.

I guess you are missing a 0x prefix. Make sure your PRIVATE_KEY is prepended with a "0x" and give it another try.

Solution 3:[3]

You can solve this by adding the chainId, like this one var tx = new Tx(rawTx, {'chain':'ropsten'});

For your code you can do this =

account.signTransaction(tx, {'chain':'ropsten'}).then(signed => {
  var tran = web3.eth.sendSignedTransaction(signed.rawTransaction);
  console.log("Raw Signed Transaction:", signed.rawTransaction);
});

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 jmg
Solution 2 norym
Solution 3 Sougata Das