sendSignedTransaction throws "Invalid Sender

150 views Asked by At

I was trying to save the content of a donation form to a blockchain but when I try I get Error: Returned error: invalid sender at Object.ErrorResponse. Can someone please help me?

const contractABI = require('./build/contracts/DonationContract.json');
const { Transaction } = require('ethereumjs-tx');

const MAX_TIME_FRAME = 60; // Adjust the maximum time frame as needed
const provider =new Web3.providers.HttpProvider('https://sepolia.infura.io/v3/+ infura key');//'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'
const web3 = new Web3(provider);
require('dotenv').config();


try {
    
    const networkId = await web3.eth.net.getId();
    const contractAddress = "0x";
    const contract = new web3.eth.Contract(contractABI.abi, contractAddress);

    const transactionObject =  contract.methods.createDonation(
      formData.street_address,
      formData.pickup_date,
      formData.pickup_time,
      formData.availability_date,
      formData.pickup_hours,
      formData.item_type,
      formData.other_item,
      formData.item_description,
      formData.quantity,
      formData.requires_refrigeration,
      formData.best_consumed_date,
      formData.partial_donation
    ); 
    const privateKey = process.env.PRIVATE_KEY;
    const fromAddress = 'etheruem address';


    const tx = new Transaction({
      nonce: await web3.eth.getTransactionCount(fromAddress),
      to: contractAddress,
      data: transactionObject.encodeABI(),
      gasPrice: web3.utils.toHex(await web3.eth.getGasPrice()),
      gasLimit: web3.utils.toHex(500000),
      value: web3.utils.toHex(0),
    });

    tx.sign(Buffer.from(privateKey, 'hex'));

    const serializedTx = tx.serialize();

    const receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
    {
      if (!err) {
        console.log(result);
      };
    }

Can someone please assist me.

1

There are 1 answers

3
Yilmaz On

From docs example, you have to mention the chain

var tx = new Tx(rawTx, {'chain':'ropsten'});

If you read the note on the same link

When using [email protected] if you don’t specify the parameter chain it will use mainnet by default.