Hyperledger fabric Client (HFC)提供了基於Node.js的應用接口來訪問Hyperledger區塊. 本文介紹了一個使用HFC訪問IBM Bluemixr區塊服務的Hello World示例程序的開發過程.node
在工做目錄下運行以下npm
命令從安裝HFC, 因爲目前IBM Bluemixr提供的Hyperledger區塊服務依舊是0.5版本, 對應的HFC版本則是0.5.x。git
npm install hfc@0.5.x
https://blockchain-certs.mybluemix.net/
var hfc = require('hfc'); var fs = require('fs'); var PEER_ADDRESS = "grpcs://e1bb641577f249a88acca04da0272249-vp3.us.blockchain.ibm.com:30304"; var MEMBERSRVC_ADDRESS = "grpcs://e1bb641577f249a88acca04da0272249-ca.us.blockchain.ibm.com:30304"; var ENROLL_USER_NAME="WebAppAdmin"; var ENROLL_USER_PASSWORD="4258cc884f"; var CHAIN_CODE_ID="e2623e76371a16ca86d97bfb90bdab95885939bf4f2fa40133d65bca4aace27eaf94d8cd10438b80de097442916f6bfef87fb45a6cd678e3cb6ec0bd8dd008c2"; console.log(" **** starting HFC sample ****"); var pem = fs.readFileSync('us.blockchain.ibm.com.cert'); var chain = hfc.newChain("testChain"); var keyValStorePath = "/tmp/hfc_keyValStore"; chain.setDevMode(false); chain.setECDSAModeForGRPC(true); chain.setKeyValStore(hfc.newFileKeyValStore(keyValStorePath)); chain.setMemberServicesUrl(MEMBERSRVC_ADDRESS, {pem:pem}); chain.addPeer(PEER_ADDRESS, {pem:pem}); chain.enroll(ENROLL_USER_NAME, ENROLL_USER_PASSWORD, function (err, user) { if (err) { console.log("ERROR: failed to register user: %s",err); process.exit(1); } console.log("**** Enrolled ****"); console.log("querying chaincode ..."); var queryRequest = { chaincodeID: CHAIN_CODE_ID, fcn: "query", args: [""] }; // Issue the query request and listen for events var tx = user.query(queryRequest); tx.on('complete', function (results) { console.log("query completed successfully; results=%j",results); console.log(results.result.toString()); process.exit(0); }); tx.on('error', function (error) { console.log("Failed to query chaincode: request=%j, error=%k",queryRequest,error); process.exit(1); }); });
C:\work\HFC>node test.js **** starting HFC sample **** **** Enrolled **** querying chaincode ... query completed successfully; results={"result":{"type":"Buffer","data":[72,101,108,108,111,32,87,111,114,108,100]}} Hello World
npm
安裝HFC會調用git
命令從github
下載相關源文件,若是使用代理服務器的話,使用如下命令配置git
。git config --global http.proxy <proxy server host>:<proxy server port>
MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere.
首先安裝Visual C++ Build Tools 2015github
而後運行如下命令npm
npm config set msvs_version 2015 --global
再次安裝HFC服務器
win_delay_load_hook.c(31): error C2373: '__pfnDliNotifyHook2': redefinition; different type modifiers
運行如下命令後再次安裝HFCui
npm -g install npm@next
E1105 15:46:35.785000000 6360 handshake.c:215] Read failed. E1105 15:46:35.785000000 6360 secure_channel_create.c:102] Secure handshake failed with error 1.
添加以下代碼this
chain.setECDSAModeForGRPC(true);
hfc [MemberServicesImpl.enroll] failed to create cert pair: err={"code":12,"metadata":{"_internal_repr":{}}} +992ms hfc [memberServices.enroll] err=Error: unknown service protos.ECAP, enrollment=undefined +1ms ERROR: failed to register admin: Error: unknown service protos.ECAP
檢查確認MEMBERSRVC_ADDRESS指向CA服務.net
本文介紹了一個基於Hyperledger fabric Client(HFC)的Hello World示例程序。3d