|
| 1 | +'use strict'; |
| 2 | +/* |
| 3 | +* Copyright IBM Corp All Rights Reserved |
| 4 | +* |
| 5 | +* SPDX-License-Identifier: Apache-2.0 |
| 6 | +*/ |
| 7 | +/* |
| 8 | + * Enroll the admin user |
| 9 | + */ |
| 10 | + |
| 11 | +var Fabric_Client = require('fabric-client'); |
| 12 | +var Fabric_CA_Client = require('fabric-ca-client'); |
| 13 | + |
| 14 | +var path = require('path'); |
| 15 | +var util = require('util'); |
| 16 | +var os = require('os'); |
| 17 | + |
| 18 | +// |
| 19 | +var fabric_client = new Fabric_Client(); |
| 20 | +var fabric_ca_client = null; |
| 21 | +var admin_user = null; |
| 22 | +var member_user = null; |
| 23 | +var store_path = path.join(__dirname, 'hfc-key-store'); |
| 24 | +console.log(' Store path:'+store_path); |
| 25 | + |
| 26 | +// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting |
| 27 | +Fabric_Client.newDefaultKeyValueStore({ path: store_path |
| 28 | +}).then((state_store) => { |
| 29 | + // assign the store to the fabric client |
| 30 | + fabric_client.setStateStore(state_store); |
| 31 | + var crypto_suite = Fabric_Client.newCryptoSuite(); |
| 32 | + // use the same location for the state store (where the users' certificate are kept) |
| 33 | + // and the crypto store (where the users' keys are kept) |
| 34 | + var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path}); |
| 35 | + crypto_suite.setCryptoKeyStore(crypto_store); |
| 36 | + fabric_client.setCryptoSuite(crypto_suite); |
| 37 | + var tlsOptions = { |
| 38 | + trustedRoots: [], |
| 39 | + verify: false |
| 40 | + }; |
| 41 | + // be sure to change the http to https when the CA is running TLS enabled |
| 42 | + fabric_ca_client = new Fabric_CA_Client('http://localhost:7054', tlsOptions , 'ca.example.com', crypto_suite); |
| 43 | + |
| 44 | + // first check to see if the admin is already enrolled |
| 45 | + return fabric_client.getUserContext('admin', true); |
| 46 | +}).then((user_from_store) => { |
| 47 | + if (user_from_store && user_from_store.isEnrolled()) { |
| 48 | + console.log('Successfully loaded admin from persistence'); |
| 49 | + admin_user = user_from_store; |
| 50 | + return null; |
| 51 | + } else { |
| 52 | + // need to enroll it with CA server |
| 53 | + return fabric_ca_client.enroll({ |
| 54 | + enrollmentID: 'admin', |
| 55 | + enrollmentSecret: 'adminpw' |
| 56 | + }).then((enrollment) => { |
| 57 | + console.log('Successfully enrolled admin user "admin"'); |
| 58 | + return fabric_client.createUser( |
| 59 | + {username: 'admin', |
| 60 | + mspid: 'Org1MSP', |
| 61 | + cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate } |
| 62 | + }); |
| 63 | + }).then((user) => { |
| 64 | + admin_user = user; |
| 65 | + return fabric_client.setUserContext(admin_user); |
| 66 | + }).catch((err) => { |
| 67 | + console.error('Failed to enroll and persist admin. Error: ' + err.stack ? err.stack : err); |
| 68 | + throw new Error('Failed to enroll admin'); |
| 69 | + }); |
| 70 | + } |
| 71 | +}).then(() => { |
| 72 | + console.log('Assigned the admin user to the fabric client ::' + admin_user.toString()); |
| 73 | +}).catch((err) => { |
| 74 | + console.error('Failed to enroll admin: ' + err); |
| 75 | +}); |
0 commit comments