导读 | 首先,在阅读本文以前假设您已经了解比特币Bitcoin基本原理。 Insight API是一个开源基于比特币Bitcoin blockchain的REST风格的API框架。Insight API运行于NodeJS环境,使用LevelDB做数据存储。使用Insight API可以开发基于Bitcoin的应用程序,如获取blockchain信息的钱包。这里我们介绍此刻最新版本2.7,它还有一个前端项目Insight。生产环境运行在这儿。 |
以Ubuntu为环境。Windows环境问题较多,我们不介绍。它的依赖项目与组件:
第一步:bitcoind 下载安装Bitcoin
Node.js v.010.x Node.js
NPM 安装node时应该就有了
第二步:获取insight-api
$ git clone https://github.com/bitpay/insight-api && cd insight-api
安装依赖组件,从package.json中我们可以看到依赖组件:
"bitcore": "git://github.com/bitpay/bitcore.git#4d8af75ae9916984c52ee2eda1870d5980656341", "base58-native": "0.1.2", "async": "*", "leveldown": "*", "levelup": "*", "glob": "*", "soop": "=0.1.5", "commander": "*", "bignum": "*", "winston": "*", "express": "~3.4.7", "buffertools": "*", "should": "~2.1.1", "socket.io": "~1.0.4", "moment": "~2.5.0", "sinon": "~1.7.3", "xmlhttprequest": "~1.6.0", "bufferput": "git://github.com/bitpay/node-bufferput.git"
执行npm install,即可自动安装它们:
$ npm install
注意这一步可以一次不会成功,有时需要换NPM的镜像。
$ npm config set registry http://registry.cnpmjs.org
打开bitcoin的配置文件bitcoin.conf, Linux下这件文件在”~/.bitcoin”, 打开编辑,设置txindex为true
配置接入IPaccept incoming connections using ‘rpcallowip’
Insight api的配置文件config.js在config文件夹中。有一些环境变量定义,如下:
BITCOIND_HOST # RPC bitcoind host BITCOIND_PORT # RPC bitcoind Port BITCOIND_P2P_PORT # P2P bitcoind Port BITCOIND_USER # RPC username BITCOIND_PASS # RPC password BITCOIND_DATADIR # bitcoind datadir. 'testnet3' will be appended automatically if testnet is used. NEED to finish with '/'. e.g: `/vol/data/` INSIGHT_NETWORK [= 'livenet' | 'testnet'] INSIGHT_DB # Path where to store insight's internal DB. (defaults to $HOME/.insight) INSIGHT_SAFE_CONFIRMATIONS=6 # Nr. of confirmation needed to start caching transaction information INSIGHT_IGNORE_CACHE # True to ignore cache of spents in transaction, with more than INSIGHT_SAFE_CONFIRMATIONS confirmations. This is useful for tracking double spents for old transactions.
bitcoind必须先运行,并且下载blockchain完成,执行
$ node insight.js
这时默认应该在3001端口,打开浏览器访问以下URL
http://localhost:3001
有返回insight api则正常。
可以手工同步历史的blockchain数据:
util/sync.js
在本地运行Grunt
$ NODE_ENV=development grunt
$ grunt test
基本REST的API调用是这样的:
Block /api/block/[:hash] /api/block/00000000a967199a2fad0877433c93df785a8d8ce062e5f9b451cd1397bdbf62 Transaction /api/tx/[:txid] /api/tx/525de308971eabd941b139f46c7198b5af9479325c2395db7f2fb5ae8562556c Address /api/addr/[:addr][?noTxList=1&noCache=1] /api/addr/mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5?noTxList=1
示例获取特定的地址
http://192.168.1.18:3001/api/addr/1JqA2ZZpNzYrSTVk828fjztWWwRWQxRoYm
返回JSON字符串
{"addrStr":"1JqA2ZZpNzYrSTVk828fjztWWwRWQxRoYm","balance":0.02,"balanceSat":2000000,"totalReceived":29.02 ,"totalReceivedSat":2902000000,"totalSent":29,"totalSentSat":2900000000,"unconfirmedBalance":0,"unconfirmedBalanceSat":0 ,"unconfirmedTxApperances":0,"txApperances":9,"transactions":["5f9bbe4fa99967a7d2e1e00645c4f31ac18ebf18fa6ffe96a3959c25416b4b08" ,"f6ceaf9edac817d57ad8500ef4f6ff3dce5445b8c4d51f1ef6e5967b80d6f5ce","2f5b0fc3fb46a89f94902fdd695e2ff0d7c12b1dbf4f6bab8b9bcf269a1310b2" ,"7b0924dacaf4b99d0be959b3a98a73904480d0c6af8e561e15cfe0bb149fe7df","691c35ea2a00155aeb8fb3086e434c958941c24b54efc656b6c16a5c7084bfb6" ,"983d3bc5c374ae2ccccf3702f4d0d6648dca295443763c143bbedaf9b9471ae4","07e092af991c72d8ba01fa7c0723635e6708e18c2f569acc6d362f8ee0647845" ,"ff9bd3221259bfaf7a1617ee3d95c5dd1676192111c526a5184661d22dbed7be","c5232b1ea17c905a900066c6d19d7a444a4ac6ca1acd7aa38eb516a6c8bebedd"]}
基于socket.io实现了web socket api,如下
/socket.io/1/
RESTful风格的架构最近几年比较流行,Insight-API就是一个基于Node.js平台的REST WEB API项目.Insight-API封装了BitCore实现WEB API,BitCore是基于JSON PRC的API,它服务于Bitcoind.Insight-API也是node.js服务端应用示例之一。 后续有空继续介绍其相关架构设计与开发事项。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/124728.html