Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /*
  2. * SPDX-License-Identifier: Apache-2.0
  3. */
  4.  
  5. 'use strict';
  6.  
  7. const FabricCAServices = require('fabric-ca-client');
  8. const { FileSystemWallet, X509WalletMixin, Gateway } = require('fabric-network');
  9. const fs = require('fs');
  10. const Client = require('fabric-client')
  11. const path = require('path');
  12. //const ccpPath = require('../config/network-config.json');
  13. const peersConfig = require('../config/network-config-copy.json')
  14.  
  15. const ccpPath = path.resolve(__dirname, '..', 'config', 'network-config-copy.json');
  16. const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
  17. const ccp = JSON.parse(ccpJSON);
  18.  
  19. async function main() {
  20. try {
  21.  
  22. // Create a new CA client for interacting with the CA.
  23. const caInfo = ccp.certificateAuthorities['ca.org1.example.com'];
  24. const caTLSCACerts = caInfo.tlsCACerts.pem;
  25. const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
  26.  
  27. // Create a new file system based wallet for managing identities.
  28. const walletPath = path.join(process.cwd(), 'wallet');
  29. const wallet = new FileSystemWallet(walletPath);
  30. console.log(`Wallet path: ${walletPath}`);
  31. const enrollment = await ca.enroll({ enrollmentID: 'adminCA', enrollmentSecret: 'adminpw' });
  32. const identity = X509WalletMixin.createIdentity('org1MSP', enrollment.certificate, enrollment.key.toBytes());
  33. await wallet.import('adminCA', identity);
  34. console.log('Successfully enrolled admin user "admin" and imported it into the wallet');
  35.  
  36.  
  37. // Create a new gateway for connecting to our peer node.
  38. const gateway = new Gateway();
  39. await gateway.connect(ccpPath, { wallet, identity: 'adminCA', discovery: { enabled: true, asLocalhost: true } });
  40.  
  41.  
  42.  
  43. let client = gateway.getClient()
  44.  
  45. var cryptoSuite = Client.newCryptoSuite();
  46. cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({ path: '/tmp' }));
  47. client.setCryptoSuite(cryptoSuite);
  48. 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')
  49.  
  50. 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')
  51. let cert = fs.readFileSync(pathCert)
  52. let pk = fs.readFileSync(pathKey)
  53.  
  54.  
  55. const channel = client.newChannel('channel')
  56. client.setAdminSigningIdentity(pk, cert, 'org1MSP')
  57.  
  58.  
  59. let targets = [];
  60. let json = peersConfig.peers
  61. for (var key in json) {
  62. console.log(json[key])
  63. let data = fs.readFileSync(json[key].tlsCACerts.path)
  64. let peer = client.newPeer(json[key].url, {
  65. pem: Buffer.from(data).toString(),
  66. 'ssl-target-name-override': json[key].grpcOptions['ssl-target-name-override']
  67. })
  68. channel.addPeer(peer)
  69. targets.push(peer)
  70.  
  71. }
  72.  
  73.  
  74.  
  75. await channel.initialize({ discover: false })
  76.  
  77. let request = {
  78. targets: targets,
  79. chaincodePath: 'chaincode_example02',
  80. chaincodeId: 'mycc2',
  81. chaincodeType: 'golang',
  82. chaincodeVersion: '1',
  83. channelName: ['channel']
  84. };
  85. const res = await client.installChaincode(request);
  86.  
  87. const tx_id = client.newTransactionID();
  88.  
  89. const requestIntantiate = {
  90. chaincodePath: 'chaincode_example02',
  91. chaincodeId: 'mycc2',
  92. chaincodeType: 'golang',
  93. chaincodeVersion: '1',
  94. channelName: ['channel'],
  95. fcn: 'init',
  96. args: ['a', '100', 'b', '200'],
  97. txId: tx_id,
  98.  
  99. 'endorsement-policy': {
  100. identities: [
  101. { role: { name: "member", mspId: "org1MSP" } },
  102. ],
  103. policy: {
  104. "1-of": [{ "signed-by": 0 }, { "signed-by": 1 }]
  105. }
  106. }
  107.  
  108.  
  109. }
  110.  
  111. const resInst = await channel.sendInstantiateProposal(requestIntantiate)
  112. console.log(resInst)
  113.  
  114.  
  115. } catch (error) {
  116. console.error(`Failed to enroll admin user "admin": ${error}`);
  117.  
  118. process.exit(1);
  119. }
  120. }
  121.  
  122. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement