Недавно я написал некоторый код для использования с приложением Hyperledger-Composer (v0.13), которое отслеживает события чейнкода, чтобы информация о блоках могла отображаться по мере их создания. Этот код является частью нового руководства по блокчейну Zero To Blockhain. Код находится в файле queryBlockchain.js в главе 5 и последующих. Структура папок для поиска этого файла:
Chapter 05
↳ controller
↳restapi
router.js
↳features
↳composer
autoLoad.js
hlcAdmin.js
queryBlockChain.js <=== You want this file for blockchain events
Z2B_Services.js <=== and this file for socket communications with the browser
Z2B_Utilities.js
↳creds
и это использует учетные данные, предоставленные установкой композитора, которые хранятся в папке Creds.
Что касается комментария Джонатана, фактический код, указанный выше, обращается к любой бизнес-сети, а не только к той, к которой подключен компоновщик гиперледжера. На уровне структуры вам потребуется доступ к действительному хранилищу ключей, имени канала, URL-адресу однорангового запроса и URL-адресу ордера для вашей целевой сети. Код, на который ссылается код, выглядит следующим образом:
var channel = {};
var client = null;
var wallet_path = path.join(__dirname, 'creds');
Promise.resolve().then(() => {
//
// As of 9/28/2017 there is a known and unresolved bug in HyperLedger Fabric
// https://github.com/hyperledger/composer/issues/957
// this requires that the file location for the wallet for Fabric version 1.0 be in the following location:
// {HOME}/.hfc-key-store
// therefore the wallet location will be ignored and any private keys required to enroll a user in this process
// must be located in {HOME}/.hfc-key-store
// this is currently managed for you in the installation exec by copying the private key for PeerAdmin to this location
//
console.log("Create a client and set the wallet location");
// hfc = require('fabric-client');
client = new hfc();
return hfc.newDefaultKeyValueStore({ path: wallet_path })
.then((wallet) => {
client.setStateStore(wallet);
// const config = require('../../../env.json');
// which contains the following and is set in this
// example to support the hyperledger fabric dev environment:
// {
// "composer":
// {
// "connectionProfile": "hlfv1",
// "network": "zerotoblockchain-network",
// "adminID": "admin",
// "adminPW": "adminpw",
// "PeerAdmin": "PeerAdmin",
// "PeerPW": "randomString",
// "NS": "org.acme.Z2BTestNetwork"
// },
// "fabric":
// {
// "user": "queryUser",
// "eventURL": "grpc://localhost:7053",
// "channelName": "composerchannel",
// "keyValStore": ".composer-credentials/PeerAdmin",
// "wallet_store": "creds",
// "peer": "peer0.org1.example.com",
// "peerRequestURL": "grpc://localhost:7051",
// "peerEventURL": "grpc://localhost:7053",
// "ordererURL" : "grpc://localhost:7050",
// "caURL": "http://localhost:7054"
// }
// }
//
return client.getUserContext(config.composer.PeerAdmin, true);})
.then((user) => {
if (user === null || user === undefined || user.isEnrolled() === false)
{console.error("User not defined, or not enrolled - error");}
channel = client.newChannel(config.fabric.channelName);
channel.addPeer(client.newPeer(config.fabric.peerRequestURL));
channel.addOrderer(client.newOrderer(config.fabric.ordererURL));
})
.then(() => {
return channel.queryInfo()
.then((blockchainInfo) => {
if (blockchainInfo) {
res.send({"result": "success", "currentHash": blockchainInfo.currentBlockHash.toString("hex"), blockchain: blockchainInfo});
} else {
console.log('response_payload is null');
res.send({"result": "uncertain", "message": 'response_payload is null'});
}
})
.catch((_err) => {
console.log("queryInfo failed with _err = ", _err);
res.send({"result": "failed", "message": _err.message});
});
});
});
}
person
Bob Dill
schedule
26.10.2017