-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
executable file
·570 lines (493 loc) · 19.4 KB
/
app.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/**
* Copyright 2017 IBM All Rights Reserved.
* Modifications by Krishna
*
* 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 express = require('express');
// var session = require('express-session');
// var cookieParser = require('cookie-parser');
var expressJWT = require('express-jwt');
var jwt = require('jsonwebtoken');
var bearerToken = require('express-bearer-token');
var bodyParser = require('body-parser');
var http = require('http');
var cors = require('cors');
var app = express();
require('./config.js');
var hfc = require('fabric-client');
// --- START APP HELPER FILES
var helper = require('./app/helper.js');
var createChannel = require('./app/create-channel.js');
var join = require('./app/join-channel.js');
var install = require('./app/install-chaincode.js');
var instantiate = require('./app/instantiate-chaincode.js');
var invoke = require('./app/invoke-transaction.js');
var query = require('./app/query.js');
// --- END APP HELPER FILES
var logger = helper.getLogger('DemoApp');
var host = process.env.HOST || hfc.getConfigSetting('host');
var port = process.env.PORT || hfc.getConfigSetting('port');
var orgs = ['Org1', 'Org2', 'Org3'];
//support parsing of application/json type post data
app.options('*', cors());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
// set secret variable
app.set('secret', 'thisismysecret');
app.use(expressJWT({
secret: 'thisismysecret'
}).unless({
path: ['/users','/orgs']
}));
// resolve tokens
app.use(bearerToken());
app.use(function(req, res, next) {
res.setHeader('Content-Type', 'application/json');
logger.debug(' ------>>>>>> new request for %s',req.originalUrl);
if (req.originalUrl.indexOf('/users') >= 0 || req.originalUrl.indexOf('/orgs') >= 0) {
return next();
}
var token = req.token;
jwt.verify(token, app.get('secret'), function(err, decoded) {
if (err) {
res.json({
success: false,
message: 'Failed to authenticate token. Make sure to include the ' +
'token returned from /users call in the authorization header ' +
' as a Bearer token'
});
return;
} else {
// add the decoded user name and org name to the request object
req.body.username = decoded.username;
req.body.orgname = decoded.orgname;
logger.debug('Decoded from JWT token: username - ' + decoded.username + ', orgname - ' + decoded.orgname);
return next();
}
});
});
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// START SERVER /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
var server = http.createServer(app).listen(port, function() {});
logger.info('****************** SERVER STARTED ************************');
logger.info('*************** http://%s:%s ******************',host,port);
server.timeout = 240000;
///////////////////////////////////////////////////////////////////////////////
///////////////////////// REST ENDPOINTS START HERE ///////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Register and enroll user
app.post('/users', async function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< U S E R L O G I N >>>>>>>>>>>>>>>>>');
logger.debug('End point : /users');
// validate the request
let fields = Object.assign(req.body);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Generate a web token
var token = jwt.sign({
exp: Math.floor(Date.now() / 1000) + parseInt(hfc.getConfigSetting('jwt_expiretime')),
username: fields.username,
orgname: fields.orgname,
role: fields.role
}, app.get('secret'));
// Get the registered user (If user is unregistered, register him)
let response = await helper.getRegisteredUser(fields.username, fields.orgname, true);
logger.debug('-- returned from registering the username %s for organization %s', fields.username, fields.orgname);
if (response && typeof response !== 'string' && response.success) {
response.token = token;
logger.debug('Successfully registered a user: %s', JSON.stringify(response));
}
res.json(response);
});
// Create Channel
app.post('/channels', async function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< C R E A T E C H A N N E L >>>>>>>>>>>>>>>>>');
logger.debug('End point : /channels');
// validate the request
let fields = Object.assign(req.body);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname',
'channelName', 'channelConfigPath']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Create a channel
let message = await createChannel.createChannel(fields.channelName, fields.channelConfigPath,
fields.username, fields.orgname);
res.json(message);
});
// Join Channel
app.post('/channels/:channelName/peers', async function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< J O I N C H A N N E L >>>>>>>>>>>>>>>>>');
logger.debug('End point : /channels/:channelName/peers');
// validate the request
let fields = Object.assign(req.body, req.params);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname', 'channelName', 'peers']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Join a channel
let message = await join.joinChannel(fields.channelName, fields.peers, fields.username, fields.orgname);
res.json(message);
});
// Install chaincode on target peers
app.post('/chaincodes', async function(req, res) {
logger.info('==================== INSTALL CHAINCODE ==================');
logger.debug('End point : /chaincodes');
// validate the request
let fields = Object.assign(req.body);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname', 'peers',
'chaincodeName', 'chaincodePath', 'chaincodeVersion', 'chaincodeType']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Install the chaincode
let message = await install.installChaincode(fields.peers, fields.chaincodeName, fields.chaincodePath,
fields.chaincodeVersion, fields.chaincodeType, fields.username, fields.orgname);
res.json(message);
});
// Instantiate chaincode on target peers
app.post('/channels/:channelName/chaincodes', async function(req, res) {
logger.info('==================== INSTANTIATE CHAINCODE ==================');
logger.debug('End point : /channels/:channelName/chaincodes');
// validate the request
let fields = Object.assign(req.body, req.params);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname',
'chaincodeName', 'chaincodeVersion', 'chaincodeType', 'channelName', 'args']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Optional request variables
logger.debug('peers'.padEnd(12) + ': ' + fields['peers']);
logger.debug('fcn'.padEnd(12) + ': ' + fields['fcn']);
let message = await instantiate.instantiateChaincode(fields.peers, fields.channelName, fields.chaincodeName,
fields.chaincodeVersion, fields.chaincodeType, fields.fcn, fields.args, fields.username, fields.orgname);
res.json(message);
});
// Invoke transaction on chaincode on target peers
app.post('/channels/:channelName/chaincodes/:chaincodeName/:function', async function(req, res) {
logger.debug('==================== INVOKE ON CHAINCODE ==================');
logger.debug('End point : /channels/:channelName/chaincodes/:chaincodeName/:function');
// validate the request
let fields = Object.assign(req.body, req.params);
logger.debug('Input : ' + JSON.stringify(fields));
let validationResponse = helper.validateRequest(fields, ['username', 'orgname',
'chaincodeName', 'channelName', 'function']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Optional request variables
logger.debug('peers'.padEnd(12) + ': ' + fields['peers']);
let response = {success: false, message :'nothing happened'};
let createType = '';
try{
let fcn = fields.function.toString().toLowerCase();
let args = [];
if(fcn == "create"){
createType = fields.type.toString();
if (createType == "item") {
args.push(createType);
args.push(fields.details.name.toString());
args.push(fields.details.amount.toString());
args.push(fields.details.price.toString());
} else if (createType == "money"){
args.push(createType);
args.push(fields.details.name.toString());
args.push(fields.details.amount.toString());
} else if (createType == "po"){
fcn = 'createPO';
for (var i in fields.details) {
args.push(JSON.stringify(fields.details[i]));
}
}
}else if(fcn == "deliver" || fcn == "receive"){
args.push(fields.po.toString());
for (var i in fields.details) {
args.push(JSON.stringify(fields.details[i]));
}
}else if(fcn == "transfer"){
if(orgs.indexOf(fields.details.recipient.toString()) >= 0){
args.push(fields.type.toString());
args.push(fields.details.name.toString());
args.push(fields.details.amount.toString());
args.push(fields.details.recipient.toString() + 'MSP');
if(fields.details.batch) {
args.push(fields.details.batch.toString());
}
} else {
throw new Error('Unknown Organization: ' + fields.details.recipient);
}
} else {
throw new Error("Unknown function.");
}
logger.debug('Function: ' + fcn + ' | Arguments: ' + args);
response = await invoke.invokeChaincode(fields.peers, fields.channelName, fields.chaincodeName,
fcn, args, fields.username, fields.orgname);
if(fcn == "create"){
response.type = createType;
response.name = fields.details.name.toString();
if(response.payload){
response.success = response.payload.success;
if(response.success == true) {
response.sum = response.payload.sum;
response.amount = response.payload.amount;
if (createType == "item") {
response.batch = response.payload.batch;
}
}
if(response.payload.message) {
response.message = response.payload.message;
}
}
}else if(fcn == "createPO"){
if(response.payload){
response.type = createType;
response.success = response.payload.success;
response.status = response.payload.status;
response.total = response.payload.total;
if(response.success == true) {
response.po = response.payload.po;
response.details = response.payload.details;
} else {
response.success = false;
}
if(response.payload.message) {
response.message = response.payload.message;
}
}
}else if(fcn == "deliver" || fcn.toLowerCase() == "receive"){
if(response.payload){
response.success = response.payload.success;
if(response.success == true) {
response.po = response.payload.po;
response.status = response.payload.status;
response.total = response.payload.total;
response.details = response.payload.details;
}
if(response.payload.message) {
response.message = response.payload.message;
}
}
}else if(fcn == "transfer"){
if(response.payload){
response.success = response.payload.success;
response.type = response.payload.type;
response.name = response.payload.name;
response.amount = response.payload.amount;
response.recipient = fields.details.recipient.toString() + 'MSP';
if(response.success == true) {
response.batches = response.payload.batches;
}
if(response.payload.message) {
response.message = response.payload.message;
}
}
} else {
throw new Error("Unknown function.");
}
} catch (error) {
let error_message = error.toString();
response = {success: false, message: error_message};
}
if (response.payload || response.payload == null) {
delete response.payload;
}
logger.debug('APP RESPONSE: ' + JSON.stringify(response));
res.json(response);
});
// Query on chaincode on target peers
app.get('/channels/:channelName/chaincodes/:chaincodeName/:function', async function(req, res) {
logger.debug('==================== QUERY BY CHAINCODE ==================');
logger.debug('End point : /channels/:channelName/chaincodes/:chaincodeName/:function');
// validate the request
let fields = Object.assign(req.body, req.params, req.query);
logger.debug('Input : ' + JSON.stringify(fields));
let validationResponse = helper.validateRequest(fields, ['username', 'orgname',
'chaincodeName', 'channelName', 'function']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Optional request variables
logger.debug('peers'.padEnd(12) + ': ' + fields['peers']);
let response = [];
try{
let fcn = 'query' + fields.function.toString().toUpperCase();
let args = [];
// Parse args
if (fcn == 'queryNAMES'){
args.push((fields.hasOwnProperty('owner') && fields.owner.toString() == 'true').toString());
} else if (fcn == 'queryPO'){
args.push((fields.hasOwnProperty('history') && fields.history.toString() == 'true').toString());
if(fields.po > 0){
args.push(fields.po);
} else {
fcn = fcn + 'ALL';
}
} else if (fcn == 'queryITEM') {
fcn = 'query';
args.push(fields.type);
args.push(fields.name);
if(fields.batch > 0){
args.push(fields.batch);
}
}
logger.debug('args(clean)'.padEnd(12) + ': ' + args);
// Query the chain code
let message = await query.queryChaincode(fields.peer, fields.channelName, fields.chaincodeName, args,
fcn, fields.username, fields.orgname);
// Create output response
if (fcn == 'queryNAMES'){
if (message.success == true){
for (var i=0; i < message.payload.items.length; i++){
response.push(message.payload.items[i]);
}
}
} else if (fcn == 'queryPOALL'){
if (message.success == true){
response = message.payload;
}
} else if (fcn == 'queryPO'){
if (message.success == true){
response = message.payload;
response.success = message.success;
response.message = message.message;
}
} else if (fcn == 'query'){
if (message.success == true){
response = message.payload;
response.success = message.success;
response.message = message.message;
if(!(fields.history && fields.history.toString() == 'true')){
for (var i=0; i < response.batches.length; i++){
delete response.batches[i].history;
}
}
}
}
logger.debug(message);
} catch (error) {
logger.error('Query failed: ' + error.toString());
}
logger.debug('APP RESPONSE: ' + JSON.stringify(response));
res.json(response);
});
// Query Get Block by BlockNumber
app.get('/channels/:channelName/blocks/:blockId', async function(req, res) {
logger.debug('==================== GET BLOCK BY NUMBER ==================');
logger.debug('End point : /channels/:channelName/transactions/:trxnId');
// validate the request
let fields = Object.assign(req.body, req.params, req.query);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname', 'channelName', 'blockId']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Optional request variables
logger.debug('peer'.padEnd(12) + ': ' + fields['peer']);
let message = await query.getBlockByNumber(fields.peer, fields.channelName, fields.blockId, fields.username, fields.orgname);
res.send(message);
});
// Query Get Transaction by Transaction ID
app.get('/channels/:channelName/transactions/:trxnId', async function(req, res) {
logger.debug('==================== GET TRANSACTION BY TRANSACTION_ID ==================');
logger.debug('End point : /channels/:channelName/transactions/:trxnId');
// validate the request
let fields = Object.assign(req.body, req.params, req.query);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname', 'channelName', 'trxnId']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Optional request variables
logger.debug('peer'.padEnd(12) + ': ' + fields['peer']);
let message = await query.getTransactionByID(fields.peer, fields.channelName, fields.trxnId, fields.username, fields.orgname);
res.send(message);
});
// Query Get Block by Hash
app.get('/channels/:channelName/blocks', async function(req, res) {
logger.debug('==================== GET BLOCK BY HASH ==================');
logger.debug('End point : /channels/:channelName/blocks');
// validate the request
let fields = Object.assign(req.body, req.params, req.query);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname', 'channelName', 'hash']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
// Optional request variables
logger.debug('peer'.padEnd(12) + ': ' + fields['peer']);
let message = await query.getBlockByHash(fields.peer, fields.channelName, fields.hash, fields.username, fields.orgname);
res.send(message);
});
// Query to fetch channels
app.get('/channels', async function(req, res) {
logger.debug('==================== GET CHANNELS ==================');
logger.debug('End point : /channels');
// validate the request
let fields = Object.assign(req.body, req.params, req.query);
let validationResponse = helper.validateRequest(fields, ['username', 'orgname', 'peer']);
if(!validationResponse.success) {
res.json(validationResponse);
return;
}
let message = await query.getChannels(fields.peer, fields.username, fields.orgname);
res.send(message);
});
//Query for Channel Information
app.get('/channels/:channelName', async function(req, res) {
logger.debug('================ GET CHANNEL INFORMATION ======================');
logger.debug('channelName'.padEnd(12) + ': ' + req.params.channelName);
logger.debug('peer'.padEnd(12) + ': ' + req.query.peer);
let peer = req.query.peer;
let message = await query.getChainInfo(peer, req.params.channelName, req.query.username, req.query.orgname);
res.send(message);
});
// Query to fetch all Installed/instantiated chaincodes
app.get('/chaincodes', async function(req, res) {
logger.debug('================ GET INSTALLED CHAINCODES ======================');
logger.debug('installType'.padEnd(12) + ': ' + req.query.type);
logger.debug('peer'.padEnd(12) + ': ' + req.query.peer);
var peer = req.query.peer;
var installType = req.query.type;
let message = await query.getInstalledChaincodes(peer, null, 'installed', req.query.username, req.query.orgname)
res.send(message);
});
//Query for Channel instantiated chaincodes
app.get('/channels/:channelName/chaincodes', async function(req, res) {
logger.debug('================ GET INSTANTIATED CHAINCODES ======================');
logger.debug('channelName'.padEnd(12) + ': ' + req.params.channelName);
logger.debug('peer'.padEnd(12) + ': ' + req.query.peer);
let peer = req.query.peer;
let message = await query.getInstalledChaincodes(peer, req.params.channelName, 'instantiated', req.query.username, req.query.orgname);
res.send(message);
});
//Query for Organizations
app.all('/orgs', async function(req, res) {
logger.debug('================ GET ORGANIZATIONS ======================');
logger.debug('End point : /orgs');
res.json(orgs);
});