-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.js
executable file
·96 lines (80 loc) · 3.59 KB
/
query.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
/*
* Chaincode query
*/
"use strict";
var Fabric_Client = require("fabric-client");
var path = require("path");
var creds = require("./creds.json");
function y() {}
const obj2 = new y();
obj2.constructor.prototype.queryn = accessChain;
module.exports = obj2;
function accessChain(data) {
return new Promise(resolve => {
console.log(data);
var fabric_client = new Fabric_Client();
// setup the fabric network
var channel = fabric_client.newChannel("defaultchannel");
var peer = fabric_client.newPeer(creds.peers["org1-peer1"].url, {
pem: creds.peers["org1-peer1"].tlsCACerts.pem,
"ssl-target-name-override": null
});
channel.addPeer(peer);
var member_user = null;
var store_path = path.join(__dirname, "hfc-key-store");
console.log("Store path:" + store_path);
// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
return Fabric_Client.newDefaultKeyValueStore({
path: store_path
}).then(state_store => {
// assign the store to the fabric client
fabric_client.setStateStore(state_store);
var crypto_suite = Fabric_Client.newCryptoSuite();
// use the same location for the state store (where the users' certificate are kept)
// and the crypto store (where the users' keys are kept)
var crypto_store = Fabric_Client.newCryptoKeyStore({
path: store_path
});
crypto_suite.setCryptoKeyStore(crypto_store);
fabric_client.setCryptoSuite(crypto_suite);
// get the enrolled user from persistence, this user will sign all requests
return fabric_client.getUserContext("user", true);
}).then(user_from_store => {
if (user_from_store && user_from_store.isEnrolled()) {
console.log("Successfully loaded user1 from persistence");
member_user = user_from_store;
} else {
throw new Error("Failed to get user1.... run registerUser.js");
}
// query chaincode function - requires 1 argument, ex: args: ['[email protected]'],
// you can query by any key that is written on to the ledger
const request = {
//targets : --- letting this default to the peers assigned to the channel
chaincodeId: "cv3",
fcn: "query",
args: [data]
};
// send the query proposal to the peer
return channel.queryByChaincode(request);
}).then(query_responses => {
console.log("Query has completed, checking results");
// query_responses could have more than one results if there multiple peers were used as targets
if (query_responses && query_responses.length == 1) {
if (query_responses[0] instanceof Error) {
console.error("error from query = ", query_responses[0]);
resolve({ success: false, message: query_responses[0] });
} else {
// console.log("Response is ", query_responses[0].toString());
console.log('Q response --- > ' + query_responses[0]);
resolve({ success: true, message: query_responses[0].toString() });
}
} else {
console.log("No payloads were returned from query");
resolve({ success: false, message: "ERROR" });
}
})
.catch(err => {
console.error("Failed to query successfully :: " + err);
});
})
}