-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
clientUtils.js
424 lines (369 loc) · 12.6 KB
/
clientUtils.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* 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.
*/
var utils = require('fabric-client/lib/utils.js');
var logger = utils.getLogger('ClientUtils');
var path = require('path');
var fs = require('fs-extra');
var util = require('util');
var Client = require('fabric-client');
var copService = require('fabric-ca-client/lib/FabricCAClientImpl.js');
var User = require('fabric-client/lib/User.js');
var Constants = require('./constants.js');
// all temporary files and directories are created under here
var tempdir = Constants.tempdir;
logger.info(util.format(
'\n\n*******************************************************************************' +
'\n*******************************************************************************' +
'\n* ' +
'\n* Using temp dir: %s' +
'\n* ' +
'\n*******************************************************************************' +
'\n*******************************************************************************\n', tempdir));
module.exports.getTempDir = function() {
fs.ensureDirSync(tempdir);
return tempdir;
};
// directory for file based KeyValueStore
module.exports.KVS = path.join(tempdir, 'hfc-test-kvs');
module.exports.storePathForOrg = function(org) {
return module.exports.KVS + '_' + org;
};
module.exports.cleanupDir = function(keyValStorePath) {
var absPath = path.join(process.cwd(), keyValStorePath);
var exists = module.exports.existsSync(absPath);
if (exists) {
fs.removeSync(absPath);
}
};
module.exports.getUniqueVersion = function(prefix) {
if (!prefix) prefix = 'v';
return prefix + Date.now();
};
// utility function to check if directory or file exists
// uses entire / absolute path from root
module.exports.existsSync = function(absolutePath /*string*/) {
try {
var stat = fs.statSync(absolutePath);
if (stat.isDirectory() || stat.isFile()) {
return true;
} else
return false;
}
catch (e) {
return false;
}
};
module.exports.readFile = readFile;
var ORGS = {};
module.exports.init = function(constants) {
if (constants) {
Constants = constants;
}
Client.addConfigFile(path.join(__dirname, Constants.networkConfig));
ORGS = Client.getConfigSetting(Constants.networkId);
};
var tlsOptions = {
trustedRoots: [],
verify: false
};
function getMember(username, password, client, userOrg) {
var caUrl = ORGS[userOrg].ca.url;
return client.getUserContext(username, true)
.then((user) => {
return new Promise((resolve, reject) => {
if (user && user.isEnrolled()) {
console.log('Successfully loaded member from persistence');
return resolve(user);
}
var member = new User(username);
var cryptoSuite = client.getCryptoSuite();
if (!cryptoSuite) {
cryptoSuite = Client.newCryptoSuite();
if (userOrg) {
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: module.exports.storePathForOrg(ORGS[userOrg].name)}));
client.setCryptoSuite(cryptoSuite);
}
}
member.setCryptoSuite(cryptoSuite);
// need to enroll it with CA server
var cop = new copService(caUrl, tlsOptions, ORGS[userOrg].ca.name, cryptoSuite);
return cop.enroll({
enrollmentID: username,
enrollmentSecret: password
}).then((enrollment) => {
console.log('Successfully enrolled user \'' + username + '\'');
return member.setEnrollment(enrollment.key, enrollment.certificate, ORGS[userOrg].mspid);
}).then(() => {
var skipPersistence = false;
if (!client.getStateStore()) {
skipPersistence = true;
}
return client.setUserContext(member, skipPersistence);
}).then(() => {
return resolve(member);
}).catch((err) => {
throw new Error('Failed to enroll and persist user. Error: ' + err.stack ? err.stack : err);
});
});
})
.catch((err) => {
throw new Error("Unable to get user context for", username);
});
}
function registerAndEnrollUser(client, cop, admin, username, userOrg) {
return new Promise((resolve, reject) => {
console.log('Registering and enrolling user', username);
var enrollUser = new User(username);
// register 'username' CA server
return cop.register({
enrollmentID: username,
role: 'client',
affiliation: 'org1.department1'
}, admin).catch((err) => {
throw err;
}).then((userSecret) => {
console.log('Successfully registered user \'' + username + '\'');
userPassword = userSecret;
// Now that 'username' is registered, try to enroll (login)
return cop.enroll({
enrollmentID: username,
enrollmentSecret: userSecret
});
}).catch((err) => {
console.log('Failed to register user. Error: ' + err.stack ? err.stack : err);
throw err;
}).then((enrollment) => {
console.log('Successfully enrolled user \'' + username + '\'');
return enrollUser.setEnrollment(enrollment.key, enrollment.certificate, ORGS[userOrg].mspid);
}).catch((err) => {
throw err;
}).then(() => {
console.log('Saving enrollment record for user \'' + username + '\'');
return client.setUserContext(enrollUser, false);
}).catch((err) => {
throw err;
}).then(() => {
return client.saveUserToStateStore();
}).then(() => {
console.log('Saved enrollment record for user \'' + username + '\'');
enrollUser._enrollmentSecret = userPassword;
return resolve(enrollUser);
}).catch((err) => {
console.log('Failed to enroll and persist user. Error: ' + err.stack ? err.stack : err);
reject(err);
});
});
}
function getUserMember(adminUser, adminPassword, client, userOrg, username) {
var caUrl = ORGS[userOrg].ca.url;
var userPassword = '';
return client.getUserContext(username, true)
.then((user) => {
return new Promise((resolve, reject) => {
if (user && user.isEnrolled()) {
console.log('Successfully loaded user', username, 'from persistence');
return resolve(user);
}
return client.getUserContext(adminUser, true)
.then((admin) => {
var cryptoSuite = client.getCryptoSuite();
if (!cryptoSuite) {
cryptoSuite = Client.newCryptoSuite();
if (userOrg) {
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: module.exports.storePathForOrg(ORGS[userOrg].name)}));
client.setCryptoSuite(cryptoSuite);
}
}
var cop = new copService(caUrl, tlsOptions, ORGS[userOrg].ca.name, cryptoSuite);
if (admin && admin.isEnrolled()) {
console.log('Successfully loaded admin member from persistence');
return registerAndEnrollUser(client, cop, admin, username, userOrg)
.then((enrollUser) => {
return resolve(enrollUser);
}, (err) => {
reject(err);
});
}
var member = new User(adminUser);
member.setCryptoSuite(cryptoSuite);
// need to enroll admin user with CA server
return cop.enroll({
enrollmentID: adminUser,
enrollmentSecret: adminPassword
}).then((enrollment) => {
console.log('Successfully enrolled admin user');
return member.setEnrollment(enrollment.key, enrollment.certificate, ORGS[userOrg].mspid);
}).then(() => {
var skipPersistence = false;
if (!client.getStateStore()) {
skipPersistence = true;
}
return client.setUserContext(member, skipPersistence);
}).then(() => {
return registerAndEnrollUser(client, cop, member, username, userOrg)
.then((enrollUser) => {
return resolve(enrollUser);
}, (err) => {
reject(err);
});
}).catch((err) => {
console.log('Failed to enroll and persist user. Error: ' + err.stack ? err.stack : err);
throw err;
});
})
.catch((err) => {
console.log("Unable to get user context for", username);
reject(err);
});
});
}).catch((err) => {
console.log('Error loading user context');
throw err;
});
}
function getAdmin(client, userOrg) {
var keyPath = path.join(__dirname, util.format(Constants.networkLocation + '/crypto-config/peerOrganizations/%s.trade.com/users/Admin@%s.trade.com/msp/keystore', userOrg, userOrg));
var keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
var certPath = path.join(__dirname, util.format(Constants.networkLocation + '/crypto-config/peerOrganizations/%s.trade.com/users/Admin@%s.trade.com/msp/signcerts', userOrg, userOrg));
var certPEM = readAllFiles(certPath)[0];
var cryptoSuite = Client.newCryptoSuite();
if (userOrg) {
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: module.exports.storePathForOrg(ORGS[userOrg].name)}));
client.setCryptoSuite(cryptoSuite);
}
return Promise.resolve(client.createUser({
username: 'peer'+userOrg+'Admin',
mspid: ORGS[userOrg].mspid,
cryptoContent: {
privateKeyPEM: keyPEM.toString(),
signedCertPEM: certPEM.toString()
}
}));
}
function getOrdererMSPId() {
Client.addConfigFile(path.join(__dirname, Constants.networkConfig));
return ORGS['orderer'].mspid;
}
function getOrdererAdmin(client) {
var keyPath = path.join(__dirname, Constants.networkLocation + '/crypto-config/ordererOrganizations/trade.com/users/[email protected]/msp/keystore');
var keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
var certPath = path.join(__dirname, Constants.networkLocation + '/crypto-config/ordererOrganizations/trade.com/users/[email protected]/msp/signcerts');
var certPEM = readAllFiles(certPath)[0];
return Promise.resolve(client.createUser({
username: 'ordererAdmin',
mspid: getOrdererMSPId(),
cryptoContent: {
privateKeyPEM: keyPEM.toString(),
signedCertPEM: certPEM.toString()
}
}));
}
function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (!!err)
reject(new Error('Failed to read file ' + path + ' due to error: ' + err));
else
resolve(data);
});
});
}
function readAllFiles(dir) {
var files = fs.readdirSync(dir);
var certs = [];
files.forEach((file_name) => {
let file_path = path.join(dir,file_name);
logger.debug(' looking at file ::'+file_path);
let data = fs.readFileSync(file_path);
certs.push(data);
});
return certs;
}
module.exports.getOrderAdminSubmitter = function(client, test) {
return getOrdererAdmin(client, test);
};
module.exports.getSubmitter = function(client, peerOrgAdmin, org, username) {
if (arguments.length < 1) throw new Error('"client" is a required parameter');
var peerAdmin, userOrg;
if (typeof peerOrgAdmin === 'boolean') {
peerAdmin = peerOrgAdmin;
} else {
peerAdmin = false;
}
// if the 3rd argument was skipped
if (typeof peerOrgAdmin === 'string') {
userOrg = peerOrgAdmin;
} else {
if (typeof org === 'string') {
userOrg = org;
} else {
userOrg = Constants.IMPORTER_ORG;
}
}
if (peerAdmin) {
return getAdmin(client, userOrg);
} else if (username) {
return getUserMember('admin', 'adminpw', client, userOrg, username);
} else {
return getMember('admin', 'adminpw', client, userOrg);
}
};
var eventhubs = [];
module.exports.eventhubs = eventhubs;
function getClientUser(userOrg, username, password) {
if (ORGS[userOrg] === null || ORGS[userOrg] === undefined) {
return new Promise((resolve, reject) => {
return reject('Unknown org: ' + userOrg);
});
}
var orgName = ORGS[userOrg].name;
var client = new Client();
var cryptoSuite = Client.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: module.exports.storePathForOrg(orgName)}));
client.setCryptoSuite(cryptoSuite);
return Client.newDefaultKeyValueStore({
path: module.exports.storePathForOrg(orgName)
}).then((store) => {
if (store) {
client.setStateStore(store);
}
// Hack/shortcut to enroll an admin user
if (username === 'admin') {
if (password !== 'adminpw') {
throw new Error('Invalid admin password');
}
return module.exports.getSubmitter(client, false, userOrg);
} else {
return module.exports.getSubmitter(client, false, userOrg, username);
}
});
}
module.exports.getClientUser = getClientUser;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports.sleep = sleep;
function cleanup() {
for(var key in eventhubs) {
var eventhub = eventhubs[key];
if (eventhub && eventhub.isconnected()) {
logger.debug('Disconnecting the event hub');
eventhub.disconnect();
}
}
eventhubs.splice(0, eventhubs.length); // Clear the array
}
module.exports.txEventsCleanup = cleanup;