Back

[**] - 包含了钱包的操作-各种关系-等等 bitcoind btc 的RPC用法 和 部分 bitcoind-cli 的用法

发布时间: 2018-12-13 10:20:00

参考:  http://cw.hubwiz.com/card/c/bitcoin-json-rpc-api/1/7/12/  (这个是所有命令的列表)

关于同时传入密码, 参考:  https://bitcoin.stackexchange.com/questions/19665/json-rpc-via-curl/42052#42052?newreg=03905cc8cd974404acd85c2e8b48b8c8

概念: 参考 https://bitcoin.stackexchange.com/questions/7490/what-is-the-difference-between-accounts-and-adresses

一个full node 就是一个wallet

一个wallet  包含了多个account  ( getnewaddress的时候传入的参数,就是account的名字, 新版本中也管account叫label)

一个account 包含了多个 address

1. 新建 address , 以及同时传入用户名和密码     getnewaddress  (可带参数)

$ curl --user your_username:your_password --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getnewaddress", "params": []}' -H 'content-type:text/plain;' http://127.0.0.1:8332/
Enter host password for user 'coiex':
{"result":"3GheLjNxTcuNEztG8x4WP8TLzkPLzjxKm2","error":null,"id":"curltest"}

这个新地址以 加密的形式保存在了  BTC数据文件的 wallet.dat中,例如:

/opt/btc_data/wallet.dat 

备份的话,备份这个文件就可以了.

也可以使用带参数的形式来创建BTC account: 

curl --user coiex:levelUP8899 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getnewaddress", "params": ["kitty_btc_account"]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/
{"result":"3KAueRCHG3pZipuNRdaPTGtq6CZBkwasHF","error":null,"id":"curltest"}

2. 备份钱包

可以直接复制 /opt/btc_data/wallet.dat 这个文件. 

也可以通过RPC来实现:

curl --user name:your_password --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "backupwallet", "params": ["/home/shensiwei/wallet.dat"]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/

3. 查询余额  ( account, address) 

3.0 查询某个account:  使用 getbalance , 接受参数: account   (如果account参数是空, 就查询整个wallet的) 

下面是分别查询 kitty_btc_account, 默认钱包(名称为空 ""), 总钱包, 

$ curl --user coiex:levelUP8 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getbalance", "params": ["kitty_btc_account"]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/ | json_pp

{
   "id" : "curltest",
   "result" : 0.00321,
   "error" : null
}

$ curl --user coiex:levelUP8 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getbalance", "params": [""]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/ | json_pp

{
   "id" : "curltest",
   "result" : 0.00586136,
   "error" : null
}

$ curl --user coiex:levelUP8 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getbalance", "params": []}' -H 'content-type:text/plain;' http://127.0.0.1:8332/ | json_pp

{
   "error" : null,
   "result" : 0.00907136,
   "id" : "curltest"
}

在 blockchain.com 上注册用户,申请API,才行.  注册申请API的地址: https://api.blockchain.info/customer/signup

3.1 简单版: 根据address来查询

https://blockchain.info/balance?active=34shDKnPjUzLvFuVn2y1wGGfsuMNFrCHAK

(这个接口可以使用 "|" 来安排多个地址)

返回的如下(单位是 sat, 就是btc的最小单位. )

{
    34shDKnPjUzLvFuVn2y1wGGfsuMNFrCHAK: {
        final_balance: 1146100,     ( 0.0114 btc )
        n_tx: 1,
        total_received: 1146100
    }
}

3.2 复杂版 根据address来查询 

https://blockchain.info/rawaddr/34shDKnPjUzLvFuVn2y1wGGfsuMNFrCHAK 

参考: https://www.blockchain.com/zh/api

这个会返回该地址的详细情况. 

3.3 ( RPC 查询某个address的余额:   不行. 不存在该方法.  参考这个帖子 https://bitcoin.stackexchange.com/questions/10090/how-to-get-an-addresss-balance-with-the-bitcoin-client  )

4. 查询某个address 对应的account : getaccount

curl --user name:pwd --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getaccount", "params": ["3KAueRCHG3pZipuNRdaPTGtq6CZBkwasHF"]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/
{"result":"","error":null,"id":"curltest"}

{
   "id" : "curltest",
   "error" : null,
   "result" : "kitty_btc_account"
}

5. 发起转账

5.1 费用: 可以来这里查询:    https://bitcoinfees.earn.com/api/v1/fees/recommended

{
  fastestFee: 18,
  halfHourFee: 18,
  hourFee: 12
}

5.2 设置矿工费:   settxfee  ,例如 , 设置成31 sat.   = 0.00000031

curl --user coiex:pwd --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "settxfee", "params": ["0.00000031"]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/
{"result":true,"error":null,"id":"curltest"}

5.3 (可选) 设置好从哪个account出来. 注意: btc的转账不是从address中出来,而是从 account出来.默认是 '' 这个account  . 

参考: https://bitcoin.stackexchange.com/questions/70464/can-sendtoaddress-use-account-other-than

account 这个概念在 0.17版本中已经被标记为 deprecated. 所以建议新建address的时候,也不要设置 account了, 默认是空就好.

这里使用 sendfrom 命令来设置 (命令略)

5.4 开始转账: sendtoaddress

第一个参数是 目标地址, 第二个参数是打币的数量. 

$ curl --user coiex:levelUP8899 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "sendtoaddress", "params": ["34shDKnPjUzLvFuVn2y1wGGfsuMNFrCHAK", 0.003]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/

 {"result":"723dce1992811e29e382f89ce55f3e430fc3d7446c085025d5b605b879b64e62","error":null,"id":"curltest"}

我们到blockchain.com 上查询一下,就可以看到了 :

https://www.blockchain.com/btc/tx/723dce1992811e29e382f89ce55f3e430fc3d7446c085025d5b605b879b64e62

这个时候, bitcoind的日志是(debug.log)

2018-12-14 08:22:46 keypool added 1 keys (1 internal), size=2000 (1000 internal)
2018-12-14 08:22:46 keypool reserve 1003
2018-12-14 08:22:46 Fee Calculation: Fee:166 Bytes:166 Needed:166 Tgt:0 (requested 0) Reason:"Minimum Required Fee" Decay 0.00000: Estimation: (-1 - -1) -nan% 0.0/(0.0 0 mem 0.0 out) Fail: (-1 - -1) -nan% 0.0/(0.0 0 mem 0.0 out)
2018-12-14 08:22:46 CommitTransaction:
CTransaction(hash=bef85f6d74, ver=2, vin.size=1, vout.size=2, nLockTime=553754)
    CTxIn(COutPoint(c2860172f2, 1), scriptSig=16001449fe8c1570929e3f5f, nSequence=4294967294)
    CScriptWitness(3045022100f6707d363fc804e86e4f15aba01289fc9ee5609c156029dc2e864dd2e4c64bf202203adeb977c16a18e6e49adba9c013f6180f33878fec7e9da4e99734040ba8bf1301, 0256a582fb4d97147fa8239be22865586aebabdb1d0495ba6d458face1e1e94c88)
    CTxOut(nValue=0.00122000, scriptPubKey=a914a4a84941ed7c8e4d90b52d4491)
    CTxOut(nValue=0.00597302, scriptPubKey=a914b5fed3fb9582885bc4a25c4af7)
2018-12-14 08:22:46 keypool keep 1003
2018-12-14 08:22:46 AddToWallet bef85f6d7400ffd9ecf029d0bc3e705b5e6028a72893ebb4a88a120df6e0075b  new
2018-12-14 08:22:46 Relaying wtx bef85f6d7400ffd9ecf029d0bc3e705b5e6028a72893ebb4a88a120df6e0075b
2018-12-14 08:22:46 AddToWallet bef85f6d7400ffd9ecf029d0bc3e705b5e6028a72893ebb4a88a120df6e0075b  

但是这个tx刚发出去, 还没有被确认, 所以在搜索的时候, 网站会有红色的 unconfirmed 的提示

这个时候, 打开看 blockchain提供的API, 也是发现不太正常的: (ver 都是2) 

要根据返回的tx id, 来查看API:  (注意下面的红色区域和文字,  本次转账的数量是  0.00133)  

url:   https://blockchain.info/rawtx/4584a85e4e84b21e0dcc8a451c9e8625eb949e870208c5144572b28dc958cca1

 (下面的图是查address的,可以不看了, 查询地址为: ) https://blockchain.info/rawaddr/34shDKnPjUzLvFuVn2y1wGGfsuMNFrCHAK

我把上图中的第一个 tx 的相关区域放开, 做个截图, 留意其中的红色区域:

等一会儿, 大约等了3首歌的时间吧, 发现debug.log 更新了下面的内容:  (还没包括最上面的第四笔 0.00133 的例子, 不过没关系, 大概就是这个意思)

2018-12-14 08:24:03 Pre-allocating up to position 0x1000000 in rev01461.dat
2018-12-14 08:24:03 UpdateTip: new best=000000000000000000083b851959281688bc439d667e3eafb5d6a5c20df7d2df height=553756 version=0x20000000 log2_work=90.124863 tx=364401776 date='2018-12-14 08:23:56' progress=1.000000 cache=65.3MiB(500958txo) warning='27 of last 100 blocks have unexpected version'
2018-12-14 08:25:46 UpdateTip: new best=00000000000000000007ae125a72efa903ec0baba38208ce0d064aef266a4ad0 height=553757 version=0x20000000 log2_work=90.124889 tx=364402432 date='2018-12-14 08:24:55' progress=1.000000 cache=65.2MiB(500607txo) warning='27 of last 100 blocks have unexpected version'
2018-12-14 08:26:07 UpdateTip: new best=0000000000000000001d8d4ad4f1070a76c9116d3f81f207ae96754da5ac5b3e height=553758 version=0x20400000 log2_work=90.124915 tx=364402897 date='2018-12-14 08:25:46' progress=1.000000 cache=65.3MiB(500972txo) warning='28 of last 100 blocks have unexpected version'
2018-12-14 08:26:07 AddToWallet 723dce1992811e29e382f89ce55f3e430fc3d7446c085025d5b605b879b64e62  update  # 第一笔, 0.003 
2018-12-14 08:26:07 AddToWallet c2860172f228dfa55c625ad76aedf7d32925e029d1a59923d67d1e2f1855f884  update  # 第二笔 
2018-12-14 08:26:07 AddToWallet bef85f6d7400ffd9ecf029d0bc3e705b5e6028a72893ebb4a88a120df6e0075b  update  # 这就是我发起的第三比交易. 0.00122

这个时候, 打开 blockchain.com 来搜索一下, 可以看到,  已经有若干次确认了

https://www.blockchain.com/btc/tx/723dce1992811e29e382f89ce55f3e430fc3d7446c085025d5b605b879b64e62

确认后的接口如下: , 可以看到有了 block_height,   下面的spent : 变成了  true

5.4.2  如何确定 确认数? (confirmed ? )  (参考:  https://bitcoin.stackexchange.com/questions/60567/how-to-check-confirmations-number-programmatically

最新的block号: 在这里查询:  https://blockchain.info/latestblock

$ curl 'https://blockchain.info/latestblock'

{
    "hash":"0000000000000000002aa93042782d42f612270dd5d303e99bc911cdc2df9973",
    "time":1544839655,
    "block_index":1736421,
    "height":553868,
    "txIndexes":[397461298,397460984,397460985,............]
 }

从API中获得该tx对应的block height , 然后用  最新高度 - block_height + 1 , 就是确认数

5.5 其他: 这篇文章提到了BTC转账的几个不同接口.   https://blog.csdn.net/a013152/article/details/81668629

5.6 这篇文章提到了 bitcoin 中 的转账, 从地址到地址是无意义的.  https://bitcointalk.org/index.php?topic=1795115.0

6.  address 与 account 

6.1 根据address 获得   account   ,  (没太大意义)

curl --user coiex:my_passwd --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getaccount", "params": ["3KAueRCHG3pZipuNRdaPTGtq6CZBkwasHF"]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/
{"result":"kitty_btc_account","error":null,"id":"curltest"}

注意: 这里返回 result: ''  是正常的, 说明当前的address 对应的 account的名字就是空.

6.2 获得本钱包中所有收到过款的 address

curl --user coiex:levelUP8899 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "listreceivedbyaddress", "params": []}' -H 'content-type:text/plain;' http://127.0.0.1:8332/ | json_pp

{
   "id" : "curltest",
   "error" : null,
   "result" : [
      {
         "account" : "",
         "confirmations" : 219,
         "address" : "32h5ez5tnYmb3ji2fjPQ5RJGyp1ELzjAiS",
         "amount" : 0.011398,
         "txids" : [
            "c15e9eb219c22696937d3567354e4103b3ce6183211e24b2f4df872bd91e9839"
         ],
         "label" : ""
      },
      {
         "label" : "",
         "txids" : [
            "bef85f6d7400ffd9ecf029d0bc3e705b5e6028a72893ebb4a88a120df6e0075b"
         ],
         "address" : "3GheLjNxTcuNEztG8x4WP8TLzkPLzjxKm2",
         "confirmations" : 108,
         "account" : "",
         "amount" : 0.00122
      }
   ]
}

6.3 列出所有的 address 

curl --user user:pwd --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "listaddressgroupings", "params": []}' -H 'content-type:text/plain;' http://127.0.0.1:8332/ | json_pp

{
   "result" : [
      [
         [
            "32h5ez5tnYmb3ji2fjPQ5RJGyp1ELzjAiS",
            0,
            ""
         ],
         [
            "38qgF8bQoBg7SbsohQa5A69WwZji95uxLb",
            0
         ],
         [
            "3Hv6yBjfiEx24Rg6xAm95FGHNeBXygRJtJ",
            0
         ],
         [
            "3JHKW2dVYqRzb6UTDy35JRbUCur1tC9di8",
            0
         ],
         [
            "3PzsLzNVoDtSeGLRSpoDDe2FoeGh18iEDo",
            0.00464136
         ]
      ],
      [
         [
            "3GheLjNxTcuNEztG8x4WP8TLzkPLzjxKm2",
            0.00122,
            ""
         ]
      ]
   ],
   "id" : "curltest",
   "error" : null
}

7. 根据account 获得 address (跟充提币没太大关系)

7.1 有account, 就列出该 account下的address

curl --user coiex:passwd --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getaddressesbyaccount", "params": ["kitty_btc_account"]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/
{"result":["3KAueRCHG3pZipuNRdaPTGtq6CZBkwasHF"],"error":null,"id":"curltest"}

7.2 没有account, (传入的参数是空字符串), 则列出本节点上的所有address

shensiwei@bitcoind-server:/opt/btc_data$ curl --user coiex:passwd --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getaddressesbyaccount", "params": [""]}' -H 'content-type:text/plain;' http://127.0.0.1:8332/
{"result":["32h5ez5tnYmb3ji2fjPQ5RJGyp1ELzjAiS","3GheLjNxTcuNEztG8x4WP8TLzkPLzjxKm2","3M2MLg1sozcuUZpLz6RUBcxoHNYSMQsNX5","3PmhuGaCT94z7zp8z6jp42DWoXJjdKugFM"],"error":null,"id":"curltest"}

8. 查看当前的情况,版本号:

8.1 查看当前bitcoin的wallet情况:

$ bitcoin-cli getwalletinfo
{
  "walletname": "wallet.dat",
  "walletversion": 159900,
  "balance": 0.01139800,
  "unconfirmed_balance": 0.00000000,
  "immature_balance": 0.00000000,
  "txcount": 1,
  "keypoololdest": 1539698055,
  "keypoolsize": 999,
  "keypoolsize_hd_internal": 1000,
  "paytxfee": 0.00000040,
  "hdmasterkeyid": "dc880427f036d4577942c4c7142f279552c9682a"
}

8.2 查看当前bitcoin的版本号等情况

 bitcoin-cli getnetworkinfo
{
  "version": 160300,   // 这个就是精准的版本号  
  "subversion": "/Satoshi:0.16.3/",   // 这个就是发布用的版本号
  "protocolversion": 70015,
  "localservices": "000000000000040d",
  "localrelay": true,
  "timeoffset": 0,
  "networkactive": true,
  "connections": 8,
  "networks": [
    {
      "name": "ipv4",
      "limited": false,
      "reachable": true,
      "proxy": "",
      "proxy_randomize_credentials": false
    },
    {
      "name": "ipv6",
      "limited": false,
      "reachable": true,
      "proxy": "",
      "proxy_randomize_credentials": false
    },
    {
      "name": "onion",
      "limited": true,
      "reachable": false,
      "proxy": "",
      "proxy_randomize_credentials": false
    }
  ],
  "relayfee": 0.00001000,
  "incrementalfee": 0.00001000,
  "localaddresses": [
  ],
  "warnings": ""
}

Back