As the Chaincode is instantiated and the needed dependencies are installed, launch the Node.js server using node server.js. Once the server is running, open the web browser by navigating to http://yourserverip:3000. You will see a web page similar to the one here:
You can start by assigning the admin to the Fabric client. For that, click the Register Admin button, which will register you in Fabric as the admin client user:
In this step, we send a request for an admin as the authorized user and register with the CA. The certificate-related folder, hrc-key-store, should be generated.
Let's take a look at how server.js registers admin users in a Fabric blockchain.
The client application uses fabric-ca-client, fabric-client, and GRpc node libraries to interact with the Fabric network.
The fabric-ca-client manages Fabric's user certificate life cycles, including registering, enrolling through API, and interacting with the Fabric CA.
The fabric-client interacts with peers and orderers to install and instantiate Chaincodes, send transactions, and perform queries.
Here are a few steps to register admin users:
- Set up hfc-key-store for Fabric_Client
- Initialize Fabric client—new Fabric_CA_Client('http://localhost:7054', tlsOptions , 'ca.example.com', crypto_suite)
- Get admin user—context - fabric_client.getUserContext('admin', true)
- Create admin user—fabric_client.createUser
Here is some sample code, serving to register an admin:
app.get('/registerAdmin',function(req,res) {
var fabric_client = new Fabric_Client();
var fabric_ca_client = null;
var admin_user = null;
var member_user = null;
var store_path = path.join(__dirname, 'hfc-key-store');
Fabric_Client.newDefaultKeyValueStore({ path: store_path }).then((state_store) => {
// assign the store to the fabric client
fabric_client.setStateStore(state_store);
var crypto_suite = Fabric_Client.newCryptoSuite();
var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
crypto_suite.setCryptoKeyStore(crypto_store);
fabric_client.setCryptoSuite(crypto_suite);
var tlsOptions = {
trustedRoots: [],
verify: false
};
fabric_ca_client = new Fabric_CA_Client('http://localhost:7054', tlsOptions , 'ca.example.com',crypto_suite);
return fabric_client.getUserContext('admin', true);
}).then((user_from_store) => {
if (user_from_store && user_from_store.isEnrolled()) {
console.log('Successfully loaded admin from persistence');
admin_user = user_from_store;
return null;
} else {
// need to enroll it with CA server
return fabric_ca_client.enroll({
enrollmentID: 'admin',
enrollmentSecret: 'adminpw'
}).then((enrollment) => {
console.log('Successfully enrolled admin user "admin"');
return fabric_client.createUser({username: 'admin',mspid: 'Org1MSP',
cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate }});
}).then((user) => {
admin_user = user;
return fabric_client.setUserContext(admin_user);
}).catch((err) => {
});
}
}).then(() => {
..
}).catch((err) => {
});
});
Once the admin is registered, we register a user as the Fabric client by clicking the Register User button. This step is similar to the precedent, and the user will be registered with the CA server and enrolled as a new user with an assigned identity, which can query and update the ledger. This authority is granted and authorized by the admin user through the Fabric network: