diff --git a/fabric-client/lib/api.js b/fabric-client/lib/api.js index eb81457335..9ef991abc9 100755 --- a/fabric-client/lib/api.js +++ b/fabric-client/lib/api.js @@ -100,12 +100,14 @@ module.exports.CryptoSuite = class { * @param {Object} opts *
`type`: type of information that 'raw' represents: x509 certificate, *
`algorithm`: an identifier for the algorithm to be used - *
`ephemeral`: true if the key to generate has to be ephemeral - * @storeKey {boolean} store the key in persistent key store- when false the key will - * the key will be returned without a Promise - * @returns {Key} Promise of an instance of the Key class wrapping the raw key bytes + *
`ephemeral`: {boolean} Optional. If true, the key to import will not be persisted + * and the key will be returned without a Promise. If not set or false, defaults to + * saving the key in persistent key store. + * @returns {Key} or {Promise} If `ephemeral` is true, the Key class wrapping the raw bytes. + * If `ephemeral' not set or false, a Promise of an instance of the + * Key class wrapping the raw key bytes. */ - importKey(raw, opts, storeKey) {} + importKey(raw, opts) {} /** * Returns the key this CSP associates to the Subject Key Identifier ski. diff --git a/fabric-client/lib/impl/CryptoSuite_ECDSA_AES.js b/fabric-client/lib/impl/CryptoSuite_ECDSA_AES.js index b1aed11829..22d586d85d 100755 --- a/fabric-client/lib/impl/CryptoSuite_ECDSA_AES.js +++ b/fabric-client/lib/impl/CryptoSuite_ECDSA_AES.js @@ -133,6 +133,7 @@ var CryptoSuite_ECDSA_AES = class extends api.CryptoSuite { var 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)); } else { if (!this._cryptoKeyStore) { @@ -168,17 +169,15 @@ var CryptoSuite_ECDSA_AES = class extends api.CryptoSuite { /** * This is an implementation of {@link module:api.CryptoSuite#importKey} - */ - importKey(raw, opts, storeKey) { + **/ + importKey(raw, opts) { logger.debug('importKey - start'); var store_key = true; //default - // if storing is not required and therefore a promise will not be returned - // then storeKey must be set to false; - if(typeof storeKey === 'boolean') { - store_key = storeKey; + if (typeof opts !== 'undefined' && typeof opts.ephemeral !== 'undefined' && opts.ephemeral === true) { + store_key = false; } if (!!store_key && !this._cryptoKeyStore) { - throw new Error('importKey storeKey is true, which requires CryptoKeyStore to be set.'); + throw new Error('importKey opts.ephemeral is false, which requires CryptoKeyStore to be set.'); } var self = this; diff --git a/fabric-client/lib/msp/msp.js b/fabric-client/lib/msp/msp.js index 86555b54da..23460adf56 100755 --- a/fabric-client/lib/msp/msp.js +++ b/fabric-client/lib/msp/msp.js @@ -146,7 +146,7 @@ var MSP = class { var cert = sid.getIdBytes().toBinary(); logger.debug('Encoded cert from deserialized identity: %s', cert); if(!store_key) { - var publicKey =this.cryptoSuite.importKey(cert, { algorithm: api.CryptoAlgorithms.X509Certificate }, false); + var publicKey =this.cryptoSuite.importKey(cert, { algorithm: api.CryptoAlgorithms.X509Certificate, ephemeral: true }); var sdk_identity = new Identity(cert, publicKey, this.getId(), this.cryptoSuite); return sdk_identity; } diff --git a/test/unit/cryptosuite-ecdsa-aes.js b/test/unit/cryptosuite-ecdsa-aes.js index 2361730c22..2bdb1a322e 100644 --- a/test/unit/cryptosuite-ecdsa-aes.js +++ b/test/unit/cryptosuite-ecdsa-aes.js @@ -167,7 +167,7 @@ test('\n\n ** CryptoSuite_ECDSA_AES - error tests **\n\n', function (t) { () => { cryptoUtils.importKey(TEST_CERT_PEM); }, - /importKey storeKey is true, which requires CryptoKeyStore to be set./, + /importKey opts.ephemeral is false, which requires CryptoKeyStore to be set./, 'Test missing cryptoKeyStore: cryptoSuite.importKey' ); t.throws( @@ -180,6 +180,31 @@ test('\n\n ** CryptoSuite_ECDSA_AES - error tests **\n\n', function (t) { t.end(); }); +test('\n\n ** CryptoSuite_ECDSA_AES - ephemeral true tests **\n\n', function (t) { + testutil.resetDefaults(); + var cryptoUtils = utils.newCryptoSuite(); + var key = cryptoUtils.importKey(TEST_KEY_PRIVATE_PEM, {ephemeral: true}); + if (key && key._key && key._key.type === 'EC') { + t.pass('importKey returned key using ephemeral true'); + } else { + t.fail('importKey did not return key using ephemeral true'); + } + + return cryptoUtils.generateKey({ephemeral: true}) + .then(function (key) { + if (key && key._key && key._key.type === 'EC') { + t.pass('generateKey returned key using ephemeral true'); + t.end(); + } else { + t.fail('generateKey did not return key using ephemeral true'); + t.end(); + } + },(err) => { + t.fail('Failed to generateKey. Can not progress any further. Exiting. ' + err.stack ? err.stack : err); + t.end(); + }); +}); + test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) { testutil.resetDefaults();