由于BTC地址模型采用UTXO
(即在钱包导入目标地址的前提下,节点只保存地址的所有交易记录),所以通过节点无法直接读取地址余额。需要遍历UTXO
列表进行累加计算出余额。
所以开源项目bitcore 诞生,bitcore
是基于nodejs 开发的提供开源的扫链、钱包、区块浏览器等综合服务
此项目依赖BTC节点
+ mongodb
(主要用于保存交易记录等信息,类似扫链数据库)
git clone https://github.com/JKinGH/bitcore.git
3. 环境准备
具体参考此项目Readme.md
3.1 安装 node官网地址,推荐使用v10.16.0
sudo su -
cd /usr/local
wget https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.xz
tar xf node-v10.16.0-linux-x64.tar.xz
mv node-v10.16.0-linux-x64 node
echo 'export PATH=/usr/local/node/bin' :$PATH>> /etc/profile
3.2 安装mongodb(docker运行)
拉取mongodb镜像
docker pull mongo:4.0.14-xenial
mongodb启动脚本如下
#!/bin/bash
#
basedir=/opt/docker-instances/mongodb
sudo docker run -d --name mongodb \
-v $basedir/data:/data/db \
-p 27017:27017 \
mongo:4.0.14-xenial
4. bitcore-node 配置文件
注意:
1. 配置文件必须是json
格式,否则读取配置文件失败
2. 主要读取trustedPeers
内容,即 BTC节点的P2P 地址、端口;RPC暂时没发现作用
3. 配置文件名称必须在bitcore
目录下,并且名称为bitcore.config.json
{
"bitcoreNode": {
"modules": ["./bitcoin"],
"services": {
"api": {
"wallets": {
"allowCreationBeforeCompleteSync": true
}
}
},
"chains": {
"BTC": {
"regtest": {
"chainSource": "p2p",
"trustedPeers": [
{
"host": "127.0.0.1",
"port": 18444
}
],
"rpc": {
"host": "127.0.0.1",
"port": 18443,
"username": "xxx",
"password": "admin@123"
}
}
}
}
}
}
4.1 testnet (测试链、默认P2P/RPC端口 18333/18332)
{
"bitcoreNode": {
"modules": ["./bitcoin"],
"services": {
"api": {
"wallets": {
"allowCreationBeforeCompleteSync": true
}
}
},
"chains": {
"BTC": {
"testnet": {
"chainSource": "p2p",
"trustedPeers": [
{
"host": "127.0.0.1",
"port": 18333
}
],
"rpc": {
"host": "127.0.0.1",
"port": 18332,
"username": "xxx",
"password": "admin@123"
}
}
}
}
}
}
5. 启动 bitcore-node 服务(类似区块浏览器API),默认端口 3000
API文档
cd bitcore/
npm run node //前台运行
可查看启动的相关信息
devops@hn-baas:~/bitcore$ npm run node
> @8.13.0 node /home/devops/bitcore
> cd packages/bitcore-node && npm start
> bitcore-node@8.13.0 start /home/devops/bitcore/packages/bitcore-node
> npm run tsc && node build/src/server.js
> bitcore-node@8.13.0 tsc /home/devops/bitcore/packages/bitcore-node
> npm run clean && tsc
> bitcore-node@8.13.0 clean /home/devops/bitcore/packages/bitcore-node
> rm -rf build
{"message":"Starting Event Service","level":"info"}
{"message":"Starting P2P Manager","level":"info"}
{"message":"Starting BTC p2p worker","level":"info"}
(node:2790) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
{"message":"2019-12-30 10:30:05.575 GMT+8 | Connected to peer: 127.0.0.1:18444 | Chain: BTC | Network: regtest","level":"info"}
{"message":"This worker is now the syncing node for BTC regtest","level":"info"}
{"message":"2019-12-30 10:30:06.143 GMT+8 | Sync completed | Chain: BTC | Network: regtest","level":"info"}
{"message":"Starting Event Service","level":"info"}
{"message":"Starting API Service on port 3000","level":"info"}
{"message":"Starting Socket Service","level":"info"}
{"message":"Started Socket Service","level":"info"}
{"message":"Starting Event Service","level":"info"}
{"message":"Starting API Service on port 3000","level":"info"}
{"message":"Starting Socket Service","level":"info"}
{"message":"Starting Event Service","level":"info"}
{"message":"Starting API Service on port 3000","level":"info"}
{"message":"Starting Socket Service","level":"info"}
{"message":"Started Socket Service","level":"info"}
{"message":"Started Socket Service","level":"info"}
{"message":"Starting Event Service","level":"info"}
{"message":"Starting API Service on port 3000","level":"info"}
{"message":"Starting Socket Service","level":"info"}
{"message":"Started Socket Service","level":"info"}
查询交易相关的UTXO
devops@hn-baas:~$ curl localhost:3000/api/BTC/regtest/tx/3b8bb3a8564925ae1c858854d7e1a7dd8fe4f5c6dbebe4cf77288d63b5af980a/coins -s | json
{
"inputs": [],
"outputs": [
{
"_id": "5e06f8575a489b45f4ea8f31",
"chain": "BTC",
"network": "regtest",
"coinbase": true,
"mintIndex": 0,
"spentTxid": "",
"mintTxid": "3b8bb3a8564925ae1c858854d7e1a7dd8fe4f5c6dbebe4cf77288d63b5af980a",
"mintHeight": 999,
"spentHeight": -2,
"address": "2N5KUtRVv34YZzhNrHBfScPVkqxvN9FmTCq",
"script": "a914846f9c3ac62f72cf3a91238d1e55b35fec0e06c087",
"value": 78125000,
"confirmations": -1
},
{
"_id": "5e06f8575a489b45f4ea8f33",
"chain": "BTC",
"network": "regtest",
"coinbase": true,
"mintIndex": 1,
"spentTxid": "",
"mintTxid": "3b8bb3a8564925ae1c858854d7e1a7dd8fe4f5c6dbebe4cf77288d63b5af980a",
"mintHeight": 999,
"spentHeight": -2,
"address": "false",
"script": "6a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9",
"value": 0,
"confirmations": -1
}
]
}
或者
cd bitcore/
nohup npm run node & //后台运行
6. 启动insight 服务(浏览器界面) 默认端口 8200
cd bitcore/
npm run node //前台运行
cd bitcore/
nohup npm run node & //后台运行
6.1 在Chrome中打开区块浏览器页面