Tools, FAQ, Tutorials:
register-user.js - Register New User
How to write a Node.js script to Register New Users to ca.example.com?
✍: FYIcenter.com
If you are an administrator on ca.example.com,
you can register other new users to ca.example.com.
Here is sample Node.js script, register-user.js, that uses your "admin" identity from the local wallet to register a new user to on ca.example.com.
/* Copyright (c) FYIcenter.com
*/
'use strict';
const FabricCAServices = require('fabric-ca-client');
const { FileSystemWallet, X509WalletMixin, Gateway } = require('fabric-network');
// Build the connection setting object
const fs = require('fs');
const ccp = JSON.parse(fs.readFileSync("connection.json", 'utf8'));
// Get user name from the command line
const user = process.argv[2];
async function main() {
try {
// Open local wallet
const wallet = new FileSystemWallet('wallet');
// 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 pass = await ca.register({ enrollmentID: user }, adminIdentity);
console.log("Temporary password received: %s", pass);
// Enroll the user
const enrollment = await ca.enroll({ enrollmentID: user, enrollmentSecret: pass });
// Remove old keys
if (await wallet.exists(user)) {
await wallet.delete(user);
};
// Import new keys to wallet
const identity = X509WalletMixin.createIdentity('Org1MSP',
enrollment.certificate, enrollment.key.toBytes());
wallet.import(user, identity);
console.log("User (%s) registered!", user);
} catch (error) {
console.error(`Something wrong: ${error}`);
process.exit(1);
}
}
main();
To run this application script on your client system, you need finish the last tutorial to enroll with your admin user and password, and import the certificate and the key info the local wallet.
Now try to register a new user to ca.example.com
$ node register-user.js alice Temporary password received: JGtVfGJRGcGZ User (alice) registered!
Note that password generated by ca.example.com for the new user is temporary password, you cannot use it to enroll (sign in) again with the sample password.
You need to save the certificate and key of the new user in the local wallet and use them later to communicate with the Fabric network.
$ ls -l wallet/alice -rw-rw-r-- 1 fyicenter 246 Apr 1 01:03 7091c900c71b08e2e221fe88fdbd1383072...-priv -rw-rw-r-- 1 fyicenter 182 Apr 1 01:03 7091c900c71b08e2e221fe88fdbd1383072...-pub -rw-rw-r-- 1 fyicenter 1110 Apr 1 01:03 alice
User name in ca.example.com must be unique. If you try to register "alice" again, you will get an error:
$ node register-user.js alice
Something wrong: Error: fabric-ca request register failed with errors
[[{"code":0,"message":"Registration of 'alice' failed: Identity 'alice' is already registered"}]]
⇒ evaluate-transaction.js - Evaluate Chaincode Transaction
⇐ wallet-import.js - Import Keys to Wallet
2019-04-22, ∼2347🔥, 0💬
Popular Posts:
How to access Query String parameters from "context.Request.Url.Que ry"object in Azure API Policy? Q...
Where to get a real Atom XML example? You can follow this tutorial to get a real Atom XML example: 1...
How To Avoid the Undefined Index Error in PHP? If you don't want your PHP page to give out errors as...
How to use the XML to JSON Conversion Tool at freeformatter.com? If you want to try the XML to JSON ...
How to use the "set-variable" Policy Statement to create custom variables for an Azure API service o...