Skip to content

Commit

Permalink
[FAB-16496] Port to protobufjs 6
Browse files Browse the repository at this point in the history
- Use protobufjs 6 for message handling
- Use @grpc/proto-loader and grpc for RPC

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
Change-Id: I5916fb0f14c6159f7b569b9ad5900b433df8624b
  • Loading branch information
Simon Stone committed Sep 6, 2019
1 parent 94ab998 commit f499e22
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 699 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bundle.js
1 change: 0 additions & 1 deletion build/test/network/docker-compose/docker-compose-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ services:
- /var/run/:/host/var/run/
- ../../../../test:/opt/gopath/src/github.com/chaincode
- ../crypto-material/:/etc/hyperledger/configtx/
- ../crypto-material:/etc/hyperledger/config
- ../../../../test/fixtures:/etc/hyperledger/fixtures

couchdb:
Expand Down
8 changes: 3 additions & 5 deletions fabric-shim-crypto/lib/enc-sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@ class ChaincodeCryptoLibrary {
if (!iv) {
// transaction proposal did not include an IV, generate one
iv = crypto.randomBytes(16);
} else { // 128-bit IV for AES (block size)
iv = iv.toBuffer();
}

if (key) {
this.cipher = crypto.createCipheriv(ALGORITHM, key.toBuffer(), iv);
this.decipher = crypto.createDecipheriv(ALGORITHM, key.toBuffer(), iv);
this.cipher = crypto.createCipheriv(ALGORITHM, key, iv);
this.decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
}

const signKey = tmap.get(SIGN_KEY);
if (signKey) {
this.signKey = importKey(signKey.toBuffer());
this.signKey = importKey(signKey);
this._ecdsa = new EC(elliptic.curves.p256);
}
}
Expand Down
28 changes: 8 additions & 20 deletions fabric-shim-crypto/test/shim-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,17 @@ const ECDSAKey = ShimCrypto.__get__('ECDSAKey');
const mapItems = {};
mapItems.iv = {
key: INIT_VECTOR,
value: {
toBuffer: () => {
return Buffer.from('0123456789012345');
}
}
value: Buffer.from('0123456789012345')
};

mapItems.encryptKey = {
key: ENCRYPT_KEY,
value: {
toBuffer: () => {
return Buffer.from('01234567890123456789012345678901');
}
}
value: Buffer.from('01234567890123456789012345678901')
};

mapItems.signKey = {
key: SIGN_KEY,
value: {
toBuffer: () => {
return Buffer.from('some signKey');
}
}
value: Buffer.from('some signKey')
};

const saveImportKey = ShimCrypto.__get__('importKey');
Expand Down Expand Up @@ -84,9 +72,9 @@ describe('enc-sign', () => {
expect(sc._ecdsa).to.be.undefined;

expect(mockCreateCipher.calledOnce).to.be.ok;
expect(mockCreateCipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value.toBuffer(), mapItems.iv.value.toBuffer()]);
expect(mockCreateCipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value, mapItems.iv.value]);
expect(mockCreateDecipher.calledOnce).to.be.ok;
expect(mockCreateDecipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value.toBuffer(), mapItems.iv.value.toBuffer()]);
expect(mockCreateDecipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value, mapItems.iv.value]);
});

it ('should set key values when init vector not in map', () => {
Expand All @@ -105,9 +93,9 @@ describe('enc-sign', () => {
expect(mockRandomBytes.calledOnce).to.be.ok;
expect(mockRandomBytes.firstCall.args).to.deep.equal([16]);
expect(mockCreateCipher.calledOnce).to.be.ok;
expect(mockCreateCipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value.toBuffer(), 'some random bytes']);
expect(mockCreateCipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value, 'some random bytes']);
expect(mockCreateDecipher.calledOnce).to.be.ok;
expect(mockCreateDecipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value.toBuffer(), 'some random bytes']);
expect(mockCreateDecipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value, 'some random bytes']);
});

it ('should set sign key values', () => {
Expand All @@ -121,7 +109,7 @@ describe('enc-sign', () => {
expect(sc._ecdsa).to.deep.equal(ecStubInstance);

expect(mockImportKey.calledOnce).to.be.ok;
expect(mockImportKey.firstCall.args).to.deep.equal([mapItems.signKey.value.toBuffer()]);
expect(mockImportKey.firstCall.args).to.deep.equal([mapItems.signKey.value]);
expect(mockEC.calledOnce).to.be.ok;
expect(mockEC.firstCall.args).to.deep.equal([elliptic.curves.p256]);

Expand Down
49 changes: 16 additions & 33 deletions fabric-shim/lib/chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
/* eslint-disable no-useless-escape */
'use strict';

const ProtoLoader = require('./protoloader');
const path = require('path');
const fabprotos = require('../bundle');
const util = require('util');
const {Certificate} = require('@fidm/x509');
const Logger = require('./logger');
Expand All @@ -25,21 +24,6 @@ const StartCommand = require('./cmds/startCommand.js');

const yargs = require('yargs');

const _chaincodeProto = ProtoLoader.load({
root: path.join(__dirname, './protos'),
file: 'peer/chaincode.proto'
}).protos;

const _serviceProto = ProtoLoader.load({
root: path.join(__dirname, './protos'),
file: 'peer/chaincode_shim.proto'
}).protos;

const _responseProto = ProtoLoader.load({
root: path.join(__dirname, './protos'),
file: 'peer/proposal_response.proto'
}).protos;

/**
* Chaincodes must implement the methods in this interface. The Init() method is called during
* chaincode <code>instantiation</code> or <code>upgrade</code> to preform any necessary intitialization
Expand Down Expand Up @@ -138,14 +122,15 @@ class Shim {

const chaincodeName = opts['chaincode-id-name'];
const client = new Handler(chaincode, url, optsCpy);
const chaincodeID = new _chaincodeProto.ChaincodeID();
chaincodeID.setName(chaincodeName);
const chaincodeID = {
name: chaincodeName
};

logger.info(util.format('Registering with peer %s as chaincode "%s"', opts['peer.address'], chaincodeName));

client.chat({
type: _serviceProto.ChaincodeMessage.Type.REGISTER,
payload: chaincodeID.toBuffer()
type: fabprotos.protos.ChaincodeMessage.Type.REGISTER,
payload: fabprotos.protos.ChaincodeID.encode(chaincodeID).finish()
});

// return the client object to give the calling code
Expand All @@ -168,11 +153,10 @@ class Shim {
* @returns {SuccessResponse}
*/
static success(payload) {
const ret = new _responseProto.Response();
ret.status = ChaincodeStub.RESPONSE_CODE.OK;
ret.payload = payload ? payload : Buffer.from('');

return ret;
return {
status: ChaincodeStub.RESPONSE_CODE.OK,
payload: payload ? payload : Buffer.from('')
};
}

/**
Expand All @@ -190,11 +174,10 @@ class Shim {
* @returns {ErrorResponse}
*/
static error(msg) {
const ret = new _responseProto.Response();
ret.status = ChaincodeStub.RESPONSE_CODE.ERROR;
ret.message = msg;

return ret;
return {
status: ChaincodeStub.RESPONSE_CODE.ERROR,
message: msg
};
}

/**
Expand Down Expand Up @@ -245,9 +228,9 @@ class ClientIdentity {
this.stub = stub;
const signingId = stub.getCreator();

this.mspId = signingId.getMspid();
this.mspId = signingId.mspid;

this.idBytes = signingId.getIdBytes().toBuffer();
this.idBytes = signingId.idBytes;
const normalizedCert = normalizeX509(this.idBytes.toString(), loggerPrefix);

// assemble the unique ID based on certificate
Expand Down
Loading

0 comments on commit f499e22

Please sign in to comment.