Skip to content

Commit

Permalink
BE-747 Removed fabric-client package … (#131)
Browse files Browse the repository at this point in the history
* BE-747 Removed fabric-client package

Signed-off-by: jeeva <jeevasang@gmail.com>

* Removed eslint

Signed-off-by: jeeva <jeevasang@gmail.com>

* Modified path

Signed-off-by: jeeva <jeevasang@gmail.com>
  • Loading branch information
JeevaSang authored Jun 29, 2020
1 parent 2e57b03 commit 05a714e
Show file tree
Hide file tree
Showing 6 changed files with 577 additions and 847 deletions.
228 changes: 0 additions & 228 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

const Fabric_Client = require('fabric-client');
const { User } = require('fabric-common');
const path = require('path');
const includes = require('lodash/includes');

const ExplorerError = require('../../common/ExplorerError');
const FabricUtils = require('./utils/FabricUtils.js');
const FabricGateway = require('../../platform/fabric/gateway/FabricGateway');
Expand All @@ -31,16 +28,10 @@ class FabricClient {
constructor(network_name, client_name) {
this.network_name = network_name;
this.client_name = client_name;
this.hfc_client = null;
this.fabricGateway = null;
this.defaultMspId = {};
this.defaultOrderer = null;
this.channelsGenHash = new Map();
this.client_config = null;
this.config = null;
this.status = false;
this.tls = false;
this.asLocalhost = false;
this.channels = [];
}

Expand Down Expand Up @@ -70,17 +61,6 @@ class FabricClient {
// Use Gateway to connect to fabric network
this.fabricGateway = new FabricGateway(configPath);
await this.fabricGateway.initialize();
this.hfc_client = Fabric_Client.loadFromConfig(this.fabricGateway.config);
/* eslint-enable */
this.defaultMspId = this.fabricGateway.getDefaultMspId();
this.tls = this.fabricGateway.getTls();
this.config = this.fabricGateway.getConfig();
// Set discovery protocol
if (!this.tls) {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpc');
} else {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpcs');
}
} catch (error) {
// TODO in case of the failure, should terminate explorer?
logger.error(error);
Expand Down Expand Up @@ -161,15 +141,6 @@ class FabricClient {
const nodes = await persistence
.getMetricService()
.getPeerList(this.network_name, channel.channel_genesis_hash);
let newchannel;
try {
newchannel = this.hfc_client.getChannel(channel.channelname);
} catch (e) {
logger.error('Failed to get channel from client ', e);
}
if (newchannel === undefined) {
newchannel = this.hfc_client.newChannel(channel.channelname);
}
for (const node of nodes) {
const peer_config = peers[node.server_hostname];
let pem;
Expand Down Expand Up @@ -225,12 +196,6 @@ class FabricClient {
await this.initializeNewChannel(channel_name);
}

if (!this.tls) {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpc');
} else {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpcs');
}

const discover_results = await this.fabricGateway.getDiscoveryResult(
channel_name
);
Expand All @@ -248,74 +213,6 @@ class FabricClient {
return discover_results;
}

/**
*
*
* @param {*} channel
* @param {*} url
* @param {*} msp_id
* @param {*} host
* @param {*} msps
* @returns
* @memberof FabricClient
*/
newOrderer(channel, url, msp_id, host, msps) {
let newOrderer = null;
channel._orderers.forEach(orderer => {
if (orderer.getUrl() === url) {
logger.debug('Found existing orderer %s', url);
newOrderer = orderer;
}
});
if (!newOrderer) {
if (msps[msp_id]) {
logger.debug('Create a new orderer %s', url);
newOrderer = this.hfc_client.newOrderer(
url,
channel._buildOptions(url, url, host, msps[msp_id])
);
channel.addOrderer(newOrderer);
} else {
throw new ExplorerError(explorer_mess.error.ERROR_2007);
}
}
return newOrderer;
}

/**
*
*
* @param {*} channel
* @param {*} url
* @param {*} msp_id
* @param {*} host
* @param {*} msps
* @returns
* @memberof FabricClient
*/
newPeer(channel, url, msp_id, host, msps) {
let newpeer = null;
channel._channel_peers.forEach(peer => {
if (peer.getUrl() === url) {
logger.debug('Found existing peer %s', url);
newpeer = peer;
}
});
if (!newpeer) {
if (msps[msp_id]) {
logger.debug('Create a new peer %s', url);
newpeer = this.hfc_client.newPeer(
url,
channel._buildOptions(url, url, host, msps[msp_id])
);
channel.addPeer(newpeer, msp_id);
} else {
throw new ExplorerError(explorer_mess.error.ERROR_2007);
}
}
return newpeer;
}

/**
*
*
Expand All @@ -332,74 +229,6 @@ class FabricClient {
return genesisBlock;
}

/**
*
*
* @param {*} msp_name
* @param {*} username
* @param {*} msp_admin_cert
* @returns
* @memberof FabricClient
*/
async newUser(msp_name, username, msp_admin_cert) {
const organization = await this.hfc_client._network_config.getOrganization(
msp_name,
true
);
if (!organization) {
logger.debug('Client.createUser missing required organization.');
return;
}
const mspid = organization.getMspid();
if (!mspid || mspid.length < 1) {
logger.debug('Client.createUser parameter mspid is required.');
return;
}
const admin_key = organization.getAdminPrivateKey();
let admin_cert = organization.getAdminCert();
if (!admin_cert) {
admin_cert = msp_admin_cert;
}
if (!admin_key) {
logger.debug(
'Client.createUser one of cryptoContent privateKey, privateKeyPEM or privateKeyObj is required.'
);
return;
}
if (!admin_cert) {
logger.debug(
'Client.createUser either cryptoContent signedCert or signedCertPEM is required.'
);
return;
}

const opts = {
username,
mspid,
cryptoContent: {
privateKeyPEM: admin_key,
signedCertPEM: admin_cert
}
};
let importedKey;
const user = new User(opts.username);
const privateKeyPEM = opts.cryptoContent.privateKeyPEM;
if (privateKeyPEM) {
logger.debug('then privateKeyPEM data');
importedKey = await this.hfc_client
.getCryptoSuite()
.importKey(privateKeyPEM.toString(), {
ephemeral: !this.hfc_client.getCryptoSuite()._cryptoKeyStore
});
}
const signedCertPEM = opts.cryptoContent.signedCertPEM;
logger.debug('then signedCertPEM data');
user.setCryptoSuite(this.hfc_client.getCryptoSuite());
await user.setEnrollment(importedKey, signedCertPEM.toString(), opts.mspid);
logger.debug('then user');
return user;
}

/**
*
*
Expand All @@ -410,16 +239,6 @@ class FabricClient {
return Array.from(this.channelsGenHash.keys());
}

/**
*
*
* @returns
* @memberof FabricClient
*/
getHFC_Client() {
return this.hfc_client;
}

/**
*
*
Expand Down Expand Up @@ -473,21 +292,6 @@ class FabricClient {
return this.client_name;
}

/**
*
*
* @param {*} channel_genesis_hash
* @returns
* @memberof FabricClient
*/
getChannelByHash(channel_genesis_hash) {
for (const [channel_name, hash_name] of this.channelsGenHash.entries()) {
if (channel_genesis_hash === hash_name) {
return this.hfc_client.getChannel(channel_name);
}
}
}

/**
*
*
Expand All @@ -503,38 +307,6 @@ class FabricClient {
}
}

/**
*
*
* @param {*} channel_name
* @returns
* @memberof FabricClient
*/
getChannel(channel_name) {
return this.hfc_client.getChannel(channel_name);
}

/**
*
*
* @param {*} defaultOrderer
* @memberof FabricClient
*/
setDefaultOrderer(defaultOrderer) {
logger.info('Set default orderer : ' + defaultOrderer.getName());
this.defaultOrderer = defaultOrderer;
}

/**
*
*
* @returns
* @memberof FabricClient
*/
getDefaultMspId() {
return this.defaultMspId;
}

/**
*
*
Expand Down
10 changes: 10 additions & 0 deletions app/platform/fabric/gateway/FabricGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class FabricGateway {
);
}

if (!this.tlsEnable) {
Client.setConfigSetting('discovery-protocol', 'grpc');
} else {
Client.setConfigSetting('discovery-protocol', 'grpcs');
}

// Set connection options; identity and wallet
this.asLocalhost =
String(Client.getConfigSetting('discovery-as-localhost', 'true')) ===
Expand Down Expand Up @@ -135,6 +141,10 @@ class FabricGateway {
return this.enableAuthentication;
}

getDiscoveryProtocol() {
return Client.getConfigSetting('discovery-protocol');
}

getDefaultMspId() {
return this.fabricConfig.getMspId();
}
Expand Down
6 changes: 2 additions & 4 deletions app/platform/fabric/sync/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
const convertHex = require('convert-hex');
const fabprotos = require('fabric-protos');
const includes = require('lodash/includes');

const helper = require('../../../common/helper');

const logger = helper.getLogger('SyncServices');

const ExplorerError = require('../../../common/ExplorerError');
const FabricUtils = require('../../../platform/fabric/utils/FabricUtils');
const fabric_const = require('../../../platform/fabric/utils/FabricConst')
Expand Down Expand Up @@ -257,9 +257,7 @@ class SyncServices {
*/
async insertNewOrderers(orderer, channel_genesis_hash, client) {
const network_name = client.network_name;
const discoveryProtocol = client.hfc_client.getConfigSetting(
'discovery-protocol'
);
const discoveryProtocol = client.fabricGateway.getDiscoveryProtocol();
const requesturl = `${discoveryProtocol}://${orderer.host}:${orderer.port}`;
logger.debug(
'insertNewOrderers discoveryProtocol ',
Expand Down
4 changes: 2 additions & 2 deletions app/platform/fabric/utils/FabricUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path');
const fs = require('fs-extra');
const sha = require('js-sha256');
const asn = require('asn1.js');
const utils = require('fabric-client/lib/utils');
const { Utils } = require('fabric-common');
const FabricClient = require('./../FabricClient.js');
const ExplorerError = require('../../../common/ExplorerError');
const explorer_error = require('../../../common/ExplorerMessage').explorer
Expand Down Expand Up @@ -164,7 +164,7 @@ function getPEMfromConfig(config) {
// Cert value is in a file
try {
result = readFileSync(config.path);
result = utils.normalizeX509(result);
result = Utils.normalizeX509(result);
} catch (e) {
logger.error(e);
}
Expand Down
Loading

0 comments on commit 05a714e

Please sign in to comment.