简介
Hyperledger fabric Client (HFC)提供了基于Node.js的应用接口来访问Hyperledger区块. 本文介绍了一个使用HFC访问IBM Bluemixr区块服务的Hello World示例程序的开发过程.
第一步 安装HFC
在工作目录下运行如下npm
命令从安装HFC, 由于目前IBM Bluemixr提供的Hyperledger区块服务依旧是0.5版本, 对应的HFC版本则是0.5.x。
npm install [email protected]
第二步 从以下链接下载IBM Bluemixr的CA证书文件 (us.blockchain.ibm.com.cert)
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>
- 安装HFC时报错: MSBUILD error MSB3428: Could not load the Visual C++ component "VCBuild.exe"
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 2015
然后运行以下命令
npm config set msvs_version 2015 --global
再次安装HFC
- 安装HFC时报错:
win_delay_load_hook.c(31): error C2373: '__pfnDliNotifyHook2': redefinition; different type modifiers
运行以下命令后再次安装HFC
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.
添加如下代码
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服务
总结
本文介绍了一个基于Hyperledger fabric Client(HFC)的Hello World示例程序。