Skip to content

Instantly share code, notes, and snippets.

@rizwan92
Last active January 9, 2019 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rizwan92/8c667deb1144bc3191eb185cc625a6f4 to your computer and use it in GitHub Desktop.
Save rizwan92/8c667deb1144bc3191eb185cc625a6f4 to your computer and use it in GitHub Desktop.
hyperledger-channel-problem
const fs = require('fs');
const path = require('path');
const Client = require('fabric-client');
var await = require('await')
const CHANNEL_1_PATH = './ksachdeva-exp-channel-1.tx';
const KEY_STORE_PATH_ADMIN = './keystore/admin';
const ORDERER_URL = 'grpcs://35.244.39.147:7050';
const ORDERER_TLS_CAROOT_PATH = './crypto-config/ordererOrganizations/ksachdeva-exp.com/orderers/orderer.ksachdeva-exp.com/tls/ca.crt';
const ORG1_ADMIN_MSP = './crypto-config/peerOrganizations/org1.ksachdeva-exp.com/users/Admin@org1.ksachdeva-exp.com/msp';
const ORG2_ADMIN_MSP = './crypto-config/peerOrganizations/org2.ksachdeva-exp.com/users/Admin@org2.ksachdeva-exp.com/msp';
const ORG3_ADMIN_MSP = './crypto-config/peerOrganizations/org3.ksachdeva-exp.com/users/Admin@org3.ksachdeva-exp.com/msp';
const org = 'org1';
const CHANNEL_NAME = 'ksachdeva-exp-channel-1'
const CHAIN_CODE_ID = 'ksachdeva-exp-cc'
async function getClient() {
var client = new Client();
console.log('Setting up the cryptoSuite ..');
const cryptoSuite = Client.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({
path: `${KEY_STORE_PATH_ADMIN}-${org}`
}));
client.setCryptoSuite(cryptoSuite);
console.log('Setting up the keyvalue store ..');
// ## Setup the default keyvalue store where the state will be stored
const store = await Client.newDefaultKeyValueStore({
path: `${KEY_STORE_PATH_ADMIN}-${org}`
});
client.setStateStore(store);
console.log('Setting up the keyvalue store ..');
const ORG_ADMIN_MSP = ORG1_ADMIN_MSP;
const privateKeyFile = fs.readdirSync(__dirname + "/" + ORG_ADMIN_MSP + '/keystore')[0];
const cryptoContentOrgAdmin = {
privateKey: ORG_ADMIN_MSP + '/keystore/' + privateKeyFile,
signedCert: ORG_ADMIN_MSP + '/signcerts/Admin@' + org + '.ksachdeva-exp.com-cert.pem'
};
const result = await client.createUser({
username: `${org}-admin`,
mspid: org,
cryptoContent: cryptoContentOrgAdmin
});
return client
}
async function getOrderer(client) {
// build an orderer that will be used to connect to it
const data = await fs.readFileSync(path.join(__dirname, ORDERER_TLS_CAROOT_PATH));
try {
var orderer = await client.newOrderer(ORDERER_URL, {
'pem': Buffer.from(data).toString(),
'ssl-target-name-override': 'orderer.ksachdeva-exp.com'
});
} catch (error) {
console.log(error);
}
return orderer
}
async function mainChannel() {
const org1Client = await getClient(org);
const orderer = await getOrderer(org1Client);
// read in the envelope for the channel config raw bytes
console.log('Reading the envelope from manually created channel transaction ..');
const envelope = fs.readFileSync(path.join(__dirname, CHANNEL_1_PATH));
// extract the configuration
console.log('Extracting the channel configuration ..');
const channelConfig = org1Client.extractChannelConfig(envelope);
console.log('Signing the extracted channel configuration ..');
const signature = org1Client.signChannelConfig(channelConfig);
// prepare the request
const channelRequest = {
name: CHANNEL_NAME,
config: channelConfig,
signatures: [signature],
orderer: orderer,
txId: org1Client.newTransactionID()
};
try {
console.log('Sending the request to create the channel ..');
var response = await org1Client.createChannel(channelRequest);
} catch (error) {
console.log(error);
}
console.log(response);
}
getClient().then(async (client) => {
const orderer = await getOrderer(client);
// console.log(orderer);
})
mainChannel()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment