-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
681 lines (620 loc) · 32.6 KB
/
index.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
'use strict';
/*******************************************************************************
* Copyright (c) 2016 theloop.co.kr
* All rights reserved.
*******************************************************************************/
var fs = require('fs');
var path = require('path');
var http = require('http');
var https = require('https');
var async = require('async');
var rest = require(__dirname + '/lib/rest.js');
var helper = require(__dirname + '/lib/helper.js');
var logger = {log: console.log, error: console.error, debug: console.log, warn: console.log};
function ibc(log_outputs) {
if(log_outputs){
switch (log_outputs) {
case log_outputs.info :
logger.log = log_outputs.info;
break;
case log_outputs.error :
logger.error = log_outputs.error;
break;
case log_outputs.warn :
logger.warn = log_outputs.warn;
break;
case log_outputs.debug :
logger.debug = log_outputs.debug;
break;
default:
logger.log = log_outputs.info;
}
}
}
ibc.chaincode = {
query: {},
invoke: {},
deploy: null,
details:{
deployed_name: '',
func: {
invoke: [],
query: []
},
git_url: '',
options: {},
peers: [],
timestamp: 0,
users: [],
unzip_dir: '',
version: '',
zip_url: '',
}
};
ibc.selectedPeer = 0;
ibc.q = []; //array of unix timestamps, 1 for each unsettled action
ibc.lastPoll = 0; //unix timestamp of the last time we polled
ibc.lastBlock = 0; //last blockheight found
var tempDirectory = path.join(__dirname, './temp'); // =./temp - temp directory name
// ============================================================================================================================
// EXTERNAL - load() - wrapper on a standard startup flow.
// 1. load network peer data
// 2. register users with security (if present)
// 3. load chaincode
// ============================================================================================================================
ibc.prototype.load = function(options, cb){
var errors = [];
if(!options.network || !options.network.peers) errors.push('the option "network.peers" is required');
if(errors.length > 0){ //check for input errors
logger.error('! [fabric-js] Input Error - ibc.load()', errors);
if(cb) cb(helper.eFmt('load() input error', 400, errors));
return; //get out of dodge
}
ibc.chaincode = { //empty it all
query: {
read: null
},
invoke: {},
deploy: null,
details:{
deployed_name: '',
func: {
invoke: [],
query: []
},
git_url: '',
options: options.network.options,
peers: [],
timestamp: 0,
users: [],
unzip_dir: '',
version: '',
zip_url: '',
}
};
// Step 1
ibc.prototype.network(options.network.peers, options.network.options);
// Step 2 - optional - only for secure networks
if(options.network.users){
options.network.users = options.network.users;
}
if(options.network.users && options.network.users.length > 0){
ibc.chaincode.details.users = options.network.users;
var arr = [];
for(var i in ibc.chaincode.details.peers){
arr.push(i); //build the list of indexes
}
async.each(arr, function(i, a_cb) {
if(options.network.users[i]){ //make sure we still have a enrollID for this network
var maxRetry = 2;
if(options.network.options && options.network.options.maxRetry) maxRetry = options.network.options.maxRetry;
ibc.prototype.register(i, options.network.users[i].enrollId, options.network.users[i].enrollSecret, maxRetry, a_cb);
}
else a_cb();
}, function(err, data){
if(err && cb) return cb(err); //error already formated
else load_cc();
});
}
else{
ibc.chaincode.details.users = [];
logger.log('[fabric-js] No membership users found after filtering, assuming this is a network w/o membership');
load_cc();
}
// Step 3
function load_cc(){
//load chaincode
ibc.prototype.load_chaincode(options.chaincode, cb);
}
};
// ============================================================================================================================
// EXTERNAL - load_chaincode() - load the chaincode
// 2. Create Function
// Create JS invoke functions for golang functions
// Create JS query functions for golang functions
// 3. Call callback()
// ============================================================================================================================
ibc.prototype.load_chaincode = function(options, cb) {
var go_funcs = [], cc_suspects = [], cc_invocations = [], cc_queries = [], cc_inits = [];
var found_query = false, found_invoke = false;
ibc.chaincode.details.deployed_name = options.deploy_name;
ibc.chaincode.details.git_url = options.git_url;
if( options.version ){
ibc.chaincode.details.version = options.version;
}
if( options.invoke.length > 0 ) {
found_invoke = true;
ibc.chaincode.details.func.invoke = [];
for(var i in options.invoke){
build_invoke_func(options.invoke[i]);
}
}
if( options.query.length > 0 ) {
found_query = true;
ibc.chaincode.details.func.query = [];
for(var i in options.query){ //build the rest call for each function
build_query_func(options.query[i]);
}
}
// Step 3. success!
logger.log('[fabric-js] load_chaincode() finished');
ibc.chaincode.details.timestamp = Date.now();
ibc.chaincode.deploy = deploy;
if(cb) {
return cb(null, ibc.chaincode);
}
};
// ============================================================================================================================
// EXTERNAL - network() - setup network configuration to hit a rest peer
// ============================================================================================================================
ibc.prototype.network = function(arrayPeers, options){
var errors = [];
ibc.chaincode.details.options = {quiet: true, timeout: 60000, tls: true}; //defaults
if(!arrayPeers || arrayPeers.constructor !== Array) errors.push('network input arg should be array of peer objects');
if(options){
if(options.quiet === true || options.quiet === false) ibc.chaincode.details.options.quiet = options.quiet; //optional fields
if(!isNaN(options.timeout)) ibc.chaincode.details.options.timeout = Number(options.timeout);
if(options.tls === true || options.tls === false) ibc.chaincode.details.options.tls = options.tls;
}
for(var i in arrayPeers){ //check for errors in peers input obj
if(!arrayPeers[i].id) errors.push('peer ' + i + ' is missing the field id');
if(!arrayPeers[i].api_host) errors.push('peer ' + i + ' is missing the field api_host');
if(options && options.tls === false){
if(!arrayPeers[i].api_port) errors.push('peer ' + i + ' is missing the field api_port');
}
else{
if(!arrayPeers[i].api_port_tls) errors.push('peer ' + i + ' is missing the field api_port_tls');
}
}
if(errors.length > 0){ //check for input errors
logger.error('! [fabric-js] Input Error - ibc.network()', errors);
}
else{
ibc.chaincode.details.peers = [];
for(i in arrayPeers){
var pos = arrayPeers[i].id.indexOf('_') + 1;
var temp = {
name: arrayPeers[i].id.substring(pos) + '-' + arrayPeers[i].id.substring(0, 12) + '...:' + arrayPeers[i].api_port_tls,
api_host: arrayPeers[i].api_host,
api_port: arrayPeers[i].api_port,
api_port_tls: arrayPeers[i].api_port_tls,
id: arrayPeers[i].id,
tls: ibc.chaincode.details.options.tls
};
if(options && options.tls === false){ //if not tls rebuild a few things
temp.name = arrayPeers[i].id.substring(pos) + '-' + arrayPeers[i].id.substring(0, 12) + '...:' + arrayPeers[i].api_port;
}
logger.log('[fabric-js] Peer: ', temp.name); //print the friendly name
ibc.chaincode.details.peers.push(temp);
}
rest.init({ //load default values for rest call to peer
host: ibc.chaincode.details.peers[0].api_host,
port: pick_port(0),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
ssl: ibc.chaincode.details.peers[0].tls,
timeout: ibc.chaincode.details.options.timeout,
quiet: ibc.chaincode.details.options.quiet
}, logger);
}
};
//pick tls or non-tls port based on the tls setting
function pick_port(pos){
var port = ibc.chaincode.details.peers[pos].api_port_tls;
if(ibc.chaincode.details.peers[pos].tls === false) port = ibc.chaincode.details.peers[pos].api_port;
return port;
}
// ============================================================================================================================
// EXTERNAL - switchPeer() - switch the default peer to hit
// ============================================================================================================================
ibc.prototype.switchPeer = function(index) {
if(ibc.chaincode.details.peers[index]) {
rest.init({ //load default values for rest call to peer
host: ibc.chaincode.details.peers[index].api_host,
port: pick_port(index),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
ssl: ibc.chaincode.details.peers[index].tls,
timeout: ibc.chaincode.details.options.timeout,
quiet: ibc.chaincode.details.options.quiet
});
ibc.selectedPeer = index;
return true;
} else {
return false;
}
};
// ============================================================================================================================
// EXTERNAL - save() - write chaincode details to a json file
// ============================================================================================================================
ibc.prototype.save = function(dir, cb){
var errors = [];
if(!dir) errors.push('the option "dir" is required');
if(errors.length > 0){ //check for input errors
logger.error('[fabric-js] Input Error - ibc.save()', errors);
if(cb) cb(helper.eFmt('save() input error', 400, errors));
}
else{
var fn = 'chaincode.json'; //default name
if(ibc.chaincode.details.deployed_name) fn = ibc.chaincode.details.deployed_name + '.json';
var dest = path.join(dir, fn);
fs.writeFile(dest, JSON.stringify({details: ibc.chaincode.details}), function(e){
if(e != null){
logger.error('[fabric-js] ibc.save() error', e);
if(cb) cb(helper.eFmt('save() fs write error', 500, e), null);
}
else {
if(cb) cb(null, null);
}
});
}
};
// ============================================================================================================================
// EXTERNAL - clear() - clear the temp directory
// ============================================================================================================================
ibc.prototype.clear = function(cb){
logger.log('[fabric-js] removing temp dir');
helper.removeThing(tempDirectory, cb); //remove everything in this directory
};
//============================================================================================================================
// EXTERNAL chain_stats() - get blockchain stats
//============================================================================================================================
ibc.prototype.chain_stats = function(cb){
var options = {path: '/chain'}; //very simple API, get chainstats!
options.success = function(statusCode, data){
logger.log('[fabric-js] Chain Stats - success');
if(cb) cb(null, data);
};
options.failure = function(statusCode, e){
logger.error('[fabric-js] Chain Stats - failure:', statusCode, e);
if(cb) cb(helper.eFmt('chain_stats() error', statusCode, e), null);
};
rest.get(options, '');
};
//============================================================================================================================
// EXTERNAL block_stats() - get block meta data
//============================================================================================================================
ibc.prototype.block_stats = function(id, cb){
var options = {path: '/chain/blocks/' + id}; //i think block IDs start at 0, height starts at 1, fyi
options.success = function(statusCode, data){
logger.log('[fabric-js] Block Stats - success');
if(cb) cb(null, data);
};
options.failure = function(statusCode, e){
logger.error('[fabric-js] Block Stats - failure:', statusCode);
if(cb) cb(helper.eFmt('block_stats() error', statusCode, e), null);
};
rest.get(options, '');
};
//============================================================================================================================
// EXTERNAL - register() - register a enrollId with a peer (only for a blockchain network with membership)
//============================================================================================================================
ibc.prototype.register = function(index, enrollID, enrollSecret, maxRetry, cb) {
register(index, enrollID, enrollSecret, maxRetry, 1, cb);
};
function register(index, enrollID, enrollSecret, maxRetry, attempt, cb){
logger.log('[fabric-js] Registering ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID);
var options = {
path: '/registrar',
host: ibc.chaincode.details.peers[index].api_host,
port: pick_port(index),
ssl: ibc.chaincode.details.peers[index].tls
};
var body = {
enrollId: enrollID,
enrollSecret: enrollSecret
};
options.success = function(statusCode, data){
logger.log('[fabric-js] Registration success x' + attempt + ' :', enrollID);
ibc.chaincode.details.peers[index].enrollID = enrollID; //remember a valid enrollID for this peer
if(cb) cb(null, data);
};
options.failure = function(statusCode, e){
logger.error('[fabric-js] Register - failure x' + attempt + ' :', enrollID, statusCode);
if(attempt <= maxRetry){ //lets try again after a short delay, maybe the peer is still starting
logger.log('[fabric-js] \tgoing to try to register again in 30 secs');
setTimeout(function(){register(index, enrollID, enrollSecret, maxRetry, ++attempt, cb);}, 30000);
}
else{
if(cb) cb(helper.eFmt('register() error', statusCode, e), null); //give up
}
};
rest.post(options, '', body);
}
//============================================================================================================================
// EXTERNAL - unregister() - unregister a enrollId from a peer (only for a blockchain network with membership), enrollID can no longer make transactions
//============================================================================================================================
ibc.prototype.unregister = function(index, enrollID, cb) {
logger.log('[fabric-js] Unregistering ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID);
var options = {
path: '/registrar/' + enrollID,
host: ibc.chaincode.details.peers[index].api_host,
port: pick_port(index),
ssl: ibc.chaincode.details.peers[index].tls
};
options.success = function(statusCode, data){
logger.log('[fabric-js] Unregistering success:', enrollID);
ibc.chaincode.details.peers[index].enrollID = null; //unremember a valid enrollID for this peer
if(cb) cb(null, data);
};
options.failure = function(statusCode, e){
logger.log('[fabric-js] Unregistering - failure:', enrollID, statusCode);
if(cb) cb(helper.eFmt('unregister() error', statusCode, e), null);
};
rest.delete(options, '');
};
//============================================================================================================================
// EXTERNAL - check_register() - check if a enrollID is registered or not with a peer
//============================================================================================================================
ibc.prototype.check_register = function(index, enrollID, cb) {
logger.log('[fabric-js] Checking ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID);
var options = {
path: '/registrar/' + enrollID,
host: ibc.chaincode.details.peers[index].api_host,
port: pick_port(index),
ssl: ibc.chaincode.details.peers[index].tls
};
options.success = function(statusCode, data){
logger.log('[fabric-js] Check Register success:', enrollID);
if(cb) cb(null, data);
};
options.failure = function(statusCode, e){
logger.error('[fabric-js] Check Register - failure:', enrollID, statusCode);
if(cb) cb(helper.eFmt('check_register() error', statusCode, e), null);
};
rest.get(options, '');
};
//============================================================================================================================
//deploy() - deploy chaincode and call a cc function
//============================================================================================================================
function deploy(func, args, deploy_options, enrollId, cb){
if(typeof enrollId === 'function'){ //if cb is in 2nd param use known enrollId
cb = enrollId;
enrollId = ibc.chaincode.details.peers[ibc.selectedPeer].enrollID;
}
if(enrollId == null) { //if enrollId not provided, use known valid one
enrollId = ibc.chaincode.details.peers[ibc.selectedPeer].enrollID;
}
logger.log('[fabric-js] Deploy Chaincode - Starting');
logger.log('[fabric-js] \tfunction:', func, ', arg:', args);
logger.log('\n\n\t Waiting...'); //this can take awhile
var options = {}, body = {};
options = {path: '/chaincode'};
body = {
jsonrpc: '2.0',
method: 'deploy',
params: {
type: 1,
chaincodeID:{
path: ibc.chaincode.details.git_url
},
ctorMsg: {
function: func,
args: args
},
secureContext: enrollId
},
id: Date.now()
};
// ---- Success ---- //
options.success = function(statusCode, data){
ibc.chaincode.details.deployed_name = data.result.message;
//if(ibc.chaincode.details.deployed_name.length < 32) ibc.chaincode.details.deployed_name = ''; //doesnt look right, let code below catch error
if(ibc.chaincode.details.deployed_name === ''){
logger.error('\n\n\t deploy resp error - there is no chaincode hash name in response:', data);
if(cb) cb(helper.eFmt('deploy() error no cc name', 502, data), null);
}
else{
ibc.prototype.save(tempDirectory); //save it to known place so we remember the cc name
if(deploy_options && deploy_options.save_path != null) { //save it to custom route
ibc.prototype.save(deploy_options.save_path);
}
if(cb){
var wait_ms = 500; //default wait after deploy, peer may still be starting
if(deploy_options && deploy_options.delay_ms && Number(deploy_options.delay_ms)) wait_ms = deploy_options.delay_ms;
logger.log('\n\n\t deploy success [waiting another', (wait_ms / 1000) ,'seconds]');
logger.log('\t', ibc.chaincode.details.deployed_name, '\n');
setTimeout(function(){
logger.log('[fabric-js] Deploy Chaincode - Complete');
cb(null, data);
}, wait_ms); //wait extra long, not always ready yet
}
}
};
// ---- Failure ---- ///
options.failure = function(statusCode, e){
logger.error('[fabric-js] deploy - failure:', statusCode);
if(cb) cb(helper.eFmt('deploy() error', statusCode, e), null);
};
rest.post(options, '', body);
}
//============================================================================================================================
//heart_beat() - interval function to poll against blockchain height (has fast and slow mode)
//============================================================================================================================
var slow_mode = 10000;
var fast_mode = 500;
function heart_beat(){
if(ibc.lastPoll + slow_mode < Date.now()){ //slow mode poll
//logger.log('[fabric-js] Its been awhile, time to poll');
ibc.lastPoll = Date.now();
ibc.prototype.chain_stats(cb_got_stats);
}
else{
for(var i in ibc.q){
var elasped = Date.now() - ibc.q[i];
if(elasped <= 3000){ //fresh unresolved action, fast mode!
logger.log('[fabric-js] Unresolved action, must poll');
ibc.lastPoll = Date.now();
ibc.prototype.chain_stats(cb_got_stats);
}
else{
//logger.log('[fabric-js] Expired, removing');
ibc.q.pop(); //expired action, remove it
}
}
}
}
function cb_got_stats(e, stats){
if(e == null){
if(stats && stats.height){
if(ibc.lastBlock != stats.height) { //this is a new block!
logger.log('[fabric-js] New block!', stats.height);
ibc.lastBlock = stats.height;
ibc.q.pop(); //action is resolved, remove
if(ibc.monitorFunction) ibc.monitorFunction(stats); //call the user's callback
}
}
}
}
//============================================================================================================================
// EXTERNAL- monitor_blockheight() - exposed function that user can use to get callback when any new block is written to the chain
//============================================================================================================================
ibc.prototype.monitor_blockheight = function(cb) { //hook in your own function, triggers when chain grows
setInterval(function(){heart_beat();}, fast_mode);
ibc.monitorFunction = cb; //store it
};
//============================================================================================================================
// EXTERNAL- get_transaction() - exposed function to find a transaction based on its UDID
//============================================================================================================================
ibc.prototype.get_transaction = function(udid, cb) {
var options = {
path: '/transactions/' + udid
};
options.success = function(statusCode, data){
logger.log('[fabric-js] Get Transaction - success:', data);
if(cb) cb(null, data);
};
options.failure = function(statusCode, e){
logger.error('[fabric-js] Get Transaction - failure:', statusCode);
if(cb) cb(helper.eFmt('read() error', statusCode, e), null);
};
rest.get(options, '');
};
//============================================================================================================================
// Helper Functions()
//============================================================================================================================
//build_invoke_func() - create JS function that calls the custom goLang function in the chaincode
//==================================================================
function build_invoke_func(name){
if(ibc.chaincode.invoke[name] != null){ //skip if already exists
logger.log('[fabric-js] \t skip, func', name, 'already exists');
}
else {
logger.log('[fabric-js] Found cc invoke function: ', name);
ibc.chaincode.details.func.invoke.push(name);
ibc.chaincode.invoke[name] = function(args, enrollId, cb){ //create the function in the chaincode obj
if(typeof enrollId === 'function'){ //if cb is in 2nd param use known enrollId
cb = enrollId;
enrollId = ibc.chaincode.details.peers[ibc.selectedPeer].enrollID;
}
if(enrollId == null) { //if enrollId not provided, use known valid one
enrollId = ibc.chaincode.details.peers[ibc.selectedPeer].enrollID;
}
var options = {}, body = {};
//if(ibc.chaincode.details.version.indexOf('hyperledger/fabric/core/chaincode/shim') >= 0){
options = {path: '/chaincode'};
body = {
jsonrpc: '2.0',
method: 'invoke',
params: {
type: 1,
chaincodeID:{
name: ibc.chaincode.details.deployed_name
},
ctorMsg: {
function: name,
args: args
},
secureContext: enrollId
},
id: Date.now()
};
options.success = function(statusCode, data){
// logger.log('[fabric-js]', name, ' - success:', data);
ibc.q.push(Date.now()); //new action, add it to queue
if(cb) cb(null, data);
};
options.failure = function(statusCode, e){
logger.error('[fabric-js]', name, ' - failure:', statusCode, e);
if(cb) cb(helper.eFmt('invoke() error', statusCode, e), null);
};
rest.post(options, '', body);
};
}
}
//==================================================================
//build_query_func() - create JS function that calls the custom goLang function in the chaincode
//==================================================================
function build_query_func(name){
if(ibc.chaincode.query[name] == null || name === 'read'){ // Check Exist
logger.log('[fabric-js] Found cc query function: ', name);
ibc.chaincode.details.func.query.push(name);
ibc.chaincode.query[name] = function(args, enrollId, cb){ //create the function in the chaincode obj
if(typeof enrollId === 'function'){ //if cb is in 2nd param use known enrollId
cb = enrollId;
enrollId = ibc.chaincode.details.peers[ibc.selectedPeer].enrollID;
}
if(enrollId == null) { //if enrollId not provided, use known valid one
enrollId = ibc.chaincode.details.peers[ibc.selectedPeer].enrollID;
}
var options = {}, body = {};
options = {path: '/chaincode'};
body = {
jsonrpc: '2.0',
method: 'query',
params: {
type: 1,
chaincodeID:{
name: ibc.chaincode.details.deployed_name
},
ctorMsg: {
function: name,
args: args
},
secureContext: enrollId
},
id: Date.now()
};
options.success = function(statusCode, data){
//logger.log('[fabric-js]', name, ' - success:', data);
if(cb){
if(data){
if(data.result) cb(null, data.result.message);
else cb(null, data.OK);
}
else cb(helper.eFmt('query() resp error', 502, data), null); //something is wrong, response is not what we expect
}
};
options.failure = function(statusCode, e){
logger.error('[fabric-js]', name, ' - failure:', statusCode, e);
if(cb) cb(helper.eFmt('query() error', statusCode, e), null);
};
rest.post(options, '', body);
};
}
}
module.exports = ibc;