-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
400 lines (369 loc) · 12.8 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
/**
* Copyright 2017 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 log4js = require('log4js');
var logger = log4js.getLogger('TradeApp');
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var util = require('util');
var app = express();
var expressJWT = require('express-jwt');
var jwt = require('jsonwebtoken');
var bearerToken = require('express-bearer-token');
var cors = require('cors');
var path = require('path');
var Constants = require('../middleware/constants.js');
var ClientUtils = require('../middleware/clientUtils.js');
var createChannel = require('../middleware/create-channel.js');
var joinChannel = require('../middleware/join-channel.js');
var installCC = require('../middleware/install-chaincode.js');
var instantiateCC = require('../middleware/instantiate-chaincode.js');
var invokeCC = require('../middleware/invoke-chaincode.js');
var queryCC = require('../middleware/query-chaincode.js');
var upgradeChannel = require('../middleware/upgrade-channel.js');
var host = process.env.HOST || 'localhost';
var port = process.env.PORT || 4000;
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// SET CONFIGURATONS ////////////////////////////
///////////////////////////////////////////////////////////////////////////////
app.options('*', cors());
app.use(cors());
//support parsing of application/json type post data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({
extended: false
}));
// set secret variable
app.set('secret', 'thisismysecret');
app.use(expressJWT({
secret: 'thisismysecret'
}).unless({
path: ['/login']
}));
app.use(bearerToken());
app.use(function(req, res, next) {
logger.debug(' ------>>>>>> new request for %s',req.originalUrl);
if (req.originalUrl.indexOf('/login') >= 0) {
return next();
}
var token = req.token;
jwt.verify(token, app.get('secret'), function(err, decoded) {
if (err) {
res.send({
success: false,
message: 'Failed to authenticate token. Make sure to include the ' +
'token returned from /login call in the authorization header ' +
' as a Bearer token'
});
return;
} else {
// add the decoded user name and org name to the request object
// for the downstream code to use
req.username = decoded.username;
req.orgname = decoded.orgName;
logger.debug(util.format('Decoded from JWT token: username - %s, orgname - %s', decoded.username, 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;
function getErrorMessage(field) {
var response = {
success: false,
message: field + ' field is missing or Invalid in the request'
};
return response;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////// REST ENDPOINTS START HERE ///////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Register and enroll user
app.post('/login', async function(req, res) {
var username = req.body.username;
var orgName = req.body.orgName;
var password = req.body.password;
logger.debug('User name for login/registration : ' + username);
logger.debug('Org name : ' + orgName);
if (!username) {
res.json(getErrorMessage('\'username\''));
return;
}
if (!orgName) {
res.json(getErrorMessage('\'orgName\''));
return;
}
// Hardcode single 'admin' user per org for now
if (username === 'admin' && !password) {
res.json(getErrorMessage('\'password\''));
return;
}
var token = jwt.sign({
// Make the token expire 60 seconds from now
exp: Math.floor(Date.now() / 1000) + (69 * 60),
username: username,
orgName: orgName
}, app.get('secret'));
ClientUtils.init();
ClientUtils.getClientUser(orgName, username, password)
.then((response) => {
logger.debug('-- returned from registering (logging in) the username %s for organization %s',username,orgName);
if (response && typeof response !== 'string') {
var resp = {};
resp.token = token;
resp.success = true;
if (response._enrollmentSecret && response._enrollmentSecret.length > 0) {
logger.debug('Successfully registered the username %s for organization %s',username,orgName);
resp.secret = response._enrollmentSecret;
resp.message = 'Registration successful';
} else {
logger.debug('Successfully enrolled the username %s for organization %s',username,orgName);
resp.message = 'Login successful';
}
res.json(resp);
} else {
logger.debug('Failed to register the username %s for organization %s with::%s',username,orgName,response);
var message = 'Registration/login failed';
if (response) {
message = JSON.stringify(response);
}
res.json({success: false, message: message});
}
})
.catch((err) => {
console.error(err);
logger.debug('Failed to register username %s for organization %s with::%s',username,orgName,err.message);
res.json({success: false, message: err.message});
});
});
// Create Channel
app.post('/channel/create', async function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< C R E A T E C H A N N E L >>>>>>>>>>>>>>>>>');
if (req.username !== 'admin') {
res.statusCode = 403;
res.send('Not an admin user: ' + req.username);
return;
}
createChannel.createChannel(Constants.CHANNEL_NAME).then(() => {
res.json({success: true, message: 'Channel created'});
}, (err) => {
res.json({success: false, message: err.message});
});
});
// Join Channel
app.post('/channel/join', async function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< J O I N C H A N N E L >>>>>>>>>>>>>>>>>');
logger.debug('username :' + req.username);
logger.debug('orgname:' + req.orgname);
if (req.username !== 'admin') {
res.statusCode = 403;
res.send('Not an admin user: ' + req.username);
return;
}
joinChannel.processJoinChannel().then(() => {
res.json({success: true, message: 'Channel joined'});
}, (err) => {
res.json({success: false, message: err.message});
});
});
// Add an Organization and Peer to the Channel
app.post('/channel/addorg', async function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< A D D O R G A N D P E E R T O C H A N N E L >>>>>>>>>>>>>>>>>');
if (req.username !== 'admin') {
res.statusCode = 403;
res.send('Not an admin user: ' + req.username);
return;
}
// Update the channel configuration, and then join the new org's peer to the channel
upgradeChannel.upgradeChannel(Constants.CHANNEL_NAME).then(() => {
// Update the configuration settings
Constants.networkConfig = '../middleware/config_upgrade.json';
var Client = require('fabric-client');
Client.addConfigFile(path.join(__dirname, Constants.networkConfig));
var ORGS = Client.getConfigSetting(Constants.networkId);
return joinChannel.joinChannel('exportingentityorg', ORGS, Constants);
}, (err) => {
res.json({success: false, message: err.message});
}).then(() => {
res.json({success: true, message: 'New Organization and Peer Added to Channel'});
}, (err) => {
res.json({success: false, message: err.message});
});
});
// Install chaincode on target peers
app.post('/chaincode/install', async function(req, res) {
logger.debug('==================== INSTALL CHAINCODE ==================');
logger.debug('username :' + req.username);
logger.debug('orgname:' + req.orgname);
if (req.username !== 'admin') {
res.statusCode = 403;
res.send('Not an admin user: ' + req.username);
return;
}
var ccpath = req.body.ccpath;
if (!ccpath) {
res.json(getErrorMessage('\'ccpath\''));
return;
}
var ccversion = req.body.ccversion;
if (!ccversion) {
res.json(getErrorMessage('\'ccversion\''));
return;
}
installCC.installChaincode(ccpath, ccversion).then(() => {
res.json({success: true, message: 'Chaincode installed'});
}, (err) => {
res.json({success: false, message: err.message});
});
});
// Instantiate chaincode on channel
app.post('/chaincode/instantiate', async function(req, res) {
logger.debug('==================== INSTANTIATE CHAINCODE ==================');
logger.debug('username :' + req.username);
logger.debug('orgname:' + req.orgname);
if (req.username !== 'admin') {
res.statusCode = 403;
res.send('Not an admin user: ' + req.username);
return;
}
var ccpath = req.body.ccpath;
if (!ccpath) {
res.json(getErrorMessage('\'ccpath\''));
return;
}
var ccversion = req.body.ccversion;
if (!ccversion) {
res.json(getErrorMessage('\'ccversion\''));
return;
}
var args = req.body.args;
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
logger.debug('args : ' + args);
instantiateCC.instantiateOrUpgradeChaincode(req.orgname, ccpath, ccversion, 'init', args, false)
.then(() => {
res.json({success: true, message: 'Chaincode instantiated'});
}, (err) => {
res.json({success: false, message: err.message});
});
ClientUtils.txEventsCleanup();
});
// Install new chaincode version on network peers and upgrade it on the channel
app.post('/chaincode/upgrade', async function(req, res) {
logger.debug('==================== UPGRADE CHAINCODE ==================');
logger.debug('username :' + req.username);
logger.debug('orgname:' + req.orgname);
if (req.username !== 'admin') {
res.statusCode = 403;
res.send('Not an admin user: ' + req.username);
return;
}
var ccpath = req.body.ccpath;
if (!ccpath) {
res.json(getErrorMessage('\'ccpath\''));
return;
}
var ccversion = req.body.ccversion;
if (!ccversion) {
res.json(getErrorMessage('\'ccversion\''));
return;
}
var args = req.body.args;
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
logger.debug('args : ' + args);
// Update the configuration settings
Constants.networkConfig = '../middleware/config_upgrade.json';
Constants.TRANSACTION_ENDORSEMENT_POLICY = Constants.ALL_FIVE_ORG_MEMBERS;
// Install and then upgrade the chaincode
installCC.installChaincode(ccpath, ccversion, Constants).then(() => {
return instantiateCC.instantiateOrUpgradeChaincode(req.orgname, ccpath, ccversion, 'init', args, true, Constants);
}, (err) => {
res.json({success: false, message: err.message});
}).then(() => {
res.json({success: true, message: 'New version of Chaincode installed and upgraded'});
}, (err) => {
res.json({success: false, message: err.message});
});
ClientUtils.txEventsCleanup();
});
// Invoke transaction on chaincode on network peers
app.post('/chaincode/:fcn', async function(req, res) {
logger.debug('==================== INVOKE ON CHAINCODE ==================');
logger.debug('username :' + req.username);
logger.debug('orgname:' + req.orgname);
var ccversion = req.body.ccversion;
if (!ccversion) {
res.json(getErrorMessage('\'ccversion\''));
return;
}
var fcn = req.params.fcn;
var args = req.body.args;
if (!fcn) {
res.json(getErrorMessage('\'fcn\''));
return;
}
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
logger.debug('args : ' + args);
invokeCC.invokeChaincode(req.orgname, ccversion, fcn, args, req.username).then(() => {
res.json({success: true, message: 'Chaincode invoked'});
}, (err) => {
res.json({success: false, message: err.message});
});
ClientUtils.txEventsCleanup();
});
// Query on chaincode on network peers
app.get('/chaincode/:fcn', async function(req, res) {
logger.debug('==================== QUERY BY CHAINCODE ==================');
logger.debug('username :' + req.username);
logger.debug('orgname:' + req.orgname);
var ccversion = req.body.ccversion;
if (!ccversion) {
res.json(getErrorMessage('\'ccversion\''));
return;
}
var fcn = req.params.fcn;
var args = req.body.args;
if (!fcn) {
res.json(getErrorMessage('\'fcn\''));
return;
}
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
logger.debug('args : ' + args);
queryCC.queryChaincode(req.orgname, ccversion, fcn, args).then((result) => {
res.json({success: true, message: result});
}, (err) => {
res.json({success: false, message: err.message});
});
ClientUtils.txEventsCleanup();
});