Skip to content

Commit

Permalink
[FAB-9402] key.js extends api.js explicitly
Browse files Browse the repository at this point in the history
Clean up:
- remove static isInstance method, user should use instanceOf
- simplify null of falsy checking in key.js
- refactor some ECDSA suite function to be async/await

Change-Id: I819a500747a7d84c0901652ebdb36cf96e5e2cf7
Signed-off-by: davidliu <[email protected]>
  • Loading branch information
davidkhala committed May 30, 2018
1 parent a851829 commit 3a9087f
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 309 deletions.
6 changes: 3 additions & 3 deletions fabric-ca-client/lib/FabricCAClientImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ var FabricCAClient = class {
socket.setTimeout(CONNECTION_TIMEOUT);
socket.on('timeout', () => {
request.abort();
reject(new Error(util.format('Calling %s endpoint failed, CONNECTION Timeout', api_method)));
reject(new Error(util.format('Calling %s endpoint failed, CONNECTION Timeout', api_method)));
});
});

Expand All @@ -761,9 +761,9 @@ var FabricCAClient = class {
request.setTimeout(SO_TIMEOUT, () => {
reject(new Error(util.format('Calling %s endpoint failed, READ Timeout', api_method)));
});
}
}

request.on('error', function (err) {
request.on('error', function (err) {
reject(new Error(util.format('Calling %s endpoint failed with error [%s]', api_method, err)));
});

Expand Down
2 changes: 1 addition & 1 deletion fabric-ca-client/lib/IdentityService.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class IdentityService {
throw new Error('Can not get signingIdentity from registrar');
}

const url = 'identities/' + enrollmentID + '?ca='+this.client._caName;;
const url = 'identities/' + enrollmentID + '?ca='+this.client._caName;
return this.client.get(url, signingIdentity);
}

Expand Down
22 changes: 10 additions & 12 deletions fabric-client/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ const Channel = class {
* @param {string} name - The name of the peer assigned to this channel
* @returns {ChannelPeer} The ChannelPeer instance
*/
getChannelPeer(name) {
const channel_peer = this._channel_peers.get(name);
getChannelPeer(name) {
const channel_peer = this._channel_peers.get(name);

if(!channel_peer){
throw new Error(util.format(PEER_NOT_ASSIGNED_MSG, name));
}
if(!channel_peer){
throw new Error(util.format(PEER_NOT_ASSIGNED_MSG, name));
}

return channel_peer;
}
return channel_peer;
}

/**
* Returns a list of peers assigned to this channel instance.
Expand Down Expand Up @@ -2273,8 +2273,6 @@ const Channel = class {
return Promise.all(promises);
}
return Promise.reject('Failed to execute transaction: ' + errorMsg);
}).catch(error => {
return Promise.reject(error);
});
}

Expand Down Expand Up @@ -2878,9 +2876,9 @@ var ChannelPeer = class {
*
* @returns {string} The organization name.
*/
getOrganizationName() {
return this._org_name;
}
getOrganizationName() {
return this._org_name;
}

/**
* Get the name. This is a client-side only identifier for this
Expand Down
2 changes: 1 addition & 1 deletion fabric-client/lib/impl/CryptoKeyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var CryptoKeyStoreMixin = (KeyValueStore) => class extends KeyValueStore {
// next try the public key entry
return self.getValue(_getKeyIndex(ski, false));
}).then((key) => {
if (ECDSAKey.isInstance(key))
if (key instanceof ECDSAKey)
return key;

if (key !== null) {
Expand Down
118 changes: 48 additions & 70 deletions fabric-client/lib/impl/CryptoSuite_ECDSA_AES.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const logger = utils.getLogger('crypto_ecdsa_aes');
class CryptoSuite_ECDSA_AES extends api.CryptoSuite {

/**
* constructor
*
* @param {number} keySize Key size for the ECDSA algorithm, can only be 256 or 384
* @param {string} hash Optional. Hash algorithm, supported values are "SHA2" and "SHA3"
*/
* constructor
*
* @param {number} keySize Key size for the ECDSA algorithm, can only be 256 or 384
* @param {string} hash Optional. Hash algorithm, supported values are "SHA2" and "SHA3"
*/
constructor(keySize, hash) {
if (!keySize) throw new Error('keySize must be specified');
if (keySize !== 256 && keySize !== 384) {
Expand Down Expand Up @@ -77,68 +77,57 @@ class CryptoSuite_ECDSA_AES extends api.CryptoSuite {
}

/**
* Set the cryptoKeyStore.
*
* When the application needs to use a key store other than the default,
* it should use the {@link Client} newCryptoKeyStore to create an instance and
* use this function to set the instance on the CryptoSuite.
*
* @param {CryptoKeyStore} cryptoKeyStore The cryptoKeyStore.
*/
* Set the cryptoKeyStore.
*
* When the application needs to use a key store other than the default,
* it should use the {@link Client} newCryptoKeyStore to create an instance and
* use this function to set the instance on the CryptoSuite.
*
* @param {CryptoKeyStore} cryptoKeyStore The cryptoKeyStore.
*/
setCryptoKeyStore(cryptoKeyStore) {
this._cryptoKeyStore = cryptoKeyStore;
}

generateKey(opts) {
async generateKey(opts) {
const pair = KEYUTIL.generateKeypair('EC', this._curveName);

if (typeof opts !== 'undefined' && typeof opts.ephemeral !== 'undefined' && opts.ephemeral === true) {
logger.debug('generateKey, ephemeral true, Promise resolved');
return Promise.resolve(new ECDSAKey(pair.prvKeyObj));
return new ECDSAKey(pair.prvKeyObj);
} else {
if (!this._cryptoKeyStore) {
throw new Error('generateKey opts.ephemeral is false, which requires CryptoKeyStore to be set.');
}
// unless "opts.ephemeral" is explicitly set to "true", default to saving the key
const key = new ECDSAKey(pair.prvKeyObj);

const self = this;
return new Promise((resolve, reject) => {
self._cryptoKeyStore._getKeyStore()
.then((store) => {
logger.debug('generateKey, store.setValue');
return store.putKey(key)
.then(() => {
return resolve(key);
}).catch((err) => {
reject(err);
});
});

});
const store = await this._cryptoKeyStore._getKeyStore();
logger.debug('generateKey, store.setValue');
await store.putKey(key);
return key;
}
}

/**
* This is an implementation of {@link module:api.CryptoSuite#deriveKey}
* To be implemented
*/
* This is an implementation of {@link module:api.CryptoSuite#deriveKey}
* To be implemented
*/
deriveKey(key, opts) {
if (key || opts) ;
throw new Error('Not implemented yet');
}

/**
* This is an implementation of {@link module:api.CryptoSuite#importKey}
* To be implemented
*/
* This is an implementation of {@link module:api.CryptoSuite#importKey}
*/
importKey(pem, opts) {
logger.debug('importKey - start');
let store_key = true; //default
if (typeof opts !== 'undefined' && typeof opts.ephemeral !== 'undefined' && opts.ephemeral === true) {
store_key = false;
}
if (!!store_key && !this._cryptoKeyStore) {
if (store_key && !this._cryptoKeyStore) {
throw new Error('importKey opts.ephemeral is false, which requires CryptoKeyStore to be set.');
}

Expand Down Expand Up @@ -196,46 +185,35 @@ class CryptoSuite_ECDSA_AES extends api.CryptoSuite {
}
}

getKey(ski) {
const self = this;
let store;
async getKey(ski) {

if (!self._cryptoKeyStore) {
if (!this._cryptoKeyStore) {
throw new Error('getKey requires CryptoKeyStore to be set.');
}
return new Promise((resolve, reject) => {
self._cryptoKeyStore._getKeyStore()
.then((st) => {
store = st;
return store.getKey(ski);
}).then((key) => {
if (ECDSAKey.isInstance(key))
return resolve(key);

if (key !== null) {
const pubKey = KEYUTIL.getKey(key);
return resolve(new ECDSAKey(pubKey));
}
}).catch((err) => {
reject(err);
});

});
const store = await this._cryptoKeyStore._getKeyStore();
const key = await store.getKey(ski);
if (key instanceof ECDSAKey)
return key;

if (key !== null) {
const pubKey = KEYUTIL.getKey(key);
return new ECDSAKey(pubKey);
}
}

/**
* This is an implementation of {@link module:api.CryptoSuite#hash}
* The opts argument is not supported.
*/
* This is an implementation of {@link module:api.CryptoSuite#hash}
* The opts argument is not supported.
*/
hash(msg, opts) {
if (opts) ;
return this._hashFunction(msg);
}

/**
* This is an implementation of {@link module:api.CryptoSuite#sign}
* Signs digest using key k.
*/
* This is an implementation of {@link module:api.CryptoSuite#sign}
* Signs digest using key k.
*/
sign(key, digest) {
if (typeof key === 'undefined' || key === null) {
throw new Error('A valid key is required to sign');
Expand Down Expand Up @@ -278,18 +256,18 @@ class CryptoSuite_ECDSA_AES extends api.CryptoSuite {
}

/**
* This is an implementation of {@link module:api.CryptoSuite#encrypt}
* To be implemented.
*/
* This is an implementation of {@link module:api.CryptoSuite#encrypt}
* To be implemented.
*/
encrypt(key, plainText, opts) {
if (key || plainText || opts) ;
throw new Error('Not implemented yet');
}

/**
* This is an implementation of {@link module:api.CryptoSuite#decrypt}
* To be implemented.
*/
* This is an implementation of {@link module:api.CryptoSuite#decrypt}
* To be implemented.
*/
decrypt(key, cipherText, opts) {
if (key || cipherText || opts) ;
throw new Error('Not implemented yet');
Expand Down
4 changes: 2 additions & 2 deletions fabric-client/lib/impl/NetworkConfig_1_0.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ var NetworkConfig_1_0 = class {
/*
* Internal utility method to get the organization the peer belongs
*/
_getOrganizationForPeer(peer_name) {
_getOrganizationForPeer(peer_name) {
if(this._network_config && this._network_config[ORGS_CONFIG]) {
for(let organization_name in this._network_config[ORGS_CONFIG]) {
let organization = this.getOrganization(organization_name);
Expand All @@ -387,7 +387,7 @@ var NetworkConfig_1_0 = class {
}
}
}
}
}
};

function getTLSCACert(config) {
Expand Down
25 changes: 6 additions & 19 deletions fabric-client/lib/impl/ecdsa/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,37 @@ const jsrsa = require('jsrsasign');
const asn1 = jsrsa.asn1;
const KEYUTIL = jsrsa.KEYUTIL;
const ECDSA = jsrsa.ECDSA;

const api = require('../../api');
const logger = utils.getLogger('ecdsa/key.js');

/**
* This module implements the {@link module:api.Key} interface, for ECDSA.
* @class ECDSA_KEY
* @extends module:api.Key
*/
module.exports = class ECDSA_KEY {
module.exports = class ECDSA_KEY extends api.Key{
/**
* this class represents the private or public key of an ECDSA key pair.
*
* @param {Object} key This must be the "privKeyObj" or "pubKeyObj" part of the object generated by jsrsasign.KEYUTIL.generateKeypair()
*/
constructor(key) {
if (typeof key === 'undefined' || key === null) {
if (!key) {
throw new Error('The key parameter is required by this key class implementation, whether this instance is for the public key or private key');
}

if (!key.type || key.type !== 'EC') {
throw new Error('This key implementation only supports keys generated by jsrsasign.KEYUTIL. It must have a "type" property of value "EC"');
}

// prvKeyHex value can be null for public keys, so need to check typeof here
if (typeof key.prvKeyHex === 'undefined') {
throw new Error('This key implementation only supports keys generated by jsrsasign.KEYUTIL. It must have a "prvKeyHex" property');
}

// pubKeyHex must have a non-null value
if (!key.pubKeyHex) {
throw new Error('This key implementation only supports keys generated by jsrsasign.KEYUTIL. It must have a "pubKeyHex" property');
}

// prvKeyHex value can be null for public keys

super();
this._key = (typeof key === 'undefined') ? null : key;
}

Expand Down Expand Up @@ -139,15 +137,4 @@ module.exports = class ECDSA_KEY {
return KEYUTIL.getPEM(this._key);
}
}

static isInstance(object) {
if (typeof object._key === 'undefined') {
return false;
}

const key = object._key;
return (key.type && key.type === 'EC' &&
typeof key.prvKeyHex !== 'undefined' && // prvKeyHex value can be null for public keys, so need to check typeof here
typeof key.pubKeyHex === 'string'); // pubKeyHex must have a non-null value
}
};
2 changes: 1 addition & 1 deletion test/integration/channel-event-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ test('Test chaincode instantiate with event, transaction invocation with chainco
let event_monitor_1 = new Promise((resolve, reject) => {
let handle = setTimeout(() => {
t.fail('Timeout - Failed to receive the event for event1');
eh.unregisterTxEvent(req1.txId.getTransactionID());
event_hub.unregisterTxEvent(req1.txId.getTransactionID());
reject('timeout');
}, 200000);

Expand Down
Loading

0 comments on commit 3a9087f

Please sign in to comment.