Tools, FAQ, Tutorials:
registerUser.js - Register User to ca.example.com
What is the user registration Node.js program registerUser.js?
✍: FYIcenter.com
User registration Node.js program registerUser.js
registers an user "user1" to ca.example.com.
1. View the registerUser.js source code:
$ cp ~/hyperledger-binaries/fabric-samples/fabcar/javascript
$ more registerUser.js
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { FileSystemWallet, Gateway, 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 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 user.
const userExists = await wallet.exists('user1');
if (userExists) {
console.log('An identity for the user "user1" already exists in the wallet');
return;
}
// 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" does not exist in the wallet');
console.log('Run the enrollAdmin.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'admin', discovery: { enabled: false } });
// Get the CA client object from the gateway for interacting with the CA.
const ca = gateway.getClient().getCertificateAuthority();
const adminIdentity = gateway.getCurrentIdentity();
// Register the user, enroll the user, and import the new identity into the wallet.
const secret = await ca.register({ affiliation: 'org1.department1', enrollmentID: 'user1', role: 'client' }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: secret });
const userIdentity = X509WalletMixin.createIdentity('Org1MSP', enrollment.certificate, enrollment.key.toBytes());
wallet.import('user1', userIdentity);
console.log('Successfully registered and enrolled admin user "user1" and imported it into the wallet');
} catch (error) {
console.error(`Failed to register user "user1": ${error}`);
process.exit(1);
}
}
main();
As you can see, the script enrolls a user ID with a secret password with ca.example.com, and also registered him as an "client" user. 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 registerUser Successfully registered and enrolled admin user "user2" and imported it into the wallet $ ls -l wallet/user1/ -rw-rw-r-- 1 fyicenter 246 Apr 20 02:45 e1cbc655460d060954514cefa0dc0a4c6e2...-priv -rw-rw-r-- 1 fyicenter 182 Apr 20 02:45 e1cbc655460d060954514cefa0dc0a4c6e2...-pub -rw-rw-r-- 1 fyicenter 1180 Apr 20 02:45 user1
⇒ "Failed to load gRPC binary module" Error
⇐ enrollAdmin.js - Enroll Admin to ca.example.com
2020-01-04, ∼2125🔥, 0💬
Popular Posts:
What are the differences of Differences of evaluateTransaction() and submitTransaction() of the fabr...
How to read Atom validation errors at w3.org? If your Atom feed has errors, the Atom validator at w3...
How to use the urllib.request.Request object to build more complex HTTP request? The urllib.request....
How to use the "@(...)" expression in Azure API Policy? The "@(...)" expression in Azure API Policy ...
How to add an API to an API product for internal testing on the Publisher Portal of an Azure API Man...