Tools, FAQ, Tutorials:
enrollAdmin.js - Enroll Admin to ca.example.com
What is the admin enrollment Node.js program enrollAdmin.js?
✍: FYIcenter.com
Admin enrollment Node.js program enrollAdmin.js
enrolls an user "admin" to ca.example.com.
1. View the enrollAdmin.js source code:
$ cp ~/hyperledger-binaries/fabric-samples/fabcar/javascript
$ more enrollAdmin.js
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const FabricCAServices = require('fabric-ca-client');
const { FileSystemWallet, X509WalletMixin } = require('fabric-network');
const fs = require('fs');
const path = require('path');
const ccpPath = path.resolve(__dirname, '..', '..', 'basic-network', 'connection.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 caURL = ccp.certificateAuthorities['ca.example.com'].url;
const ca = new FabricCAServices(caURL);
// 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}`);
// Check to see if we've already enrolled the admin user.
const adminExists = await wallet.exists('admin');
if (adminExists) {
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' });
const identity = X509WalletMixin.createIdentity('Org1MSP', enrollment.certificate, enrollment.key.toBytes());
wallet.import('admin', identity);
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();
As you can see, the script enrolls a user ID with a secret password with ca.example.com. Then it receives the public certificate and the private key from ca.example.com and save them in a directory call wallet.
2. Run it:
$ node enrollAdmin Wallet path: /home/fyicenter/hyperledger-binaries/fabric-samples/fabcar/javascript/wallet Successfully enrolled admin user "admin" and imported it into the wallet $ ls -l wallet/admin/ -rw-rw-r-- 1 fyicenter 246 Apr 1 01:40 2844345e545c1957df94a6362cedabc81682...-priv -rw-rw-r-- 1 fyicenter 182 Apr 1 01:40 2844345e545c1957df94a6362cedabc81682...-pub -rw-rw-r-- 1 fyicenter 986 Apr 1 01:40 admin
⇒ registerUser.js - Register User to ca.example.com
⇐ Reinstall Node.js Required Modules
2020-01-04, ∼2503🔥, 0💬
Popular Posts:
How to use the "forward-request" Policy Statement to call the backend service for an Azure API servi...
How To Read the Entire File into a Single String in PHP? If you have a file, and you want to read th...
What is EPUB 2.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 2.0 Metadata "dc:publisher" ...
What is the Azure AD v1.0 OpenID Metadata Document? Azure AD v1.0 OpenID Metadata Document is an onl...
How to dump (or encode, serialize) a Python object into a JSON string using json.dumps()? The json.d...