web3.eth.personal.signTransaction - 为交易生成签名

优质
小牛编辑
131浏览
2023-12-01

对交易进行签名,账户必须先解锁。

注意:在未加密的HTTP连接上发送账户密码有巨大的风险!

调用:

web3.eth.personal.signTransaction(transaction, password [, callback])

参数:

  • transaction:Object - The transaction data to sign web3.eth.sendTransaction() for more.
  • password:String - The password of the from account, to sign the transaction with.
  • callback:Function - (optional) Optional callback, returns an error object as first parameter and the result as second.

返回值:

一个Promise对象,其解析值为RLP编码的交易对象,其raw属性可以使用web3.eth.sendSignedTransaction()发送交易。

示例代码:

web3.eth.signTransaction({
    from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0",
    gasPrice: "20000000000",
    gas: "21000",
    to: '0x3535353535353535353535353535353535353535',
    value: "1000000000000000000",
    data: ""
}, 'MyPassword!').then(console.log);
> {
    raw: '0xf86c808504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a04f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88da07e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0',
    tx: {
        nonce: '0x0',
        gasPrice: '0x4a817c800',
        gas: '0x5208',
        to: '0x3535353535353535353535353535353535353535',
        value: '0xde0b6b3a7640000',
        input: '0x',
        v: '0x25',
        r: '0x4f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88d',
        s: '0x7e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0',
        hash: '0xda3be87732110de6c1354c83770aae630ede9ac308d9f7b399ecfba23d923384'
    }
}