嗨,我用的是Web3js和Ganache。我试图发送一个序列化的事务,但得到了以下错误:(node:90923) UnhandledPromiseRejectionWarning: Error: Returned error: Invalid Signature。我试图将Node版本从14.x.x转换为12.x.x,但没有成功。不确定问题是来自Web3js还是来自@ethereumjs/tx.
有什么想法吗?有谁经历过同样的问题吗?
谢谢!
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'));发布于 2022-04-01 10:21:49
transaction.sign(privateKeySenderHex)返回新对象尝试:
const signedTx = transaction.sign(privateKeySenderHex);
const serializedTransaction = signedTx.serialize();完整的例子:
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');
}https://ethereum.stackexchange.com/questions/117756
复制相似问题