fabric-samples/commercial-paper/organization/digibank/application/enrollUser.js
Arnaud J Le Hors 2e3683c419
Add missing Copyright in some files (#163)
Signed-off-by: Arnaud J Le Hors <lehors@us.ibm.com>
2020-05-19 15:50:48 +01:00

56 lines
2 KiB
JavaScript

/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const FabricCAServices = require('fabric-ca-client');
const { Wallets } = require('fabric-network');
const fs = require('fs');
const yaml = require('js-yaml');
const path = require('path');
async function main() {
try {
// load the network configuration
let connectionProfile = yaml.safeLoad(fs.readFileSync('../gateway/connection-org1.yaml', 'utf8'));
// Create a new CA client for interacting with the CA.
const caInfo = connectionProfile.certificateAuthorities['ca.org1.example.com'];
const caTLSCACerts = caInfo.tlsCACerts.pem;
const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), '../identity/user/balaji/wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the admin user.
const userExists = await wallet.get('balaji');
if (userExists) {
console.log('An identity for the client user "balaji" already exists in the wallet');
return;
}
// Enroll the admin user, and import the new identity into the wallet.
const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: 'user1pw' });
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put('balaji', x509Identity);
console.log('Successfully enrolled client user "balaji" and imported it into the wallet');
} catch (error) {
console.error(`Failed to enroll client user "balaji": ${error}`);
process.exit(1);
}
}
main();