Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('\n\n** ChannelEventHub test connect failure on transaction registration \n\n', (t) => {
const client = new Client();
const channel = client.newChannel('mychannel');
const peer = new Peer('grpc://127.0.0.1:9999');
let event_hub = null;
const member = new User('user1');
const crypto_suite = utils.newCryptoSuite();
crypto_suite.setCryptoKeyStore(utils.newCryptoKeyStore());
member.setCryptoSuite(crypto_suite);
crypto_suite.generateKey()
.then((key) => {
return member.setEnrollment(key, test_user.TEST_CERT_PEM, 'DEFAULT');
}).then(() => {
client.setUserContext(member, true);
// tx test
event_hub = channel.newChannelEventHub(peer);
event_hub.registerTxEvent('123',
() => {
t.fail('Failed callback should not have been called - tx test 2');
t.end();
},
(error) => {
test('\n\n**PKCS11 - generate a non-ephemeral key\n\n', async (t) => {
try {
const key = await cryptoUtils.generateKey({algorithm: 'AES', ephemeral: false});
t.pass('Successfully generated a non-ephemeral AES key');
const ski = key.getSKI();
// re-construct a new instance of the CryptoSuite so that when "getKey()"
// is called it'll not have the previously generated key in memory but
// have to retrieve it from persistence store
cryptoUtils.closeSession();
cryptoUtils.finalize();
cryptoUtils = utils.newCryptoSuite({
lib: libpath,
slot,
pin: pin
});
const recoveredKey = await cryptoUtils.getKey(ski);
t.true(!!recoveredKey, 'Successfully recovered key from store using calculated SKI');
const message = 'Hello World!!';
// Encrypt a message.
const cipherText = cryptoUtils.encrypt(recoveredKey, Buffer.from(message), {});
// Decrypt the message.
const plainText = cryptoUtils.decrypt(recoveredKey, cipherText, {});
t.equal(plainText.toString(), message, 'Successfully decrypted');
t.end();
test('\n\n**PKCS11 - Test sign and verify with non-ephemeral ECDSA key pair in the crypto card.\n\n', async (t) => {
try {
let key = await cryptoUtils.generateKey({algorithm: 'ECDSA', ephemeral: false});
t.pass('Successfully generated ECDSA key pair');
const ski = key.getSKI();
// re-construct a new instance of the CryptoSuite so that when "getKey()"
// is called it'll not have the previously generated key in memory but
// have to retrieve it from persistence store
cryptoUtils.closeSession();
cryptoUtils.finalize();
cryptoUtils = utils.newCryptoSuite({
lib: libpath,
slot: 0,
pin: pin
});
key = await cryptoUtils.getKey(ski);
t.true(!!key, 'Successfully recovered key from store using calculated SKI');
const message = 'Hello World!';
const sig = cryptoUtils.sign(key, Buffer.from(message), null);
const v = cryptoUtils.verify(key, sig, Buffer.from(message));
t.equal(v, true, 'Successfully verified signature');
t.end();
} catch (e) {
t.fail('Failed. ' + e);
test('\n\n ** Transaction - constructor set get tests **\n\n', function (t) {
testutil.resetDefaults();
t.throws(function() {
new TransactionID();
},
/Missing userContext or signing identity parameter/,
'Test Missing signer parameter');
var member = new User('admin');
// test set enrollment for identity and signing identity
var cryptoUtils = utils.newCryptoSuite();
cryptoUtils.generateKey({ephemeral : true})
.then(function (key) {
// the private key and cert don't match, but it's ok, the code doesn't check
return member.setEnrollment(key, TEST_CERT_PEM, 'DEFAULT');
}).then(() =>{
var trans_id = new TransactionID(member.getSigningIdentity());
t.pass('Successfully created a new TransactionID');
t.equals(trans_id.isAdmin(), false, ' should have false admin');
trans_id = new TransactionID(member.getSigningIdentity(), true);
t.equals(trans_id.isAdmin(), true, ' should have true admin');
t.end();
}).catch((err) => {
t.fail(err.stack ? err.stack : err);
t.end();
});
test('\n\n** EventHub test actions when connect failures on block registration \n\n', (t) => {
var client = new Client();
var event_hub = null;
var member = new User('user1');
var crypto_suite = utils.newCryptoSuite();
crypto_suite.setCryptoKeyStore(utils.newCryptoKeyStore());
member.setCryptoSuite(crypto_suite);
crypto_suite.generateKey()
.then(function (key) {
return member.setEnrollment(key, test_user.TEST_CERT_PEM, 'DEFAULT');
}).then(() => {
//var id = member.getIdentity();
client.setUserContext(member, true);
// test 1
event_hub = client.newEventHub();
event_hub.setPeerAddr('grpc://localhost:9997');
event_hub.connect();
t.doesNotThrow(
() => {
event_hub.registerBlockEvent(() => {
t.pass('User constructor test 2: new User cfg getName was successful');
} else {
t.fail('User constructor test 2: new User cfg getName was not successful');
}
if (member2.getRoles() &&
member2.getRoles().indexOf('admin') > -1 &&
member2.getRoles().indexOf('user') > -1) {
t.pass('User constructor test 2: new User cfg getRoles was successful');
} else {
t.fail('User constructor test 2: new User cfg getRoles was not successful');
}
// test set enrollment for identity and signing identity
try {
const cryptoUtils = utils.newCryptoSuite();
cryptoUtils.setCryptoKeyStore(utils.newCryptoKeyStore());
const key = await cryptoUtils.generateKey();
await member2.setEnrollment(key, TEST_CERT_PEM, 'DEFAULT');
const id = member2.getIdentity();
t.equal(id._publicKey._key.pubKeyHex, '0452a75e1ee105da7ab3d389fda69d8a04f5cf65b305b49cec7cdbdeb91a585cf87bef5a96aa9683d96bbabfe60d8cc6f5db9d0bc8c58d56bb28887ed81c6005ac', 'User class setEnrollment() test');
t.end();
} catch (err) {
t.fail(err.stack ? err.stack : err);
t.end();
}
});
if (typeof process.env.PKCS11_PIN === 'string' && process.env.PKCS11_PIN !== '') {
pin = process.env.PKCS11_PIN;
}
if (typeof process.env.PKCS11_SLOT === 'string' && process.env.PKCS11_SLOT !== '') {
slot = parseInt(process.env.PKCS11_SLOT);
}
if (!libpath) {
t.fail('Could not locate a PKCS11 library -- PKCS11 tests will probably fail');
t.end();
} else {
utils.setConfigSetting('crypto-hsm', true);
cryptoUtils = utils.newCryptoSuite({
lib: libpath,
slot: slot,
pin: pin
});
try {
// Test generate AES key, encrypt, and decrypt.
const key = await cryptoUtils.generateKey({algorithm: 'AES', ephemeral: true});
t.pass('Successfully generated an ephemeral AES key');
const ski = key.getSKI();
t.comment('AES ski[' + ski.length + ']: ' + ski.toString('hex'));
const recoveredKey = await cryptoUtils.getKey(ski);
t.true(!!recoveredKey, 'Successfully recovered key from calculated SKI');
async function setMember(client) {
// do some setup for following test
const member = new User('admin');
utils.setConfigSetting('key-value-store', 'fabric-client/lib/impl/FileKeyValueStore.js');
const store = await Client.newDefaultKeyValueStore({path: testutil.KVS});
client.setStateStore(store);
const cryptoUtils = utils.newCryptoSuite();
const key = await cryptoUtils.generateKey({ephemeral: true});
const TEST_CERT_PEM = require('./user.js').TEST_CERT_PEM;
await member.setEnrollment(key, TEST_CERT_PEM, 'DEFAULT');
client.setUserContext(member, true);
}
() => {
new SigningIdentity('cert', 'pubKey');
},
/Missing required parameter "mspId"/,
'Checking required input parameters'
);
t.throws(
() => {
new SigningIdentity('cert', 'pubKey', 'mspId', 'cryptoSuite');
},
/Missing required parameter "signer"/,
'Checking required input parameters'
);
const cryptoUtils = utils.newCryptoSuite();
cryptoUtils.setCryptoKeyStore(utils.newCryptoKeyStore());
// test identity serialization and deserialization
const mspImpl = new MSP({
rootCerts: [],
admins: [],
id: 'testMSP',
cryptoSuite: cryptoUtils
});
let pubKey = cryptoUtils.importKey(TEST_CERT_PEM, {algorithm: CryptoAlgorithms.X509Certificate});
const identity = new Identity(TEST_CERT_PEM, pubKey, mspImpl.getId(), cryptoUtils);
const serializedID = identity.serialize();
// deserializeIdentity should work both ways ... with promise and without
const identity_g = mspImpl.deserializeIdentity(serializedID, false);