[de9008]: / web-app / server / src / fabric / network.js

Download this file

319 lines (241 with data), 11.7 kB

  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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//Import Hyperledger Fabric 1.4 programming model - fabric-network
'use strict';
const { FileSystemWallet, Gateway, X509WalletMixin } = require('fabric-network');
const path = require('path');
const fs = require('fs');
//connect to the config file
const configPath = path.join(process.cwd(), './config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configJSON);
let connection_file = config.connection_file;
// let userName = config.userName;
let gatewayDiscovery = config.gatewayDiscovery;
let appAdmin_pat = config.appAdmin_pat;
let appAdmin_doc = config.appAdmin_doc;
let orgMSPID_pat = config.orgMSPID_pat;
let orgMSPID_doc = config.orgMSPID_doc;
// connect to the connection file
const ccpPath = path.join(process.cwd(), connection_file);
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
const util = require('util');
exports.connectToNetwork = async function (userName) {
const gateway = new Gateway();
try {
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
console.log('userName: ');
console.log(userName);
console.log('wallet: ');
console.log(util.inspect(wallet));
console.log('ccp: ');
console.log(util.inspect(ccp));
// userName = 'V123412';
const userExists = await wallet.exists(userName);
if (!userExists) {
console.log('An identity for the user ' + userName + ' does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
let response = {};
response.error = 'An identity for the user ' + userName + ' does not exist in the wallet. Register ' + userName + ' first';
return response;
}
console.log('before gateway.connect: ');
await gateway.connect(ccp, { wallet, identity: userName, discovery: gatewayDiscovery });
// Connect to our local fabric
const network = await gateway.getNetwork('mychannel');
console.log('Connected to mychannel. ');
// Get the contract we have installed on the peer
const contract = await network.getContract('contract');
let networkObj = {
contract: contract,
network: network,
gateway: gateway
};
return networkObj;
} catch (error) {
console.log(`Error processing transaction. ${error}`);
console.log(error.stack);
let response = {};
response.error = error;
return response;
} finally {
console.log('Done connecting to network.');
// gateway.disconnect();
}
};
//Client application part for calling/invoking any smart contract function(query etc)
exports.invoke = async function (networkObj, isQuery, func, args) {
try {
console.log('inside invoke');
console.log(`isQuery: ${isQuery}, func: ${func}, args: ${args}`);
console.log(util.inspect(networkObj));
// console.log(util.inspect(JSON.parse(args[0])));
if (isQuery === true) {
console.log('inside isQuery');
if (args) {
console.log('inside isQuery, args');
args = JSON.parse(args[0]);
console.log(util.inspect(args));
args = JSON.stringify(args);
console.log(util.inspect(args));
console.log(args);
let response = await networkObj.contract.evaluateTransaction(func, args);
console.log(response);
console.log(`Transaction ${func} with args ${args} has been evaluated`);
await networkObj.gateway.disconnect();
return response;
} else {
let response = await networkObj.contract.evaluateTransaction(func);
console.log(response);
console.log(`Transaction ${func} without args has been evaluated`);
await networkObj.gateway.disconnect();
return response;
}
} else {
console.log('notQuery');
if (args) {
console.log('notQuery, args');
console.log('$$$$$$$$$$$$$ args: ');
console.log(args);
console.log(func);
console.log(typeof args);
args = JSON.parse(args[0]);
console.log(util.inspect(args));
args = JSON.stringify(args);
console.log(util.inspect(args));
console.log('before submit');
console.log(util.inspect(networkObj));
let response = await networkObj.contract.submitTransaction(func, args);
console.log('after submit');
console.log(response);
console.log(`Transaction ${func} with args ${args} has been submitted`);
await networkObj.gateway.disconnect();
return response;
} else {
let response = await networkObj.contract.submitTransaction(func);
console.log(response);
console.log(`Transaction ${func} with args has been submitted`);
await networkObj.gateway.disconnect();
return response;
}
}
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
return error;
}
};
//Client application part for registering a new Patient
exports.registerPatient = async function (patientId, adharNo, name, age, phNo) {
console.log('adharNo');
console.log(adharNo);
console.log('name');
console.log(name);
if (!adharNo || !name || !age || !phNo) {
let response = {};
response.error = 'Error! You need to fill all fields before you can register!';
return response;
}
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
console.log(wallet);
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(patientId);
if (userExists) {
let response = {};
console.log(`An identity for the patient with patientId ${patientId} already exists in the wallet`);
response.error = `Error! An identity for the patient with patientId ${patientId} already exists in the wallet.`;
return response;
}
// Check to see if we've already enrolled the admin user.
const adminExists = await wallet.exists(appAdmin_pat);
if (!adminExists) {
console.log(`An identity for the admin user ${appAdmin_pat} does not exist in the wallet`);
console.log('Run the enrollAdmin.js application before retrying');
let response = {};
response.error = `An identity for the admin user ${appAdmin_pat} does not exist in the wallet.
Run the enrollAdmin.js application before retrying`;
return response;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: appAdmin_pat, discovery: gatewayDiscovery });
// Get the CA client object from the gateway for interacting with the CA.
const ca = gateway.getClient().getCertificateAuthority();
const adminIdentity = gateway.getCurrentIdentity();
console.log(`AdminIdentity: + ${adminIdentity}`);
// Register the user, enroll the user, and import the new identity into the wallet.
const secret = await ca.register({ affiliation: '', enrollmentID: patientId, role: 'client' }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: patientId, enrollmentSecret: secret });
const userIdentity = await X509WalletMixin.createIdentity(orgMSPID_pat, enrollment.certificate, enrollment.key.toBytes());
await wallet.import(patientId, userIdentity);
console.log(`Successfully registered Patient ${name} . Use patientId ${patientId} and password: secret99 to login above.`); //password is static and set to secret99 for patients
let response = `Successfully registered Patient ${name} . Use patientId ${patientId} and password: secret99 to login above.`;
return response;
} catch (error) {
console.error(`Failed to register patient + ${adharNo} + : ${error}`);
let response = {};
response.error = error;
return response;
}
};
//Client application part for registering a new Doctor
exports.registerDoctor = async function (doctorId, licenseId, name, age, phNo) {
console.log('LicenseId');
console.log(licenseId);
console.log('name');
console.log(name);
if (!licenseId || !name || !age || !phNo) {
let response = {};
response.error = 'Error! You need to fill all fields before you can register!';
return response;
}
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
console.log(wallet);
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(doctorId);
if (userExists) {
let response = {};
console.log(`An identity for the user ${doctorId} already exists in the wallet`);
response.error = `Error! An identity for the user ${doctorId} already exists in the wallet. Please enter a different license number.`;
return response;
}
// Check to see if we've already enrolled the admin user.
const adminExists = await wallet.exists(appAdmin_pat);
if (!adminExists) {
console.log(`An identity for the admin user ${appAdmin_pat} does not exist in the wallet`);
console.log('Run the enrollAdmin.js application before retrying');
let response = {};
response.error = `An identity for the admin user ${appAdmin_pat} does not exist in the wallet.
Run the enrollAdmin.js application before retrying`;
return response;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: appAdmin_pat, discovery: gatewayDiscovery });
// Get the CA client object from the gateway for interacting with the CA.
const ca = gateway.getClient().getCertificateAuthority();
const adminIdentity = gateway.getCurrentIdentity();
console.log(`AdminIdentity: + ${adminIdentity}`);
// Register the user, enroll the user, and import the new identity into the wallet.
const secret = await ca.register({ affiliation: '', enrollmentID: doctorId, role: 'client' }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: doctorId, enrollmentSecret: secret });
const userIdentity = await X509WalletMixin.createIdentity(orgMSPID_doc, enrollment.certificate, enrollment.key.toBytes());
await wallet.import(doctorId, userIdentity);
console.log(`Successfully registered Doctor ${name} . Use DoctorId ${doctorId} and password: doctor99 to login above.`); //password is static and set to doctor99 for doctors
let response = `Successfully registered Doctor ${name} . Use DoctorId ${doctorId} and password: doctor99 to login above.`;
return response;
} catch (error) {
console.error(`Failed to register doctor + ${licenseId} + : ${error}`);
let response = {};
response.error = error;
return response;
}
};