Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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(appAdmin);
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: appAdmin, enrollmentSecret: appAdminSecret });
const identity = X509WalletMixin.createIdentity(orgMSPID, enrollment.certificate, enrollment.key.toBytes());
wallet.import(appAdmin, identity);
console.log('msg: Successfully enrolled admin user ' + appAdmin + ' and imported it into the wallet');
} catch (error) {
console.error('Failed to enroll admin user ' + appAdmin + ': ${error}');
process.exit(1);
}
}
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);
// Check to see if we've already enrolled the admin user.
const adminExists = await wallet.exists(appAdmin);
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: appAdmin, enrollmentSecret: appAdminSecret });
const identity = X509WalletMixin.createIdentity(orgMSPID, enrollment.certificate, enrollment.key.toBytes());
wallet.import(appAdmin, identity);
console.log('msg: Successfully enrolled admin user ' + appAdmin + ' and imported it into the wallet');
} catch (error) {
console.error('Failed to enroll admin user ' + appAdmin + ': ${error}');
process.exit(1);
}
}
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: appAdmin, discovery: gatewayDiscovery });
// 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: 'B1', role: 'client' }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: 'B1', enrollmentSecret: secret });
const userIdentity = X509WalletMixin.createIdentity(orgMSPID, enrollment.certificate, enrollment.key.toBytes());
wallet.import('B1', userIdentity);
console.log('Successfully registered and enrolled admin user "B1" and imported it into the wallet');
} catch (error) {
console.error(`Failed to register user "B1": ${error}`);
process.exit(1);
}
}
for (let i = 0; i < orgs.length; i++) {
// Check if organization is present in connection profile and if it contains at least one CA.
if (ccp.organizations[orgs[i]].certificateAuthorities && ccp.organizations[orgs[i]].certificateAuthorities.length != 0) {
// Create a new CA client for interacting with the CA.
const caURL = ccp.certificateAuthorities[ccp.organizations[orgs[i]].certificateAuthorities[0]].url;
const ca = new FabricCAServices(caURL);
// Check to see if we've already enrolled the admin user for this CA.
const adminExists = await wallet.exists('admin' + orgs[i]);
if (adminExists) {
console.log('An identity for the admin user already exists in the wallet: admin', orgs[i]);
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(config.organizations[orgs[i]].MSP, enrollment.certificate, enrollment.key.toBytes());
wallet.import('admin' + orgs[i], identity);
console.log('Successfully enrolled admin user and imported it into the wallet: admin' + orgs[i]);
}
}
} catch (error) {
console.error(`Failed to enroll admin user "admin": ${error}`);
process.exit(1);
}
}
// 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(appAdmin);
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: appAdmin, enrollmentSecret: appAdminSecret });
const identity = X509WalletMixin.createIdentity(orgMSPID, enrollment.certificate, enrollment.key.toBytes());
wallet.import(appAdmin, identity);
console.log('msg: Successfully enrolled admin user ' + appAdmin + ' and imported it into the wallet');
} catch (error) {
console.error(`Failed to enroll admin user ' + ${appAdmin} + : ${error}`);
process.exit(1);
}
}
async function main(){
// Main try/catch block
try {
// define the identity to use
const credPath = path.join(fixtures , '/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com');
const cert = fs.readFileSync(path.join(credPath , '/msp/signcerts/User1@org1.example.com-cert.pem')).toString();
const key = fs.readFileSync(path.join(credPath , '/msp/keystore/c75bd6911aca808941c3557ee7c97e90f3952e379497dc55eb903f31b50abc83_sk')).toString();
const identityLabel = 'User1@org1.example.com';
// prep wallet and test it at the same time
await wallet.import(identityLabel, X509WalletMixin.createIdentity('Org1MSP', cert, key));
} catch (error) {
console.log(`Error adding to wallet. ${error}`);
console.log(error.stack);
}
}
org: string): Promise {
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(user.name);
if (userExists) {
throw new Error('An identity for the user ' + user.name + ' already exists in the wallet');
}
const attrs = user.attrs;
attrs.push({name: 'vehicle_manufacture.username', value: user.name + '@' + org, ecert: true});
// Register the user, enroll the user, and import the new identity into the wallet.
const secret = await ca.register({ affiliation: '', enrollmentID: user.name, role: 'client', attrs }, admin);
const enrollment = await ca.enroll({ enrollmentID: user.name, enrollmentSecret: secret });
const userIdentity = X509WalletMixin.createIdentity(mspid, enrollment.certificate, enrollment.key.toBytes());
await wallet.import(user.name, userIdentity);
}
}
async function _evaluateTransaction(org, chaincode, network_config){
const ccp = network_config['common-connection-profile'];
const orgConfig = ccp.organizations[org];
const cert = common.readAllFiles(orgConfig.signedCertPEM)[0];
const key = common.readAllFiles(orgConfig.adminPrivateKeyPEM)[0];
const inMemoryWallet = new InMemoryWallet();
const gateway = new Gateway();
try {
await inMemoryWallet.import('admin', X509WalletMixin.createIdentity(orgConfig.mspid, cert, key));
const opts = {
wallet: inMemoryWallet,
identity: 'admin',
discovery: { enabled: false }
};
await gateway.connect(ccp, opts);
const network = await gateway.getNetwork(chaincode.channelId)
const contract = await network.getContract(chaincode.chaincodeId);
const args = [chaincode.fcn, ...chaincode.args];
const result = await contract.evaluateTransaction(...args);
gateway.disconnect();
async function _submitTransaction(org, chaincode, network_config){
const ccp = network_config['common-connection-profile'];
const orgConfig = ccp.organizations[org];
const cert = common.readAllFiles(orgConfig.signedCertPEM)[0];
const key = common.readAllFiles(orgConfig.adminPrivateKeyPEM)[0];
const inMemoryWallet = new InMemoryWallet();
const gateway = new Gateway();
try {
await inMemoryWallet.import('admin', X509WalletMixin.createIdentity(orgConfig.mspid, cert, key));
const opts = {
wallet: inMemoryWallet,
identity: 'admin',
discovery: { enabled: false }
};
await gateway.connect(ccp, opts);
const network = await gateway.getNetwork(chaincode.channelId)
const contract = await network.getContract(chaincode.chaincodeId);
const args = [chaincode.fcn, ...chaincode.args];
const result = await contract.submitTransaction(...args);
gateway.disconnect();