|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const FabricCAServices = require('fabric-ca-client'); |
| 8 | +const { Wallets } = require('fabric-network'); |
| 9 | +const fs = require('fs'); |
| 10 | +const yaml = require('js-yaml'); |
| 11 | +const path = require('path'); |
| 12 | + |
| 13 | +async function main() { |
| 14 | + try { |
| 15 | + // load the network configuration |
| 16 | + let connectionProfile = yaml.safeLoad(fs.readFileSync('../gateway/connection-org2.yaml', 'utf8')); |
| 17 | + |
| 18 | + // Create a new CA client for interacting with the CA. |
| 19 | + const caInfo = connectionProfile.certificateAuthorities['ca.org2.example.com']; |
| 20 | + const caTLSCACerts = caInfo.tlsCACerts.pem; |
| 21 | + const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName); |
| 22 | + |
| 23 | + // Create a new file system based wallet for managing identities. |
| 24 | + const walletPath = path.join(process.cwd(), '../identity/user/isabella/wallet'); |
| 25 | + const wallet = await Wallets.newFileSystemWallet(walletPath); |
| 26 | + console.log(`Wallet path: ${walletPath}`); |
| 27 | + |
| 28 | + // Check to see if we've already enrolled the admin user. |
| 29 | + const userExists = await wallet.get('isabella'); |
| 30 | + if (userExists) { |
| 31 | + console.log('An identity for the client user "user1" already exists in the wallet'); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // Enroll the admin user, and import the new identity into the wallet. |
| 36 | + const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: 'user1pw' }); |
| 37 | + const x509Identity = { |
| 38 | + credentials: { |
| 39 | + certificate: enrollment.certificate, |
| 40 | + privateKey: enrollment.key.toBytes(), |
| 41 | + }, |
| 42 | + mspId: 'Org2MSP', |
| 43 | + type: 'X.509', |
| 44 | + }; |
| 45 | + await wallet.put('isabella', x509Identity); |
| 46 | + console.log('Successfully enrolled client user "isabella" and imported it into the wallet'); |
| 47 | + |
| 48 | + } catch (error) { |
| 49 | + console.error(`Failed to enroll client user "isabella": ${error}`); |
| 50 | + process.exit(1); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +main(); |
0 commit comments