Repository navigation
Expand file tree
/
Copy pathadmin.js
More file actions
52 lines (44 loc) · 2.06 KB
/
Copy pathadmin.js
File metadata and controls
52 lines (44 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const FabricCAServices = require('fabric-ca-client');
const { Wallets } = require('fabric-network');
const fs = require('fs');
const path = require('path');
async function main() {
try {
// Load the network configuration (connection profile).
const ccpPath = path.resolve('../organizations/peerOrganizations/org1.example.com/connection-org1.json'); // Adjust this path as needed
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new CA client for interacting with the CA.
const caURL = ccp.certificateAuthorities['ca.org1.example.com'].url; // Adjust this with the correct CA name
const ca = new FabricCAServices(caURL);
// Create a new wallet for managing identities.
const walletPath = path.join(__dirname, 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the admin.
const adminIdentity = await wallet.get('admin');
if (adminIdentity) {
console.log('An identity for the admin user "admin" already exists in the wallet');
return;
}
// Enroll the admin user, and import the new identity into the wallet.
const enrollment = await ca.enroll({
enrollmentID: 'admin',
enrollmentSecret: 'adminpw', // Use the correct enrollment secret from your CA
});
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: 'Org1MSP', // Adjust this according to your organization
type: 'X.509',
};
await wallet.put('admin', x509Identity);
console.log('Successfully enrolled admin user "admin" and imported it into the wallet');
} catch (error) {
console.error(`Failed to enroll admin user "admin": ${error}`);
process.exit(1);
}
}
main();