详细使用请看视频—— Bitcoin调用rpc查询区块地址
配置文件
Mac 系统下 默认配置文件文件在 ~/Library/Application\ Support/Bitcoin
放在 /mnt/bitcoin 目录下
# /mnt/bitcoin/bitcoin.conf
server=1
daemon=1
rpcallowip=127.0.0.1/0 #局域网访问
rpcthreads=10
txindex=1
rpcuser=user #用户
rpcpassword=pass #密码
[regtest] #从 0.17.0开始,在一个配置文件中设置不同的启动模式
rpcport=18443
deprecatedrpc=signrawtransaction # 从 0.17.0版本signrawtransaction 要被弃用
运行节点
bitcoind -datadir=/mnt/bitcoin # 确保有/mnt/bitcoin/bitcoin.conf
# 运行regtest模式,即私链模式
bitcoind -regtest -datadir=/mnt/bitcoin
# 私链持续挖矿
while true; do bitcoin-cli -regtest -datadir=/mnt/bitcoin generate 1; sleep 1; done
curl getblockhash
如果没有配置 rpcuser、 rpcpassword,curl 不会返回数据
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