Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // import FabricCAServices from 'fabric-ca-client';
import fs from 'fs';
import path from 'path';
import util from 'util';
import { LogChainConfigs } from '../../configs';
import { S3Wallets } from '../aws/services/aws-wallet.service';
import { FabricConfig, FabricConfigs } from './configs';
import { CommonConnection } from './types';
const hfc = require('fabric-client');
const readFile = util.promisify(fs.readFile);
export class AppUtil {
static ccp: CommonConnection = {};
/**
* Build the common connection configuration file
* @param org The organization
* @param connection_config_file The connection config file.
*/
public static readonly buildCCPOrg = async (
org: string,
connection_config_file: string,
) => {
const FabricConfigObj = FabricConfigs[org];
if (!FabricConfigObj) {
throw new Error(
`FabricConfigs for ${org} not exist`,
);
}
const ccpPath = connection_config_file;
const contents = await readFile(ccpPath, 'utf8');
// build a JSON object from the file contents
AppUtil.ccp[org] = JSON.parse(contents);
return AppUtil.ccp[org];
};
/**
* Build the wallet.
*/
public static buildWallet = async () => {
const wallet_path = path.join(
process.env.HLF_NETWORK_DIRECTORY as string,
'wallet',
);
// Create a new wallet: Note that wallet is for managing identities.
return S3Wallets.newFileSystemWallet(wallet_path);
};
/**
* Build CA client
* @param ccp The common connection profile
* @param ca_host_name
*/
public static buildCAClient = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ccp: any,
ca_host_name: string,
) => {
// Create a new CA client for interacting with the CA.
// const caInfo = ccp.certificateAuthorities[ca_host_name]; //lookup CA details from config
// const caTLSCACerts = caInfo.tlsCACerts.pem;
// const caClient = new FabricCAServices(
// caInfo.url,
// { trustedRoots: caTLSCACerts, verify: false },
// caInfo.caName,
// );
const orgName = ca_host_name.replace('ca.', '');
const client = await AppUtil.getClientForOrg(orgName);
const admins = hfc.getConfigSetting('admins');
const adminUser = await client.setUserContext({username: admins[0].username, password: admins[0].secret});
const caClient = client.getCertificateAuthority();
const caInfo = ccp.certificateAuthorities[ca_host_name];
console.log(`Built a CA Client named ${caInfo.caName}`);
return { caClient, adminUser };
};
/**
* Build Org config base on Company
* @param {number} company_id
* @returns FabricConfig
*/
public static buildOrgConfigsBasedOnCompany(
company_id: number,
): FabricConfig {
let org = 0;
if (process.env.HLF_CHANNEL_NAME === 'shipping') {
org = 1;
if (company_id !== LogChainConfigs.LogChainCompanyId) {
org = 2;
}
} else {
org = 3;
if (company_id !== LogChainConfigs.LogChainCompanyId) {
org = 4;
}
}
const FabricConfigObj = FabricConfigs['org'+org];
if (!FabricConfigObj) {
throw new Error(
`FabricConfigs for ${org} not exist`,
);
}
return FabricConfigObj;
};
/**
* Get client by org name
* @param {string} orgname
* @param {} username=''
* @returns Promise
*/
public static async getClientForOrg (orgname: string, username = '') {
console.info('============ START getClientForOrg for org %s and user %s', orgname, username);
const FabricConfigObj = FabricConfigs[orgname];
if (!FabricConfigObj) {
throw new Error(
`FabricConfigs for ${orgname} not exist`,
);
}
hfc.addConfigFile(path.resolve(FabricConfigObj.configDir, 'config.json'));
const config = path.resolve(FabricConfigObj.configDir, `connection-${orgname.toLowerCase()}.json`);
const clientConfig = path.resolve(FabricConfigObj.configDir, 'client-' + orgname.toLowerCase() + '.yaml');
console.info('##### getClient - Loading connection profiles from file: %s and %s', config, clientConfig);
// Load the connection profiles. First load the network settings, then load the client specific settings
const client = hfc.loadFromConfig(config);
client.loadFromConfig(clientConfig);
// Create the state store and the crypto store
await client.initCredentialStores();
// Try and obtain the user from persistence if the user has previously been
// registered and enrolled
if (username) {
const user = await client.getUserContext(username, true);
if (!user) {
throw new Error(util.format('##### getClient - User was not found :', username));
} else {
console.info('##### getClient - User %s was found to be registered and enrolled', username);
}
}
console.info('============ END getClientForOrg for org %s and user %s \n\n', orgname);
return client;
}
}
|