首先,在閱讀本文以前假設您已經了解比特幣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
配置
bitcoind
打開bitcoin的配置文件bitcoin.conf, Linux下這件文件在"~/.bitcoin", 打開編輯,設置txindex為true
配置接入IPaccept incoming connections using 'rpcallowip'
Insight API
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
有返回insight api則正常。
同步
可以手工同步歷史的blockchain數據:
util/sync.js
開發
在本地運行Grunt
$ NODE_ENV=development grunt
運行測試
$ grunt test
API
基本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"]}
Web Socket API
基於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服務端應用示例之一。 后續有空繼續介紹其相關架構設計與開發事項。
希望對您軟件開發有幫助。您可能感興趣的文章:
比特幣Bitcoin源代碼安裝編譯
作者:Petter Liu
出處:http://www.cnblogs.com/wintersun/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
該文章也同時發布在我的獨立博客中-Petter Liu Blog。