首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web3js signTransation

Web3js signTransation
EN

Stack Overflow用户
提问于 2021-11-14 00:18:58
回答 1查看 124关注 0票数 0

我正在遵循文档,以便能够在Kovan testnet上签名和发送事务。当我控制txHash的时候,我得到了一个未定义的值。

代码语言:javascript
复制
web3.eth.getTransactionCount(account1, (err, txCount) => {
    // 1)Build Transaction
    const txObject = {
        nonce: web3.utils.toHex(txCount),
        to: account2,
        value: web3.utils.toHex(web3.utils.toWei('0.05', 'ether')),
        gasLimit: web3.utils.toHex(2100),
        gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
    }
    
     // 2)Sign Transaction
    const tx = new Tx(txObject,{'chain':42})
    tx.sign(privateKey1)
    
    const serializedTransaction = tx.serialize()
    const raw = '0x' + serializedTransaction.toString('hex')

    console.log("raw:", raw)
    console.log("tx:", serializedTransaction)

    // 3)Broadcast Transaction
    web3.eth.sendSignedTransaction(raw, (err, txHash) =>{
        console.log('txHash:', txHash)
    })
    // COMMENTED-OUT web3.eth.sendSignedTransaction('0x' + serializedTransaction .toString('hex'))
    // .on('receipt', console.log);
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-14 08:28:21

signTransaction()只执行签名。它不会将(签名的)事务广播到网络。

为此,您可以使用sendSignedTransaction() (docs),它将(签名和序列化的) tx数据提交给提供商,然后提供商将其广播到网络。

代码语言:javascript
复制
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('receipt', console.log);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69959320

复制
相关文章

相似问题

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