(eth)以太坊jsonRPC钱包制作

发布时间:2018年10月17日 价值:20000.00 / 共识:35

0.以太坊安装:
1)构建:

  1. sudo apt-get install software-properties-common
  2. sudo add-apt-repository -y ppa:ethereum/ethereum
  3. sudo apt-get update
  4. sudo apt-get install ethereum

2)控制台启动:
geth —identity “maimai2018” //自定义节点身份名称
—datadir “/root/geth/test” //区块储同步的信息等存的位置,账户秘钥文件keystore也存储在此处
—rpc —rpccorsdomain “*” //开启rpc接口
—port “8090” //节点的端口
—rpcapi “db,eth,net,web3,personal,admin,miner” // 节点的方法库,personal为解锁加锁账户需要,一般都开启
—rpcaddr “0.0.0.0” //rpc允许外部所有ip
—rpcport “8111” //rpc http端口
—testnet //测试网
console //打开控制台

指令综合:

  1. geth --identity "chainwang2018" --datadir "/root/geth/test" --rpc --rpccorsdomain "*" --port "8090" --rpcapi "db,eth,net,web3,personal,admin,miner" --rpcaddr "0.0.0.0" --rpcport "8111" --testnet console

3)挂至后台输出日志:

  1. nohup geth --identity "chainwang2018" --datadir "/root/geth/test" --rpc --rpccorsdomain "*" --port "8090" --rpcapi "db,eth,net,web3,personal,admin,miner" --rpcaddr "0.0.0.0" --rpcport "8111" --testnet >> /home/geth.log

ps:以太坊输出或者输入的数字都是16进制的,数字和账户的前缀为0x(零X)开头,数字解析时去掉0x,然后转换成十进制即可。

HTTP POST请求
端口: ip:8111
其他jsonRPC参考网址:https://github.com/ethereum/wiki/wiki/JSON-RPC#json-rpc-methods

1.创建账户:
请求:

  1. {
  2. "jsonrpc":"2.0",
  3. "method": "personal_newAccount",
  4. "params": ["wangyaxing"], //账户密码
  5. "id":1}

输出:

  1. {
  2. "jsonrpc": "2.0",
  3. "id": 1,
  4. "result": "0x0466d7316d681b9646f3b766aa3872a182a6c557" //公钥地址
  5. }

PS:输出的钱包位置,在服务器中为:

2.监控交易:

监控交易时,写循环从某个高度开始跑,定时高度逐步增加,获取里面的交易,得到我们钱包账户的交易信息。

1)查询区块高度
请求:

  1. {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}

输出:

  1. {
  2. "jsonrpc": "2.0",
  3. "id": 1,
  4. "result": "0x3eea49" //此处为区块高度(去掉0X前缀,16进制的返回结果转换为十进制)
  5. }

2)通过区块高度,获取当前区块高度内的所有交易
请求:

  1. {"jsonrpc":"2.0",
  2. "method":"eth_getBlockByNumber", //方法
  3. "params":[
  4. "0x1b4444", //查询的区块高度
  5. true
  6. ],
  7. "id":1
  8. }

输出:

  1. {
  2. "jsonrpc": "2.0",
  3. "id": 1,
  4. "result": {
  5. "difficulty": "0x46a6992a9",
  6. "extraData": "0x526f707374656e20506f6f6c2042797a616e7469756d",
  7. "gasLimit": "0x6acfc0",
  8. "gasUsed": "0x6a7080",
  9. "hash": "0x2400fbdf20d4e9933b486064b9548dfd6f31dae56f3b59404f5298126aaedb30",
  10. "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  11. "miner": "0xf4d8e706cfb25c0decbbdd4d2e2cc10c66376a3f",
  12. "mixHash": "0xeb87a854fc1f6535b021b50df367bd44da3a73680436fc736d3631b05d488c4e",
  13. "nonce": "0x21c07165011475e5",
  14. "number": "0x1b4456",
  15. "parentHash": "0xd91e76563e6c85a6e564d6d279cb19719504d08035970b2e89404b69ead90f6e",
  16. "receiptsRoot": "0xcda7b6a4229f59c262b09172b822878a4512bd7e9650000d73a9359662175337",
  17. "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  18. "size": "0x3c2",
  19. "stateRoot": "0x1800f6b4850fac7570b2e6ee67abff36a29a07f3a1d3d562ed589ecf3130ba76",
  20. "timestamp": "0x59d16b05",
  21. "totalDifficulty": "0xc04129639959e",
  22. "transactions": [
  23. {
  24. "blockHash": "0x2400fbdf20d4e9933b486064b9548dfd6f31dae56f3b59404f5298126aaedb30",
  25. "blockNumber": "0x1b4456",
  26. "from": "0x7075355043c6af2d36c7b056f5daa5b02a9e283f",//从哪个账户转
  27. "gas": "0x4cb26", //gas的数量
  28. "gasPrice": "0x45d964b800",//gas的单价 gas和gasPrice相乘为消耗的转账费用,单位为wei
  29. "hash": "0x632ef6aea8eb203473cb90b6eec92639f6508dea7202aaf3a52bffadd776d9a1", ////交易hash
  30. "input": "0x",
  31. "nonce": "0x6d",
  32. "to": "0xe143b3e8646e5ccb3462aba805f1e7a019171cb4", //收账账户
  33. "transactionIndex": "0x0",
  34. "value": "0x16345785d8a0000",
  35. "v": "0x1b",
  36. "r": "0xd63c79c65b02a0bfedaa8c0b6bc41ac1497677a4311b465399034afce8a58981",
  37. "s": "0x364cab16e02688b930c8d119ae8da51577f69fcb73713c62ab1fa83a6ba83370"
  38. },
  39. {
  40. "blockHash": "0x2400fbdf20d4e9933b486064b9548dfd6f31dae56f3b59404f5298126aaedb30",
  41. "blockNumber": "0x1b4456",
  42. "from": "0x04ccc070c6007b2b422ab4b52bda60d5d8693abf",
  43. "gas": "0x3567e0",
  44. "gasPrice": "0x2e90eddabf",
  45. "hash": "0xb39d1448f1224612dbcd22ec3fd02dfa6dd5c5a476238d2219c0cb5cb369c3c7",
  46. "input": "0x60606040525b6f604060005360016000f30000000000006000525b602060106001f05061c3505a11601a575b600a8060376000396000f360606040526008565b00",
  47. "nonce": "0x67ce",
  48. "to": null,
  49. "transactionIndex": "0x1",
  50. "value": "0x100000",
  51. "v": "0x29",
  52. "r": "0xeabba9f6b6b06e9b38d6de1597f15275d3cb057e979eed1ccacdf304d00353f9",
  53. "s": "0x4961544f44ff8f1b55320ee912f8a8c75b1b067cb1b3806c2667d2593f733eb5"
  54. },
  55. {
  56. "blockHash": "0x2400fbdf20d4e9933b486064b9548dfd6f31dae56f3b59404f5298126aaedb30",
  57. "blockNumber": "0x1b4456",
  58. "from": "0x04ccc070c6007b2b422ab4b52bda60d5d8693abf",
  59. "gas": "0x3567e0",
  60. "gasPrice": "0x2e90eddac0",
  61. "hash": "0x0df4befcdc49de8091e7063812a21f1af65190d433bd292b0503d58e0832f30a",
  62. "input": "0x60606040525b6f604060005360016000f30000000000006000525b602060106001f05061c3505a11601a575b600a8060376000396000f360606040526008565b00",
  63. "nonce": "0x67cf",
  64. "to": null,
  65. "transactionIndex": "0x2",
  66. "value": "0x100000",
  67. "v": "0x29",
  68. "r": "0x23c3c7c1c9f1afc57e6c983129d8dbf52d33b4af50df08d24cadf919136d18d7",
  69. "s": "0x47cabe6731569c9b7a5c83ff0a1f648d79280af42f078ad788c2c5ba5e8c24b5"
  70. }
  71. ],
  72. "transactionsRoot": "0x44d58320d7bb0d336974af67ec6bc851e53d7f27e75e08cd6c64537f7828bf0a",
  73. "uncles": []
  74. }
  75. }

3.交易:

1)解锁账户:

  1. {"jsonrpc":"2.0",
  2. "method": "personal_unlockAccount", //解锁钱包方法名
  3. "params": [
  4. "0xf6833150d714baabbd7e5768c5f7b200ac180594", //钱包账户
  5. "wangyaxing", //钱包密码
  6. 30 //解锁时间
  7. ],
  8. "id":1}

输出:

  1. {
  2. "jsonrpc": "2.0",
  3. "id": 1,
  4. "result": true //返回true解锁成功
  5. }

2)交易:

  1. {
  2. "jsonrpc":"2.0","method":"eth_sendTransaction",
  3. "params":[{
  4. "from": "0xf6833150d714baabbd7e5768c5f7b200ac180594", //转账账户公钥
  5. "to": "0x7e95b67a2f91ddd2e3225745598bcf38ca9a9d5c", //接受账户公钥
  6. "gas": "0x76c0", //gas的数量
  7. "gasPrice": "0x9184e72a000",//gas的单价
  8. "value": "0x9184e72a", //转账金额,单位为wei
  9. "data": "" //转账附带的信息
  10. }],
  11. "id":1
  12. }

输出:

  1. {
  2. "jsonrpc": "2.0",
  3. "id": 1,
  4. "result": "0xe1a12bf15162016f28d263f9fcfc6ea7c1475d4a21b29ea9c46c60af1d580582" //交易TX哈希hash
  5. }

3)加锁:

  1. {
  2. "jsonrpc":"2.0",
  3. "method": "personal_lockAccount",
  4. "params": ["
  5. 0xf6833150d714baabbd7e5768c5f7b200ac180594 //要加锁的账户公钥
  6. "],
  7. "id":1
  8. }

4.通过交易tx哈希查询交易详情:
请求:

  1. {"jsonrpc":"2.0",
  2. "method":"eth_getTransactionByHash", //方法名
  3. "params":[
  4. "0xe1a12bf15162016f28d263f9fcfc6ea7c1475d4a21b29ea9c46c60af1d580582" //交易TX哈希hash
  5. ],
  6. "id":1
  7. }

输出:

  1. {
  2. "jsonrpc": "2.0",
  3. "id": 1,
  4. "result": {
  5. "blockHash": "0x4a924269e319f996548b2113c5fac2cda05b1030e2d46ce02afaa7afd089ee30", //交易所在的块的hash
  6. "blockNumber": "0x3eeb49", //交易所在的块的高度
  7. "from": "0xf6833150d714baabbd7e5768c5f7b200ac180594", //转账账户
  8. "gas": "0x76c0",
  9. "gasPrice": "0x9184e72a000",
  10. "hash": "0xe1a12bf15162016f28d263f9fcfc6ea7c1475d4a21b29ea9c46c60af1d580582", //交易hash
  11. "input": "0x",
  12. "nonce": "0x0",
  13. "to": "0x7e95b67a2f91ddd2e3225745598bcf38ca9a9d5c", //收账账户
  14. "transactionIndex": "0x0",
  15. "value": "0x9184e72a", //转账金额
  16. "v": "0x2a",
  17. "r": "0xdf57dc352f8f6edc0207ed7c52fe54110fab221a14331f55032d47dcf6e98cb4",
  18. "s": "0x4d081d3e58791d90209e62063f276ddff1ae12dba67a48d1319e677d4f6abcf1"
  19. }
  20. }

5.查看账户列表:

  1. {"jsonrpc":"2.0","method": "personal_listAccounts", "params": [],"id":1}

输出:

  1. {
  2. "jsonrpc": "2.0",
  3. "id": 1,
  4. "result": [
  5. "0xf6833150d714baabbd7e5768c5f7b200ac180594",
  6. "0x7e95b67a2f91ddd2e3225745598bcf38ca9a9d5c",
  7. "0x0466d7316d681b9646f3b766aa3872a182a6c557",
  8. "0x1203fd985625a1082b12adc4d4c390815e240977"
  9. ]
  10. }
  • 分享 收藏
0 条评论
  • 这篇文章暂无评论,赶紧评论一下吧~