Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SPDX-License-Identifier: Apache-2.0
- */
- 'use strict';
- const FabricCAServices = require('fabric-ca-client');
- const { FileSystemWallet, X509WalletMixin, Gateway } = require('fabric-network');
- const fs = require('fs');
- const Client = require('fabric-client')
- const path = require('path');
- //const ccpPath = require('../config/network-config.json');
- const peersConfig = require('../config/network-config-copy.json')
- const ccpPath = path.resolve(__dirname, '..', 'config', 'network-config-copy.json');
- const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
- const ccp = JSON.parse(ccpJSON);
- async function main() {
- try {
- // Create a new CA client for interacting with the CA.
- const caInfo = ccp.certificateAuthorities['ca.org1.example.com'];
- const caTLSCACerts = caInfo.tlsCACerts.pem;
- const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
- // Create a new file system based wallet for managing identities.
- const walletPath = path.join(process.cwd(), 'wallet');
- const wallet = new FileSystemWallet(walletPath);
- console.log(`Wallet path: ${walletPath}`);
- const enrollment = await ca.enroll({ enrollmentID: 'adminCA', enrollmentSecret: 'adminpw' });
- const identity = X509WalletMixin.createIdentity('org1MSP', enrollment.certificate, enrollment.key.toBytes());
- await wallet.import('adminCA', identity);
- console.log('Successfully enrolled admin user "admin" and imported it into the wallet');
- // Create a new gateway for connecting to our peer node.
- const gateway = new Gateway();
- await gateway.connect(ccpPath, { wallet, identity: 'adminCA', discovery: { enabled: true, asLocalhost: true } });
- let client = gateway.getClient()
- var cryptoSuite = Client.newCryptoSuite();
- cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({ path: '/tmp' }));
- client.setCryptoSuite(cryptoSuite);
- const pathCert = path.resolve('/Users/mtng/go1.10/src/github.com/hf-dev/hyperledgerfabrictestnet/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/admincerts/Admin@org1.example.com-cert.pem')
- const pathKey = path.resolve('/Users/mtng/go1.10/src/github.com/hf-dev/hyperledgerfabrictestnet/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/0d0ed5f38a1084b1ca12d0836c0e4b011e8a97ca93d919a9d4d4275b147b3e30_sk')
- let cert = fs.readFileSync(pathCert)
- let pk = fs.readFileSync(pathKey)
- const channel = client.newChannel('channel')
- client.setAdminSigningIdentity(pk, cert, 'org1MSP')
- let targets = [];
- let json = peersConfig.peers
- for (var key in json) {
- console.log(json[key])
- let data = fs.readFileSync(json[key].tlsCACerts.path)
- let peer = client.newPeer(json[key].url, {
- pem: Buffer.from(data).toString(),
- 'ssl-target-name-override': json[key].grpcOptions['ssl-target-name-override']
- })
- channel.addPeer(peer)
- targets.push(peer)
- }
- await channel.initialize({ discover: false })
- let request = {
- targets: targets,
- chaincodePath: 'chaincode_example02',
- chaincodeId: 'mycc2',
- chaincodeType: 'golang',
- chaincodeVersion: '1',
- channelName: ['channel']
- };
- const res = await client.installChaincode(request);
- const tx_id = client.newTransactionID();
- const requestIntantiate = {
- chaincodePath: 'chaincode_example02',
- chaincodeId: 'mycc2',
- chaincodeType: 'golang',
- chaincodeVersion: '1',
- channelName: ['channel'],
- fcn: 'init',
- args: ['a', '100', 'b', '200'],
- txId: tx_id,
- 'endorsement-policy': {
- identities: [
- { role: { name: "member", mspId: "org1MSP" } },
- ],
- policy: {
- "1-of": [{ "signed-by": 0 }, { "signed-by": 1 }]
- }
- }
- }
- const resInst = await channel.sendInstantiateProposal(requestIntantiate)
- console.log(resInst)
- } catch (error) {
- console.error(`Failed to enroll admin user "admin": ${error}`);
- process.exit(1);
- }
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement