0

Hi I'm using Web3js and Ganache. I'm trying to send a serialized transaction but got the following error: (node:90923) UnhandledPromiseRejectionWarning: Error: Returned error: Invalid Signature . I've tried to switch Node version from 14.x.x to 12.x.x but no success. Not sure if the issue comes from Web3js or from @ethereumjs/tx...

Any ideas? Anyone experienced the same issue?

Thanks!

const EthereumTx  = require("@ethereumjs/tx").Transaction;

const URL = 'http://127.0.0.1:7545';

const web3 = new Web3(URL);

const sendingAddress = '0xEF72345AD7616204dfD98D61716ba3222eb085eC';
const receivingAddress = '0x286164093efbaea6143F62212aA37A56Cd85362c';

web3.eth.getBalance(sendingAddress).then(console.log);
web3.eth.getBalance(receivingAddress).then(console.log);

// *********CREATE A TRANSACTION*********

const rawTransaction = {
    nonce: 0,
    to: receivingAddress,
    gasPrice: 20000000,
    gasLimit: 30000,
    value: 100,
    data: ""
};

// -- Step 7: Sign the transaction with the Hex value of the private key of the sender

const privateKeySender = 'PRIVATE KEY';
const privateKeySenderHex = new Buffer.from(privateKeySender, 'hex');
const transaction = new EthereumTx(rawTransaction);
// console.log('transaction', transaction);
transaction.sign(privateKeySenderHex)

// // -- Step 8: Send the serialized signed transaction to the Ethereum network.

const serializedTransaction = transaction.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex'));
2
  • Did you try to replace 'PRIVATE KEY' with the private key provided by Ganache? Commented Jan 2, 2022 at 14:28
  • Hi @IsmailElMoudni yes of course :) the problem most likely happens in sendSignedTransaction method
    – Eloi
    Commented Jan 2, 2022 at 17:28

1 Answer 1

0

transaction.sign(privateKeySenderHex) return new object try:

const signedTx = transaction.sign(privateKeySenderHex);
const serializedTransaction = signedTx.serialize();

full example:

singTransaction({ privateKey, to, value }: ArgsSingTransaction) {
 const hexPrivatekey = Buffer.from(privateKey, 'hex');

 const gasPrice = 20000000000;
 const gasLimit = 6721975;
 const nonce = 0;

 const tx = new Tx({ to: to, value: value, gasPrice, gasLimit, nonce, });

 const signedTx = tx.sign(hexPrivatekey);

 const serializedTx = signedTx.serialize();
 console.log(signedTx.isSigned())
 console.log(signedTx.validate())
 console.log(signedTx.verifySignature())
 console.log(signedTx.errorStr())

 return '0x' + serializedTx.toString('hex');
}

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.