Skip to content

Commit 07b18fc

Browse files
committed
- added optional username for secure context
1 parent a7171e4 commit 07b18fc

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,30 +291,42 @@ Ex:
291291
- Chaincode functions are dependent on actually be found inside your Go chaincode
292292
- My advise is to build your chaincode off of the Marble Application one. This way you get the basic CRUD functions below:
293293

294-
### chaincode.read(name, [callback])
294+
### chaincode.read(name, [username], [callback])
295295
Read variable named name from chaincode state.
296296
This will call the `Query()` function in the Go chaincode, therefore the `Query()` function needs to exists in the cc.
297297
The variable name will be passed as `arg[0]` to `Query()`.
298+
The `username` parameter should be the desired secure context username that has already been registered against the selected peer.
299+
If left `null` the SDK will use a known username for the selected peer. (this is only relevant in a permissioned network)
298300

299-
### chaincode.query(args, [callback])
301+
### chaincode.query(args, [username], [callback])
300302
This will call the query function with custom input arguments.
301303
Usually "args" is an array of strings.
304+
The `username` parameter should be the desired secure context username that has already been registered against the selected peer.
305+
If left `null` the SDK will use a known username for the selected peer. (this is only relevant in a permissioned network)
302306

303-
### chaincode.write(name, val, [callback])
307+
### chaincode.write(name, val, [username], [callback])
304308
Write 'val' to variable named 'name'. This will call the `write()` function in the Go chaincode, therefore the `write()` function needs to exists in the cc.
309+
The `username` parameter should be the desired secure context username that has already been registered against the selected peer.
310+
If left `null` the SDK will use a known username for the selected peer. (this is only relevant in a permissioned network)
305311

306-
### chaincode.remove(name, [callback])
312+
### chaincode.remove(name, [username], [callback])
307313
Delete variable named 'name'. This will call the `delete()` function in the Go chaincode, therefore the `delete()` function needs to exists in the cc.
314+
The `username` parameter should be the desired secure context username that has already been registered against the selected peer.
315+
If left `null` the SDK will use a known username for the selected peer. (this is only relevant in a permissioned network)
308316

309-
### chaincode.deploy(func, args, [save_path], [callback])
317+
### chaincode.deploy(func, args, [save_path], [username], [callback])
310318
Deploy the chaincode.
311319
Call GoLang function named 'func' and feed it 'args'.
312320
Optionally save [Chaincode Summary File](#ccsf) to 'save_path'.
313321
Usualy "args" is an array of strings.
322+
The `username` parameter should be the desired secure context username that has already been registered against the selected peer.
323+
If left `null` the SDK will use a known username for the selected peer. (this is only relevant in a permissioned network)
314324

315-
### chaincode.CUSTOM_FUNCTION_NAME(args, [callback])
325+
### chaincode.CUSTOM_FUNCTION_NAME(args, [username], [callback])
316326
Will invoke your Go function CUSTOM_FUNCTION_NAME and pass it 'arg'.
317327
Usualy "args" is an array of strings.
328+
The `username` parameter should be the desired secure context username that has already been registered against the selected peer.
329+
If left `null` the SDK will use a known username for the selected peer. (this is only relevant in a permissioned network)
318330

319331
***
320332
***

index.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,11 @@ ibc.prototype.block_stats = function(id, cb){
448448
//read() - read generic variable from chaincode state
449449
//============================================================================================================================
450450
function read(name, username, cb){
451-
if(typeof username === 'function' || (username == null && cb == null)) {
452-
cb = username; //cb is in 2nd param OR null use known username
451+
if(typeof username === 'function'){ //if cb is in 2nd param use known username
452+
cb = username;
453+
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
454+
}
455+
if(username == null) { //if username not provided, use known valid one
453456
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
454457
}
455458

@@ -485,10 +488,14 @@ function read(name, username, cb){
485488
//query() - read generic variable from chaincode state
486489
//============================================================================================================================
487490
function query(args, username, cb){
488-
if(typeof username === 'function' || (username == null && cb == null)) {
489-
cb = username; //cb is in 2nd param OR null use known username
491+
if(typeof username === 'function'){ //if cb is in 2nd param use known username
492+
cb = username;
490493
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
491494
}
495+
if(username == null) { //if username not provided, use known valid one
496+
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
497+
}
498+
492499
var options = {
493500
path: '/devops/query'
494501
};
@@ -521,10 +528,14 @@ function query(args, username, cb){
521528
//write() - write generic variable to chaincode state
522529
//============================================================================================================================
523530
function write(name, val, username, cb){
524-
if(typeof username === 'function' || (username == null && cb == null)) {
525-
cb = username; //cb is in 2nd param OR null use known username
531+
if(typeof username === 'function'){ //if cb is in 2nd param use known username
532+
cb = username;
526533
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
527534
}
535+
if(username == null) { //if username not provided, use known valid one
536+
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
537+
}
538+
528539
var options = {
529540
path: '/devops/invoke'
530541
};
@@ -558,10 +569,14 @@ function write(name, val, username, cb){
558569
//remove() - delete a generic variable from chaincode state
559570
//============================================================================================================================
560571
function remove(name, username, cb){
561-
if(typeof username === 'function' || (username == null && cb == null)) {
562-
cb = username; //cb is in 2nd param OR null use known username
572+
if(typeof username === 'function'){ //if cb is in 2nd param use known username
573+
cb = username;
563574
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
564575
}
576+
if(username == null) { //if username not provided, use known valid one
577+
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
578+
}
579+
565580
var options = {
566581
path: '/devops/invoke'
567582
};
@@ -626,10 +641,14 @@ ibc.prototype.register = function(index, enrollID, enrollSecret, cb) {
626641
//deploy() - deploy chaincode and call a cc function
627642
//============================================================================================================================
628643
function deploy(func, args, save_path, username, cb){
629-
if(typeof username === 'function' || (username == null && cb == null)) {
630-
cb = username; //cb is in 2nd param OR null use known username
644+
if(typeof username === 'function'){ //if cb is in 2nd param use known username
645+
cb = username;
646+
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
647+
}
648+
if(username == null) { //if username not provided, use known valid one
631649
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
632650
}
651+
633652
console.log('[ibc-js] Deploying Chaincode - Starting');
634653
console.log('[ibc-js] \tfunction:', func, ', arg:', args);
635654
console.log('\n\n\t Waiting...'); //this can take awhile
@@ -728,10 +747,14 @@ function build_chaincode_func(name){
728747
console.log('[ibc-js] Found cc function: ', name);
729748
ibc.chaincode.details.func.push(name);
730749
ibc.chaincode[name] = function(args, username, cb){ //create the function in the chaincode obj
731-
if(typeof username === 'function' || (username == null && cb == null)) {
732-
cb = username; //cb is in 2nd param OR null use known username
750+
if(typeof username === 'function'){ //if cb is in 2nd param use known username
751+
cb = username;
752+
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
753+
}
754+
if(username == null) { //if username not provided, use known valid one
733755
username = ibc.chaincode.details.peers[ibc.selectedPeer].user;
734756
}
757+
735758
var options = {path: '/devops/invoke'};
736759
var body = {
737760
chaincodeSpec: {

0 commit comments

Comments
 (0)