-
Notifications
You must be signed in to change notification settings - Fork 977
/
marbles_cc_lib.js
360 lines (318 loc) · 10.5 KB
/
marbles_cc_lib.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
//-------------------------------------------------------------------
// Marbles Chaincode Library
// - this contains the most interesting code pieces of marbles.
// - each function is using the FCW library to communicate to the peer/orderer
// - from here we can interact with our chaincode.
// - the cc_function is the chaincode function we will call
// - the cc_args are the arguments to pass to your chaincode function
//-------------------------------------------------------------------
module.exports = function (enrollObj, g_options, fcw, logger) {
var marbles_chaincode = {};
// Chaincode -------------------------------------------------------------------------------
//check if chaincode exists
marbles_chaincode.check_if_already_instantiated = function (options, cb) {
console.log('');
logger.info('Checking for chaincode...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
cc_function: 'read',
cc_args: ['selftest']
};
fcw.query_chaincode(enrollObj, opts, function (err, resp) { // send a request to our peer
if (err != null) {
if (cb) return cb(err, resp);
}
else {
if (resp.parsed == null || isNaN(resp.parsed)) { //if nothing is here, no chaincode
if (cb) return cb({ error: 'chaincode not found' }, resp);
}
else {
if (cb) return cb(null, resp);
}
}
});
};
//check chaincode version
marbles_chaincode.check_version = function (options, cb) {
console.log('');
logger.info('Checking chaincode and ui compatibility...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
cc_function: 'read',
cc_args: ['marbles_ui']
};
fcw.query_chaincode(enrollObj, opts, function (err, resp) {
if (err != null) {
if (cb) return cb(err, resp);
}
else {
if (resp.parsed == null) { //if nothing is here, no chaincode
if (cb) return cb({ error: 'chaincode not found' }, resp);
}
else {
if (cb) return cb(null, resp);
}
}
});
};
// Marbles -------------------------------------------------------------------------------
//create a marble
marbles_chaincode.create_a_marble = function (options, cb) {
console.log('');
logger.info('Creating a marble...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
event_urls: g_options.event_urls,
endorsed_hook: options.endorsed_hook,
ordered_hook: options.ordered_hook,
cc_function: 'init_marble',
cc_args: [
'm' + leftPad(Date.now() + randStr(5), 19),
options.args.color,
options.args.size,
options.args.owner_id,
options.args.auth_company
],
};
fcw.invoke_chaincode(enrollObj, opts, function (err, resp) {
if (cb) {
if (!resp) resp = {};
resp.id = opts.cc_args[0]; //pass marble id back
cb(err, resp);
}
});
};
//get marble
marbles_chaincode.get_marble = function (options, cb) {
logger.info('fetching marble ' + options.marble_id + ' list...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_version: g_options.chaincode_version,
chaincode_id: g_options.chaincode_id,
cc_function: 'read',
cc_args: [options.args.marble_id]
};
fcw.query_chaincode(enrollObj, opts, cb);
};
//set marble owner
marbles_chaincode.set_marble_owner = function (options, cb) {
console.log('');
logger.info('Setting marble owner...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
event_urls: g_options.event_urls,
endorsed_hook: options.endorsed_hook,
ordered_hook: options.ordered_hook,
cc_function: 'set_owner',
cc_args: [
options.args.marble_id,
options.args.owner_id,
options.args.auth_company
],
};
fcw.invoke_chaincode(enrollObj, opts, cb);
};
//delete marble
marbles_chaincode.delete_marble = function (options, cb) {
console.log('');
logger.info('Deleting a marble...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
event_urls: g_options.event_urls,
endorsed_hook: options.endorsed_hook,
ordered_hook: options.ordered_hook,
cc_function: 'delete_marble',
cc_args: [options.args.marble_id, options.args.auth_company],
};
fcw.invoke_chaincode(enrollObj, opts, cb);
};
//get history for key
marbles_chaincode.get_history = function (options, cb) {
logger.info('Getting history for...', options.args);
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
event_urls: g_options.event_urls,
endorsed_hook: options.endorsed_hook,
ordered_hook: options.ordered_hook,
cc_function: 'getHistory',
cc_args: [options.args.id]
};
fcw.query_chaincode(enrollObj, opts, cb);
};
//get multiple marbles/owners by start and stop ids
marbles_chaincode.get_multiple_keys = function (options, cb) {
logger.info('Getting marbles between ids', options.args);
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
event_urls: g_options.event_urls,
endorsed_hook: options.endorsed_hook,
ordered_hook: options.ordered_hook,
cc_function: 'getMarblesByRange',
cc_args: [options.args.start_id, options.args.stop_id]
};
fcw.query_chaincode(enrollObj, opts, cb);
};
// Owners -------------------------------------------------------------------------------
//register a owner/user
marbles_chaincode.register_owner = function (options, cb) {
console.log('');
logger.info('Creating a marble owner...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
event_urls: g_options.event_urls,
endorsed_hook: options.endorsed_hook,
ordered_hook: options.ordered_hook,
cc_function: 'init_owner',
cc_args: [
'o' + leftPad(Date.now() + randStr(5), 19),
options.args.marble_owner,
options.args.owners_company
],
};
fcw.invoke_chaincode(enrollObj, opts, function (err, resp) {
if (cb) {
if (!resp) resp = {};
resp.id = opts.cc_args[0]; //pass owner id back
cb(err, resp);
}
});
};
//get a owner/user
marbles_chaincode.get_owner = function (options, cb) {
var full_username = build_owner_name(options.args.marble_owner, options.args.owners_company);
console.log('');
logger.info('Fetching owner ' + full_username + ' list...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
cc_function: 'read',
cc_args: [full_username]
};
fcw.query_chaincode(enrollObj, opts, cb);
};
//get the owner list
marbles_chaincode.get_owner_list = function (options, cb) {
console.log('');
logger.info('Fetching owner index list...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
cc_function: 'read',
cc_args: ['_ownerindex']
};
fcw.query_chaincode(enrollObj, opts, cb);
};
// disable a marble owner
marbles_chaincode.disable_owner = function (options, cb) {
console.log('');
logger.info('Disabling a marble owner...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
event_urls: g_options.event_urls,
endorsed_hook: options.endorsed_hook,
ordered_hook: options.ordered_hook,
cc_function: 'disable_owner',
cc_args: [
options.args.owner_id,
options.args.auth_company
],
};
fcw.invoke_chaincode(enrollObj, opts, function (err, resp) {
if (cb) {
if (!resp) resp = {};
resp.id = opts.cc_args[0]; //pass owner id back
cb(err, resp);
}
});
};
//build full name
marbles_chaincode.build_owner_name = function (username, company) {
return build_owner_name(username, company);
};
// All ---------------------------------------------------------------------------------
//build full name
marbles_chaincode.read_everything = function (options, cb) {
console.log('');
logger.info('Fetching EVERYTHING...');
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts,
channel_id: g_options.channel_id,
chaincode_version: g_options.chaincode_version,
chaincode_id: g_options.chaincode_id,
cc_function: 'read_everything',
cc_args: ['']
};
fcw.query_chaincode(enrollObj, opts, cb);
};
// get block height of the channel
marbles_chaincode.channel_stats = function (options, cb) {
var opts = {
peer_urls: g_options.peer_urls,
peer_tls_opts: g_options.peer_tls_opts
};
fcw.query_channel(enrollObj, opts, cb);
};
// Other -------------------------------------------------------------------------------
// Format Owner's Actual Key Name
function build_owner_name(username, company) {
return username.toLowerCase() + '.' + company;
}
// random string of x length
function randStr(length) {
var text = '';
var possible = 'abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHJKMNPQRSTUVWXYZ';
for (var i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
// left pad string with "0"s
function leftPad(str, length) {
for (var i = str.length; i < length; i++) str = '0' + String(str);
return str;
}
return marbles_chaincode;
};