-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
425 lines (403 loc) · 13 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
/**
* 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('SampleWebApp');
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
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');
require('./config.js');
var hfc = require('fabric-client');
var helper = require('./app/helper.js');
var channels = 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 request = require('./app/request-api.js');
var query = require('./app/query.js');
var host = process.env.HOST || hfc.getConfigSetting('host');
var port = process.env.PORT || hfc.getConfigSetting('port');
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// 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: ['/users']
}));
app.use(bearerToken());
app.use(function(req, res, next) {
if (req.originalUrl.indexOf('/users') >= 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 /users 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://' + 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('/users', function(req, res) {
var username = req.body.username;
var orgName = req.body.orgName;
logger.debug('End point : /users');
logger.debug('User name : ' + username);
logger.debug('Org name : ' + orgName);
if (!username) {
res.json(getErrorMessage('\'username\''));
return;
}
if (!orgName) {
res.json(getErrorMessage('\'orgName\''));
return;
}
var token = jwt.sign({
exp: Math.floor(Date.now() / 1000) + parseInt(hfc.getConfigSetting('jwt_expiretime')),
username: username,
orgName: orgName
}, app.get('secret'));
helper.getRegisteredUsers(username, orgName, true).then(function(response) {
if (response && typeof response !== 'string') {
response.token = token;
res.json(response);
} else {
res.json({
success: false,
message: response
});
}
});
});
// Create Channel
app.post('/channels', function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< C R E A T E C H A N N E L >>>>>>>>>>>>>>>>>');
logger.debug('End point : /channels');
var channelName = req.body.channelName;
var channelConfigPath = req.body.channelConfigPath;
logger.debug('Channel name : ' + channelName);
logger.debug('channelConfigPath : ' + channelConfigPath); //../artifacts/channel/mychannel.tx
if (!channelName) {
res.json(getErrorMessage('\'channelName\''));
return;
}
if (!channelConfigPath) {
res.json(getErrorMessage('\'channelConfigPath\''));
return;
}
channels.createChannel(channelName, channelConfigPath, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Join Channel
app.post('/channels/:channelName/peers', function(req, res) {
logger.info('<<<<<<<<<<<<<<<<< J O I N C H A N N E L >>>>>>>>>>>>>>>>>');
var channelName = req.params.channelName;
var peers = req.body.peers;
logger.debug('channelName : ' + channelName);
logger.debug('peers : ' + peers);
if (!channelName) {
res.json(getErrorMessage('\'channelName\''));
return;
}
if (!peers || peers.length == 0) {
res.json(getErrorMessage('\'peers\''));
return;
}
join.joinChannel(channelName, peers, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Install chaincode on target peers
app.post('/chaincodes', function(req, res) {
logger.debug('==================== INSTALL CHAINCODE ==================');
var peers = req.body.peers;
var chaincodeName = req.body.chaincodeName;
var chaincodePath = req.body.chaincodePath;
var chaincodeVersion = req.body.chaincodeVersion;
logger.debug('peers : ' + peers); // target peers list
logger.debug('chaincodeName : ' + chaincodeName);
logger.debug('chaincodePath : ' + chaincodePath);
logger.debug('chaincodeVersion : ' + chaincodeVersion);
if (!peers || peers.length == 0) {
res.json(getErrorMessage('\'peers\''));
return;
}
if (!chaincodeName) {
res.json(getErrorMessage('\'chaincodeName\''));
return;
}
if (!chaincodePath) {
res.json(getErrorMessage('\'chaincodePath\''));
return;
}
if (!chaincodeVersion) {
res.json(getErrorMessage('\'chaincodeVersion\''));
return;
}
install.installChaincode(peers, chaincodeName, chaincodePath, chaincodeVersion, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Instantiate chaincode on target peers
app.post('/channels/:channelName/chaincodes', function(req, res) {
logger.debug('==================== INSTANTIATE CHAINCODE ==================');
var chaincodeName = req.body.chaincodeName;
var chaincodeVersion = req.body.chaincodeVersion;
var channelName = req.params.channelName;
var fcn = req.body.fcn;
logger.debug('channelName : ' + channelName);
logger.debug('chaincodeName : ' + chaincodeName);
logger.debug('chaincodeVersion : ' + chaincodeVersion);
logger.debug('fcn : ' + fcn);
if (!chaincodeName) {
res.json(getErrorMessage('\'chaincodeName\''));
return;
}
if (!chaincodeVersion) {
res.json(getErrorMessage('\'chaincodeVersion\''));
return;
}
if (!channelName) {
res.json(getErrorMessage('\'channelName\''));
return;
}
instantiate.instantiateChaincode(channelName, chaincodeName, chaincodeVersion, fcn, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Invoke transaction on chaincode on target peers
app.post('/channels/:channelName/chaincodes/:chaincodeName', function(req, res) {
logger.debug('==================== INVOKE ON CHAINCODE ==================');
var peers = req.body.peers;
var chaincodeName = req.params.chaincodeName;
var channelName = req.params.channelName;
var fcn = req.body.fcn;
var args = req.body.args;
logger.debug('channelName : ' + channelName);
logger.debug('chaincodeName : ' + chaincodeName);
logger.debug('fcn : ' + fcn);
logger.debug('args : ' + args);
if (!chaincodeName) {
res.json(getErrorMessage('\'chaincodeName\''));
return;
}
if (!channelName) {
res.json(getErrorMessage('\'channelName\''));
return;
}
if (!fcn) {
res.json(getErrorMessage('\'fcn\''));
return;
}
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
if (fcn == "requestAPI") {
request.requestAPI(peers, channelName, chaincodeName, fcn, args, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
}else {
invoke.invokeChaincode(peers, channelName, chaincodeName, fcn, args, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
}
});
// Query on chaincode on target peers
app.get('/channels/:channelName/chaincodes/:chaincodeName', function(req, res) {
logger.debug('==================== QUERY BY CHAINCODE ==================');
var channelName = req.params.channelName;
var chaincodeName = req.params.chaincodeName;
let args = req.query.args;
let fcn = req.query.fcn;
let peer = req.query.peer;
logger.debug('channelName : ' + channelName);
logger.debug('chaincodeName : ' + chaincodeName);
logger.debug('fcn : ' + fcn);
logger.debug('args : ' + args);
if (!chaincodeName) {
res.json(getErrorMessage('\'chaincodeName\''));
return;
}
if (!channelName) {
res.json(getErrorMessage('\'channelName\''));
return;
}
if (!fcn) {
res.json(getErrorMessage('\'fcn\''));
return;
}
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
args = args.replace(/'/g, '"');
args = JSON.parse(args);
logger.debug(args);
query.queryChaincode(peer, channelName, chaincodeName, args, fcn, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Query Get Block by BlockNumber
app.get('/channels/:channelName/blocks/:blockId', function(req, res) {
logger.debug('==================== GET BLOCK BY NUMBER ==================');
let blockId = req.params.blockId;
let peer = req.query.peer;
logger.debug('channelName : ' + req.params.channelName);
logger.debug('BlockID : ' + blockId);
logger.debug('Peer : ' + peer);
if (!blockId) {
res.json(getErrorMessage('\'blockId\''));
return;
}
query.getBlockByNumber(peer, blockId, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Query Get Transaction by Transaction ID
app.get('/channels/:channelName/transactions/:trxnId', function(req, res) {
logger.debug(
'================ GET TRANSACTION BY TRANSACTION_ID ======================'
);
logger.debug('channelName : ' + req.params.channelName);
let trxnId = req.params.trxnId;
let peer = req.query.peer;
if (!trxnId) {
res.json(getErrorMessage('\'trxnId\''));
return;
}
query.getTransactionByID(peer, trxnId, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Query Get Block by Hash
app.get('/channels/:channelName/blocks', function(req, res) {
logger.debug('================ GET BLOCK BY HASH ======================');
logger.debug('channelName : ' + req.params.channelName);
let hash = req.query.hash;
let peer = req.query.peer;
if (!hash) {
res.json(getErrorMessage('\'hash\''));
return;
}
query.getBlockByHash(peer, hash, req.username, req.orgname).then(
function(message) {
res.send(message);
});
});
//Query for Channel Information
app.get('/channels/:channelName', function(req, res) {
logger.debug(
'================ GET CHANNEL INFORMATION ======================');
logger.debug('channelName : ' + req.params.channelName);
let peer = req.query.peer;
query.getChainInfo(peer, req.username, req.orgname).then(
function(message) {
res.send(message);
});
});
// Query to fetch all Installed/instantiated chaincodes
app.get('/chaincodes', function(req, res) {
var peer = req.query.peer;
var installType = req.query.type;
//TODO: add Constnats
if (installType === 'installed') {
logger.debug(
'================ GET INSTALLED CHAINCODES ======================');
} else {
logger.debug(
'================ GET INSTANTIATED CHAINCODES ======================');
}
query.getInstalledChaincodes(peer, installType, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
});
// Query to fetch channels
app.get('/channels', function(req, res) {
logger.debug('================ GET CHANNELS ======================');
logger.debug('peer: ' + req.query.peer);
var peer = req.query.peer;
if (!peer) {
res.json(getErrorMessage('\'peer\''));
return;
}
query.getChannels(peer, req.username, req.orgname)
.then(function(
message) {
res.send(message);
});
});