Ubuntu下比特幣私有鏈搭建

本文作者:陳進堅
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
聯繫方式:[email protected]

安裝bitcoind

在官網https://bitcoincore.org/bin中找到合適的版本,必須是0.18.0以下的版本,要不然會連接不上節點,新版本的配置有待研究

root@ubuntu:~# wget https://bitcoincore.org/bin/bitcoin-core-0.17.1/bitcoin-0.17.1-x86_64-linux-gnu.tar.gz
root@ubuntu:~# tar zxf bitcoin-0.17.1-x86_64-linux-gnu.tar.gz

創建軟連接

root@ubuntu:~# cd bitcoin-0.17.1
root@ubuntu:~# ln -fs /root/bitcoin-0.17.1/bin/bitcoind /usr/local/bin/bitcoind
root@ubuntu:~# ln -fs /root/bitcoin-0.17.1/bin/bitcoin-cli /usr/local/bin/bitcoin-cli

檢查版本號

root@ubuntu:~# bitcoind --version

配置bitcoin參數

root@ubuntu:~$ mkdir .bitcoin	#創建目錄
root@ubuntu:~$ cd .bitcoin/
root@ubuntu:~$ vi bitcoin.conf

將下面信息全部複製,並修改rpcuser(RPC用戶名),rpcpassword(RPC用戶密碼),rpcallowip(允許訪問的ip地址)然後保存

# Generated by https://jlopp.github.io/bitcoin-core-config-generator/

# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf

# [rpc]
# Accept command line and JSON-RPC commands.
server=1
txindex=1

# Username for JSON-RPC connections
rpcuser=bitcoinrpc

# Password for JSON-RPC connections
rpcpassword=bitcoinrpc

# Listen for JSON-RPC connections on this port
rpcport=18332

# Allow JSON-RPC connections from specified source. Valid for <ip> are a single IP (e.g. 1.2.3.4), 
# a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24). This option 
# can be specified multiple times.
rpcallowip=192.168.1.178
rpcallowip=192.168.1.179

# 設定默認爲私有鏈
regtest=1

啓動bitcoin程序

root@ubuntu:~/.bitcoin$ bitcoind -daemon
Bitcoin server starting

-deamon表示後臺運行

停止bitcoin程序

root@ubuntu:~/.bitcoin/testnet3$ bitcoin-cli stop
Bitcoin server stopping

訪問Json-RPC接口

在主網(mainnet)和測試網絡(testnet)模式下json-rpc端口是使用.bitcoin/bitcoin.conf中配置的18332端口進行訪問,但是在私有鏈(regtest)模式下似乎配置了並沒有作用,還是要訪問默認的18443端口。

我們可以使用postman或者curl等工具進行訪問:bitcoinrpc:bitcoinrpc分別是rpc用戶名和密碼

curl -s -X POST --user bitcoinrpc:bitcoinrpc  -H 'content-type: text/plain;' http://127.0.0.1:18443/   --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getnetworkinfo", "params": [] }'

更多RPC方法可以參考https://www.blockchain.com/es/api/json_rpc_apihttps://bitcoin-rpc.github.io/en/doc/0.17.99/rpc/

bitcoin-cli常用命令

查看錢包信息

該命令可以獲取到錢包版本、餘額、交易數量等信息

bitcoin-cli getwalletinfo

獲取所有錢包地址及其賬號名

bitcoin-cli listreceivedbyaddress 1 true

查詢餘額

bitcoin-cli getbalance

注意:查詢得到的餘額是所有錢包地址的可用餘額總和,並且不包含私鑰不在節點的錢包地址餘額。

生成錢包地址

bitcoin-cli getnewaddress "test"  #"test"是輸入的賬號

查詢地址收到幣的數量

bitcoin-cli getreceivedbyaddress 2MtmeZ7W17zJzigtRhzKMP6MSc2DSyL5LYU

命令列表

bitcoin-cli help
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章