Skip to content

Commit 5a90e4b

Browse files
committed
protect against deletion
1 parent 2a78f9c commit 5a90e4b

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

chaincode/lib_marbles.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ func get_complete_marble_index(stub shim.ChaincodeStubInterface) ([]string, erro
8686
func delete_marble(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
8787
fmt.Println("starting delete_marble")
8888

89-
if len(args) != 1 {
90-
return nil, errors.New("Incorrect number of arguments. Expecting 1")
89+
if len(args) != 2 {
90+
return nil, errors.New("Incorrect number of arguments. Expecting 2")
9191
}
9292

9393
name := args[0]
94+
authed_by_company := args[1]
9495

9596
//get the marble
9697
marble, err := get_marble(stub, name)
@@ -99,6 +100,11 @@ func delete_marble(stub shim.ChaincodeStubInterface, args []string) ([]byte, err
99100
return nil, err
100101
}
101102

103+
//check authorizing company
104+
if marble.Owner.Company != authed_by_company{
105+
return nil, errors.New("The company '" + authed_by_company + "' cannot authorize deletion for '" + marble.Owner.Company + "'.")
106+
}
107+
102108
//remove the marble
103109
err = stub.DelState(name) //remove the key from chaincode state
104110
if err != nil {

utils/marbles_cc_lib/marbles.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = function (chain, chaincode_id, logger) {
2525
function (results) {
2626
var proposalResponses = results[0];
2727
var proposal = results[1];
28-
if (proposalResponses[0].response.status === 200) {
28+
if (proposalResponses && proposalResponses[0] && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
2929
console.log('Successfully obtained transaction endorsement.' + JSON.stringify(proposalResponses));
3030
if(ws) ws.send(JSON.stringify({msg: 'tx_step', state: 'ordering'}));
3131
return webUser.sendTransaction(proposalResponses, proposal);
@@ -158,13 +158,13 @@ module.exports = function (chain, chaincode_id, logger) {
158158
function (results) {
159159
var proposalResponses = results[0];
160160
var proposal = results[1];
161-
if (proposalResponses[0] && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
161+
if (proposalResponses && proposalResponses[0] && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
162162
console.log('Successfully obtained transaction endorsement.' + JSON.stringify(proposalResponses));
163163
if(ws) ws.send(JSON.stringify({msg: 'tx_step', state: 'ordering'}));
164164
return webUser.sendTransaction(proposalResponses, proposal);
165165
}
166166
else {
167-
console.log('Failed to obtain transaction endorsement. Error msg: ', proposalResponses[0]);
167+
console.log('Failed to obtain transaction endorsement', proposalResponses);
168168
if(ws) ws.send(JSON.stringify({msg: 'tx_step', state: 'endorsing_failed'}));
169169
throw common.format_error_msg(proposalResponses[0]);
170170
}
@@ -218,13 +218,14 @@ module.exports = function (chain, chaincode_id, logger) {
218218
function (results) {
219219
var proposalResponses = results[0];
220220
var proposal = results[1];
221-
if (proposalResponses[0].response.status === 200) {
221+
if (proposalResponses && proposalResponses[0] && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
222222
console.log('Successfully obtained transaction endorsement.' + JSON.stringify(proposalResponses));
223223
if(ws) ws.send(JSON.stringify({msg: 'tx_step', state: 'ordering'}));
224224
return webUser.sendTransaction(proposalResponses, proposal);
225225
}
226226
else {
227-
console.log('Failed to obtain transaction endorsement. Error code: ' + proposalResponses[0].response.status);
227+
console.log('Failed to obtain transaction endorsement', proposalResponses);
228+
if(ws) ws.send(JSON.stringify({msg: 'tx_step', state: 'endorsing_failed'}));
228229
throw common.format_error_msg(proposalResponses[0]);
229230
}
230231
}
@@ -237,6 +238,7 @@ module.exports = function (chain, chaincode_id, logger) {
237238
}
238239
else {
239240
console.log('Failed to order the endorsement of the transaction.');
241+
if(ws) ws.send(JSON.stringify({msg: 'tx_step', state: 'ordering_failed'}));
240242
throw response;
241243
}
242244
}
@@ -245,6 +247,7 @@ module.exports = function (chain, chaincode_id, logger) {
245247
console.log('error in catch block', typeof err, err);
246248
var e = null;
247249
if(typeof err === 'string'){ //only pass these errors until we fix it
250+
if(err.indexOf('cannot authorize')) e = err;
248251
if(err.indexOf('Marble does not exist')) e = err;
249252
if(err.indexOf('Incorrect number of arguments')) e = err;
250253
if(err.indexOf('Owner does not exist')) e = err;

utils/websocket_server_side.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ module.exports = function (checkPerodically, marbles_lib, logger) {
3434
if(data.type == 'create'){
3535
console.log('[ws] create marbles req');
3636
options = [data.name, data.color, data.size, data.username, data.company];
37-
marbles_lib.create_a_marble(webUser, [hfc.getPeer(helper.getPeersUrl(0))], ws, options, function(){
38-
37+
marbles_lib.create_a_marble(webUser, [hfc.getPeer(helper.getPeersUrl(0))], ws, options, function(err, resp){
38+
if(err != null) send_err(err, data);
3939
});
4040
}
4141

@@ -58,8 +58,9 @@ module.exports = function (checkPerodically, marbles_lib, logger) {
5858
//delete marble
5959
else if(data.type == 'delete_marble'){
6060
console.log('[ws] delete marble req');
61-
marbles_lib.delete_marble(webUser, [hfc.getPeer(helper.getPeersUrl(0))], ws, [data.name], function(err, resp){
62-
61+
options = [data.name, process.env.marble_company];
62+
marbles_lib.delete_marble(webUser, [hfc.getPeer(helper.getPeersUrl(0))], ws, options, function(err, resp){
63+
if(err != null) send_err(err, data);
6364
});
6465
}
6566

0 commit comments

Comments
 (0)