forked from HyperledgerHandsOn/trade-finance-logistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-chaincode.js
122 lines (107 loc) · 3.68 KB
/
query-chaincode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright 2018 IBM All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var path = require('path');
var fs = require('fs');
var Constants = require('./constants.js');
var Client = require('fabric-client');
var ClientUtils = require('./clientUtils.js');
//
// Send chaincode query request to the peer
//
function queryChaincode(userOrg, version, funcName, argList, userName, constants) {
if (constants) {
Constants = constants;
}
ClientUtils.init(Constants);
var ORGS = JSON.parse(fs.readFileSync(path.join(__dirname, Constants.networkConfig)))[Constants.networkId];
Client.setConfigSetting('request-timeout', 60000);
var channel_name = Client.getConfigSetting('E2E_CONFIGTX_CHANNEL_NAME', Constants.CHANNEL_NAME);
// this is a transaction, will just use org's identity to
// submit the request. intentionally we are using a different org
// than the one that submitted the "move" transaction, although either org
// should work properly
var client = new Client();
var channel = client.newChannel(channel_name);
var orgName = ORGS[userOrg].name;
var cryptoSuite = Client.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: ClientUtils.storePathForOrg(orgName)}));
client.setCryptoSuite(cryptoSuite);
var targets = [];
// set up the channel to use each org's 'peer1' for
// both requests and events
for (let key in ORGS) {
if (ORGS.hasOwnProperty(key) && typeof ORGS[key].peer1 !== 'undefined') {
let data = fs.readFileSync(path.join(__dirname, ORGS[key].peer1['tls_cacerts']));
let peer = client.newPeer(
ORGS[key].peer1.requests,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[key].peer1['server-hostname']
});
channel.addPeer(peer);
}
}
return Client.newDefaultKeyValueStore({
path: ClientUtils.storePathForOrg(orgName)
}).then((store) => {
client.setStateStore(store);
return ClientUtils.getSubmitter(client, false, userOrg, userName);
}).then((user) => {
if (userName) {
console.log('Successfully enrolled user', userName);
} else {
console.log('Successfully enrolled user \'admin\'');
}
// send query
var request = {
chaincodeId : Constants.CHAINCODE_ID,
fcn: funcName,
args: argList
};
return channel.queryByChaincode(request);
},
(err) => {
var errMesg = 'Failed to get submitter ';
if (userName) {
errMesg = errMesg + userName + '. Error: ' + err;
} else {
errMesg = errMesg + 'admin. Error: ' + err;
}
console.log(errMesg);
throw new Error(errMesg);
}).then((response_payloads) => {
if (response_payloads) {
var value = '';
for(let i = 0; i < response_payloads.length; i++) {
if(value === '') {
value = response_payloads[i].toString('utf8');
} else if(value !== response_payloads[i].toString('utf8')) {
throw new Error('Responses from peers don\'t match');
}
}
return value;
} else {
console.log('response_payloads is null');
throw new Error('Failed to get response on query');
}
},
(err) => {
console.log('Failed to send query due to error: ' + err.stack ? err.stack : err);
throw new Error('Failed, got error on query');
});
};
module.exports.queryChaincode = queryChaincode;