詳細(xì)使用請(qǐng)看視頻—— Bitcoin調(diào)用rpc查詢區(qū)塊地址
配置文件
Mac 系統(tǒng)下 默認(rèn)配置文件文件在 ~/Library/Application\ Support/Bitcoin
放在 /mnt/bitcoin 目錄下
# /mnt/bitcoin/bitcoin.conf
server=1
daemon=1
rpcallowip=127.0.0.1/0 #局域網(wǎng)訪問
rpcthreads=10
txindex=1
rpcuser=user #用戶
rpcpassword=pass #密碼
[regtest] #從 0.17.0開始的圆,在一個(gè)配置文件中設(shè)置不同的啟動(dòng)模式
rpcport=18443
deprecatedrpc=signrawtransaction # 從 0.17.0版本signrawtransaction 要被棄用
運(yùn)行節(jié)點(diǎn)
bitcoind -datadir=/mnt/bitcoin # 確保有/mnt/bitcoin/bitcoin.conf
# 運(yùn)行regtest模式,即私鏈模式
bitcoind -regtest -datadir=/mnt/bitcoin
# 私鏈持續(xù)挖礦
while true; do bitcoin-cli -regtest -datadir=/mnt/bitcoin generate 1; sleep 1; done
curl getblockhash
如果沒有配置 rpcuser停蕉、 rpcpassword铜犬,curl 不會(huì)返回?cái)?shù)據(jù)
curl -X POST http://127.0.0.1:8332/ -u user:pass -H 'content-type:text/plain;' \
-d '{"jsonrpc":"1.0","id":"1","method":"getblockhash","params":[0]}'
{
"result": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
"error": null,
"id": "1"
}
curl getblockcount
curl -X POST http://127.0.0.1:8332/ -u user:pass -H 'content-type:text/plain;' \
-d '{"jsonrpc":"2.0","id":"1","method":"getblockcount","params":[]}'
{
"result":238667,
"error":null,
"id":"1"
}
python script
import json
import requests
url = "http://127.0.0.1:8332/"
auth=("user", "pass")
payload = json.dumps({"method": 'getblockhash', "params": [1] })
response = requests.request("POST", url, data=payload, auth=auth)
print(response.text)
參考:
https://bitcoincore.org/bin
https://bitcoin.org/en/release/v0.17.0#configuration-sections-for-testnet-and-regtest
https://github.com/blockchain-etl/bitcoin-etl
https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)
https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list
https://bitcoin.stackexchange.com/questions/19665/json-rpc-via-curl