Skip to content

Commit

Permalink
[FAB-4306] signatures not able to save
Browse files Browse the repository at this point in the history
Allow signatures to be strings and convert
them into protobuf ConfigSignature when
needed.

Change-Id: I904d9cad3017e7318b520585af7c96bb54b07c52
Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob committed Jun 2, 2017
1 parent a494295 commit af194a8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
23 changes: 22 additions & 1 deletion fabric-client/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ var Client = class {
let proto_config_signature = new _configtxProto.ConfigSignature();
proto_config_signature.setSignatureHeader(signature_header_bytes);
proto_config_signature.setSignature(signature_bytes);

return proto_config_signature;
}

Expand Down Expand Up @@ -522,7 +523,8 @@ var Client = class {
logger.debug('_createOrUpdateChannel - have config_update');
var proto_config_Update_envelope = new _configtxProto.ConfigUpdateEnvelope();
proto_config_Update_envelope.setConfigUpdate(request.config);
proto_config_Update_envelope.setSignatures(request.signatures);
var signatures = _stringToSignature(request.signatures);
proto_config_Update_envelope.setSignatures(signatures);

var proto_channel_header = Channel._buildChannelHeader(
_commonProto.HeaderType.CONFIG_UPDATE,
Expand Down Expand Up @@ -1301,4 +1303,23 @@ function _getChaincodePackageData(request, devMode) {
});
}

// internal utility method to check and convert any strings to protobuf signatures
function _stringToSignature(string_signatures) {
var signatures = [];
for(var i in string_signatures) {
let signature = string_signatures[i];
// check for properties rather than object type
if(signature && signature.signature_header && signature.signature) {
logger.debug('_stringToSignature - signature is protobuf');
}
else {
logger.debug('_stringToSignature - signature is string');
var signature_bytes = Buffer.from(signature, 'hex');
signature = _configtxProto.ConfigSignature.decode(signature_bytes);
}
signatures.push(signature);
}
return signatures;
}

module.exports = Client;
7 changes: 5 additions & 2 deletions test/integration/e2e/create-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,16 @@ test('\n\n***** SDK Built config update create flow *****\n\n', function(t) {

// sign the config
var signature = client.signChannelConfig(config);
// convert signature to a storable string
// fabric-client SDK will convert back during create
var string_signature = signature.toBuffer().toString('hex');
t.pass('Successfully signed config update');
// collect signature from org1 admin
// TODO: signature counting against policies on the orderer
// at the moment is being investigated, but it requires this
// weird double-signature from each org admin
signatures.push(signature);
signatures.push(signature);
signatures.push(string_signature);
signatures.push(string_signature);

// make sure we do not reuse the user
client._userContext = null;
Expand Down
30 changes: 30 additions & 0 deletions test/unit/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var testutil = require('./util.js');

var caImport;

var grpc = require('grpc');
var _configtxProto = grpc.load(__dirname + '/../../fabric-client/lib/protos/common/configtx.proto').common;
var rewire = require('rewire');
var ClientRewired = rewire('fabric-client/lib/Client.js');

test('\n\n ** index.js **\n\n', function (t) {
t.equals(typeof hfc, 'function');

Expand Down Expand Up @@ -1074,3 +1079,28 @@ test('\n\n ** test related APIs for create channel **\n\n', function (t) {

t.end();
});

test('\n\n ** test internal method to rebuild ConfigSignatures **\n\n', function (t) {
var some_proto_signatures = [];
var proto_config_signature = new _configtxProto.ConfigSignature();
proto_config_signature.setSignatureHeader(Buffer.from('signature_header_bytes'));
proto_config_signature.setSignature(Buffer.from('signature_bytes'));
some_proto_signatures.push(proto_config_signature);
var string_config_signature = proto_config_signature.toBuffer().toString('hex');
some_proto_signatures.push(string_config_signature);

var _stringToSignature = ClientRewired.__get__('_stringToSignature');
var all_proto_signatures = _stringToSignature(some_proto_signatures);
for(let i in all_proto_signatures) {
var start_header = proto_config_signature.getSignatureHeader().toBuffer().toString();
var start_sig = proto_config_signature.getSignature().toBuffer().toString();
var decode_header = all_proto_signatures[i].getSignatureHeader().toBuffer().toString();
var decode_sig = all_proto_signatures[i].getSignature().toBuffer().toString();
logger.info(' headers are ==> %s :: %s', start_header, decode_header);
logger.info(' signatures are ==> %s :: %s', start_sig, decode_sig);

t.equals(start_header, decode_header, 'check signature headers are the same');
t.equals(start_sig, decode_sig, 'check signatures are the same');
}
t.end();
});

0 comments on commit af194a8

Please sign in to comment.