以太坊离线签名交易接口sendRawTransaction()的用法

  • powervip
  • 更新于 2022-11-25 17:34
  • 阅读 397

以太坊离线签名交易接口sendRawTransaction()的用法

以普通转账为例,在以太坊控制台下离线签名交易的实现过程分以下步骤:

(1)使用接口unlockAccount()解锁本地钱包

personal.unlockAccount("0xe4697cBB8955526C703A4441f219cf0A04447a9B", "123", 0) true

(2)使用接口getTransactionCount()获取nonce的数值

eth.getTransactionCount("0xe4697cBB8955526C703A4441f219cf0A04447a9B") 0

说明: 为防止双花,每笔交易中都有一个随机数nonce。对于同一个账户,这个nonce是一个连续递增的整数,每发送1笔交易,nonce值加1。一个以太坊的账号,它发起的第1笔交易的nonce值是0,第2笔交易的nonce值是1,如此类推。使用接口getTransactionCount()获取指定账号发起过的交易笔数,可作为下一笔交易的nonce值

(3) 使用接口signTransaction()对交易数据进行离线签名

eth.signTransaction({ ...... nonce: 0, ...... gasPrice: '0x100000000', ...... gas: '1000000', ...... from:"0xe4697cBB8955526C703A4441f219cf0A04447a9B", ...... to:"0x815261DC4186502eC0D8CCFEf163785e1617b5A8", ...... value: '0x1' ...... }) { raw: "0xf8a980850100000000830f424094815261dc4186502ec0d8ccfef163785e1617b5a801808201dfa04ea1b2f2b4c02a2edbf1f2d483a9d2bd829fba1e88e3d615f6aa4210a2593622a05c32b412d78c5acdd97dbb7b0cac8d5e8613cc749cf236e309ad9a25a004a385a07b185c31906d2e9b2e2059d215fd4a2150feadae92ffde081566001352b94217a06fccf184dce61466f94f1c2dc244b1fa053f16a7079a0de78aa0dde35afe9aa7", tx: { gas: "0xf4240", gasPrice: "0x100000000", hash: "0x6c6ec0e55cc57af35ab90e4b9b7677d089e241db8b400dec87ff347d22558ace", input: "0x", nonce: "0x0", r: "0x4ea1b2f2b4c02a2edbf1f2d483a9d2bd829fba1e88e3d615f6aa4210a2593622", s: "0x5c32b412d78c5acdd97dbb7b0cac8d5e8613cc749cf236e309ad9a25a004a385", to: "0x815261dc4186502ec0d8ccfef163785e1617b5a8", v: "0x1df", value: "0x1" } }

说明: nonce:交易 gasPrice:gas价格 gas:该笔交易的gas limit from:转账发送账号, to:转账接收账号, value:转账金额,0x1表示0.000000000000000001个以太坊

(4)使用接口sendRawTransaction()对发送已经签名的交易数据

eth.sendRawTransaction("0xf8a980850100000000830f424094815261dc4186502ec0d8ccfef163785e1617b5a801808201dfa04ea1b2f2b4c02a2edbf1f2d483a9d2bd829fba1e88e3d615f6aa4210a2593622a05c32b412d78c5acdd97dbb7b0cac8d5e8613cc749cf236e309ad9a25a004a385a07b185c31906d2e9b2e2059d215fd4a2150feadae92ffde081566001352b94217a06fccf184dce61466f94f1c2dc244b1fa053f16a7079a0de78aa0dde35afe9aa7") "0x6c6ec0e55cc57af35ab90e4b9b7677d089e241db8b400dec87ff347d22558ace"

说明: sendRawTransaction()的参数,就是eth.signTransaction()的返回结果中的raw字段的内容,即已经签名的交易数据。 sendRawTransaction()返回交易的hash值

(5)使用接口getTransaction()获取交易成功后的交易详情

eth.getTransaction("0x6c6ec0e55cc57af35ab90e4b9b7677d089e241db8b400dec87ff347d22558ace") { blockHash: "0x668de63b825b627140c85949fbc9221fd9baaefb6ad123493020071d277297a1", blockNumber: 7857, from: "0xe4697cbb8955526c703a4441f219cf0a04447a9b", gas: 1000000, gasPrice: 4294967296, hash: "0x6c6ec0e55cc57af35ab90e4b9b7677d089e241db8b400dec87ff347d22558ace", input: "0x", nonce: 0, r: "0x4ea1b2f2b4c02a2edbf1f2d483a9d2bd829fba1e88e3d615f6aa4210a2593622", s: "0x5c32b412d78c5acdd97dbb7b0cac8d5e8613cc749cf236e309ad9a25a004a385", to: "0x815261dc4186502ec0d8ccfef163785e1617b5a8", transactionIndex: 0, v: "0x1df", value: 1, }

(6)使用接口getTransactionReceipt()获取交易成功后的交易收据

eth.getTransactionReceipt("0x6c6ec0e55cc57af35ab90e4b9b7677d089e241db8b400dec87ff347d22558ace") { blockHash: "0x668de63b825b627140c85949fbc9221fd9baaefb6ad123493020071d277297a1", blockNumber: 7857, contractAddress: null, cumulativeGasUsed: 21000, from: "0xe4697cbb8955526c703a4441f219cf0a04447a9b", gasUsed: 21000, logs: [], logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", status: "0x1", to: "0x815261dc4186502ec0d8ccfef163785e1617b5a8", transactionHash: "0x6c6ec0e55cc57af35ab90e4b9b7677d089e241db8b400dec87ff347d22558ace", transactionIndex: 0 }


我是powervip

我的公众号:区块链战斗机 我的网盘:https://pan.baidu.com/s/1frOZuMxdp5IVaY94ZdoN-A 我的知乎:https://www.zhihu.com/people/powervip 我的学习笔记:www.study.win <br>

如果你觉得这篇文章写得还可以,请帮忙点个赞,谢谢!著作权归作者所有。如需转载,请注明原文出处并保留原文链接。你的鼓励,我的动力!

本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
powervip

17 篇文章, 49 学分