首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sendSignedTransaction -(节点:90923) UnhandledPromiseRejectionWarning:错误:返回错误:无效签名

sendSignedTransaction -(节点:90923) UnhandledPromiseRejectionWarning:错误:返回错误:无效签名
EN

Ethereum用户
提问于 2022-01-01 19:00:49
回答 1查看 230关注 0票数 0

嗨,我用的是Web3jsGanache。我试图发送一个序列化的事务,但得到了以下错误:(node:90923) UnhandledPromiseRejectionWarning: Error: Returned error: Invalid Signature。我试图将Node版本从14.x.x转换为12.x.x,但没有成功。不确定问题是来自Web3js还是来自@ethereumjs/tx.

有什么想法吗?有谁经历过同样的问题吗?

谢谢!

代码语言:javascript
复制
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'));
EN

回答 1

Ethereum用户

发布于 2022-04-01 10:21:49

transaction.sign(privateKeySenderHex)返回新对象尝试:

代码语言:javascript
复制
const signedTx = transaction.sign(privateKeySenderHex);
const serializedTransaction = signedTx.serialize();

完整的例子:

代码语言:javascript
复制
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');
}
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/117756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档