Getting Invalid Sender Error while eth_sendRawTransaction

async function Main(recieverAdd, value) {
// Configuring the connection to an Ethereum node
const network = ‘sepolia’;
const web3 = new Web3(
new Web3.providers.HttpProvider(
https://sepolia.infura.io/v3/4c37afbbaa224d3fa8e4bb42c311xxxx’,
),
);
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
‘d2c2930xxxxxxxx’,
);
web3.eth.accounts.wallet.add(signer);
console.log(‘Sending to ’ + recieverAdd + ’ ’ + value + ’ ETH’);
// Estimatic the gas limit
var limit = await web3.eth.estimateGas({
from: signer.address,
to: recieverAdd,
value: web3.utils.toWei(value),
});
// .then(console.log);

// Creating the transaction object
const tx = {
  from: signer.address,
  to: recieverAdd,
  value: web3.utils.numberToHex(web3.utils.toWei(value, 'ether')),
  gas: web3.utils.toHex(limit),
  nonce: await web3.eth.getTransactionCount(signer.address),
  maxPriorityFeePerGas: web3.utils.toHex(web3.utils.toWei('2', 'gwei')),
  chainId: 3,
  // chain: 'sepolia',
  type: 0x2,
};

console.log(signer.privateKey);

const signedTx = await web3.eth.accounts.signTransaction(
  tx,
  signer.privateKey,
);
console.log('Raw transaction data: ' + signedTx.rawTransaction);

// Sending the transaction to the network
try {
  fetch('https://sepolia.infura.io/v3/4c37afbbaa224d3fa8e4bb42c311xxxx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      method: 'eth_sendRawTransaction',
      params: [signedTx.rawTransaction],
      id: 1,
    }),
  })
    .then(response => response.json())
    .then(data => {
      console.log('Success:', data);
    })
    .catch(error => {
      console.error('Error:', error);
    });

  // console.log(`Mined in block ${receipt.blockNumber}`);
} catch (e) {
  console.log(e);
}

}

this responds with an error > Success: {“error”: {“code”: -32000, “message”: “invalid sender”}, “id”: 1, “jsonrpc”: “2.0”}

I am using account from metamask wallet and “web3”: “^1.9.0”

what should i do?

4 Likes

Hi @Frozen_Samurai do you still have an issue with it please check earlier post here

2 Likes

I solved the issue, by changing the chainId to 11155111 (for sepolia network only), everything works for me.

3 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.