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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章