Skip to content

Commit f499e22

Browse files
author
Simon Stone
committed
[FAB-16496] Port to protobufjs 6
- 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
1 parent 94ab998 commit f499e22

File tree

20 files changed

+343
-699
lines changed

20 files changed

+343
-699
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bundle.js

build/test/network/docker-compose/docker-compose-base.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ services:
120120
- /var/run/:/host/var/run/
121121
- ../../../../test:/opt/gopath/src/github.com/chaincode
122122
- ../crypto-material/:/etc/hyperledger/configtx/
123-
- ../crypto-material:/etc/hyperledger/config
124123
- ../../../../test/fixtures:/etc/hyperledger/fixtures
125124

126125
couchdb:

fabric-shim-crypto/lib/enc-sign.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ class ChaincodeCryptoLibrary {
4040
if (!iv) {
4141
// transaction proposal did not include an IV, generate one
4242
iv = crypto.randomBytes(16);
43-
} else { // 128-bit IV for AES (block size)
44-
iv = iv.toBuffer();
4543
}
4644

4745
if (key) {
48-
this.cipher = crypto.createCipheriv(ALGORITHM, key.toBuffer(), iv);
49-
this.decipher = crypto.createDecipheriv(ALGORITHM, key.toBuffer(), iv);
46+
this.cipher = crypto.createCipheriv(ALGORITHM, key, iv);
47+
this.decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
5048
}
5149

5250
const signKey = tmap.get(SIGN_KEY);
5351
if (signKey) {
54-
this.signKey = importKey(signKey.toBuffer());
52+
this.signKey = importKey(signKey);
5553
this._ecdsa = new EC(elliptic.curves.p256);
5654
}
5755
}

fabric-shim-crypto/test/shim-crypto.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,17 @@ const ECDSAKey = ShimCrypto.__get__('ECDSAKey');
2121
const mapItems = {};
2222
mapItems.iv = {
2323
key: INIT_VECTOR,
24-
value: {
25-
toBuffer: () => {
26-
return Buffer.from('0123456789012345');
27-
}
28-
}
24+
value: Buffer.from('0123456789012345')
2925
};
3026

3127
mapItems.encryptKey = {
3228
key: ENCRYPT_KEY,
33-
value: {
34-
toBuffer: () => {
35-
return Buffer.from('01234567890123456789012345678901');
36-
}
37-
}
29+
value: Buffer.from('01234567890123456789012345678901')
3830
};
3931

4032
mapItems.signKey = {
4133
key: SIGN_KEY,
42-
value: {
43-
toBuffer: () => {
44-
return Buffer.from('some signKey');
45-
}
46-
}
34+
value: Buffer.from('some signKey')
4735
};
4836

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

8674
expect(mockCreateCipher.calledOnce).to.be.ok;
87-
expect(mockCreateCipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value.toBuffer(), mapItems.iv.value.toBuffer()]);
75+
expect(mockCreateCipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value, mapItems.iv.value]);
8876
expect(mockCreateDecipher.calledOnce).to.be.ok;
89-
expect(mockCreateDecipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value.toBuffer(), mapItems.iv.value.toBuffer()]);
77+
expect(mockCreateDecipher.firstCall.args).to.deep.equal([ALGORITHM, mapItems.encryptKey.value, mapItems.iv.value]);
9078
});
9179

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

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

123111
expect(mockImportKey.calledOnce).to.be.ok;
124-
expect(mockImportKey.firstCall.args).to.deep.equal([mapItems.signKey.value.toBuffer()]);
112+
expect(mockImportKey.firstCall.args).to.deep.equal([mapItems.signKey.value]);
125113
expect(mockEC.calledOnce).to.be.ok;
126114
expect(mockEC.firstCall.args).to.deep.equal([elliptic.curves.p256]);
127115

fabric-shim/lib/chaincode.js

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
/* eslint-disable no-useless-escape */
77
'use strict';
88

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

2625
const yargs = require('yargs');
2726

28-
const _chaincodeProto = ProtoLoader.load({
29-
root: path.join(__dirname, './protos'),
30-
file: 'peer/chaincode.proto'
31-
}).protos;
32-
33-
const _serviceProto = ProtoLoader.load({
34-
root: path.join(__dirname, './protos'),
35-
file: 'peer/chaincode_shim.proto'
36-
}).protos;
37-
38-
const _responseProto = ProtoLoader.load({
39-
root: path.join(__dirname, './protos'),
40-
file: 'peer/proposal_response.proto'
41-
}).protos;
42-
4327
/**
4428
* Chaincodes must implement the methods in this interface. The Init() method is called during
4529
* chaincode <code>instantiation</code> or <code>upgrade</code> to preform any necessary intitialization
@@ -138,14 +122,15 @@ class Shim {
138122

139123
const chaincodeName = opts['chaincode-id-name'];
140124
const client = new Handler(chaincode, url, optsCpy);
141-
const chaincodeID = new _chaincodeProto.ChaincodeID();
142-
chaincodeID.setName(chaincodeName);
125+
const chaincodeID = {
126+
name: chaincodeName
127+
};
143128

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

146131
client.chat({
147-
type: _serviceProto.ChaincodeMessage.Type.REGISTER,
148-
payload: chaincodeID.toBuffer()
132+
type: fabprotos.protos.ChaincodeMessage.Type.REGISTER,
133+
payload: fabprotos.protos.ChaincodeID.encode(chaincodeID).finish()
149134
});
150135

151136
// return the client object to give the calling code
@@ -168,11 +153,10 @@ class Shim {
168153
* @returns {SuccessResponse}
169154
*/
170155
static success(payload) {
171-
const ret = new _responseProto.Response();
172-
ret.status = ChaincodeStub.RESPONSE_CODE.OK;
173-
ret.payload = payload ? payload : Buffer.from('');
174-
175-
return ret;
156+
return {
157+
status: ChaincodeStub.RESPONSE_CODE.OK,
158+
payload: payload ? payload : Buffer.from('')
159+
};
176160
}
177161

178162
/**
@@ -190,11 +174,10 @@ class Shim {
190174
* @returns {ErrorResponse}
191175
*/
192176
static error(msg) {
193-
const ret = new _responseProto.Response();
194-
ret.status = ChaincodeStub.RESPONSE_CODE.ERROR;
195-
ret.message = msg;
196-
197-
return ret;
177+
return {
178+
status: ChaincodeStub.RESPONSE_CODE.ERROR,
179+
message: msg
180+
};
198181
}
199182

200183
/**
@@ -245,9 +228,9 @@ class ClientIdentity {
245228
this.stub = stub;
246229
const signingId = stub.getCreator();
247230

248-
this.mspId = signingId.getMspid();
231+
this.mspId = signingId.mspid;
249232

250-
this.idBytes = signingId.getIdBytes().toBuffer();
233+
this.idBytes = signingId.idBytes;
251234
const normalizedCert = normalizeX509(this.idBytes.toString(), loggerPrefix);
252235

253236
// assemble the unique ID based on certificate

0 commit comments

Comments
 (0)