Created
March 28, 2019 09:40
-
-
Save kctam/223fb3b1651eadeb69fef90e4ef898ce to your computer and use it in GitHub Desktop.
Direct adopted from fabric-samples/fabcar/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment