Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'fabric-ca-client' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
return new Promise((resolve, reject) => {
if (user && user.isEnrolled()) {
console.log('Successfully loaded member from persistence');
return resolve(user);
}
// need to enroll it with CA server
let cop = new copService(caUrl);
let member;
return cop.enroll({
enrollmentID: username,
enrollmentSecret: password
}).then((enrollment) => {
console.log('Successfully enrolled user \'' + username + '\'');
member = new User(username, client);
return member.setEnrollment(enrollment.key, enrollment.certificate, mspid);
}).then(() => {
return client.setUserContext(member);
}).then(() => {
return resolve(member);
}).catch((err) => {
console.log('Failed to enroll and persist user. Error: ' + err.stack ? err.stack : err);
}).then((enrollment) => {
let cert;
try {
cert = X509.parseCert(FabricCAServices.normalizeX509(enrollment.certificate));
} catch (err) {
t.fail(util.format('Failed to parse enrollment cert\n%s\n. Error: %s', enrollment.certificate, err));
}
if (!cert.extensions || !cert.extensions.authorityKeyIdentifier) {
t.fail(util.format('Parsed certificate does not contain Authority Key Identifier needed for revoke(): %j', cert));
}
// convert the raw AKI string in the form of 'keyid:HX:HX....' (HX represents a hex-encoded byte) to a hex string
const akiString = cert.extensions.authorityKeyIdentifier;
const arr = akiString.split(':');
if (arr[0] !== 'keyid') {
t.fail(util.format('Found an Autheority Key Identifier we do not understand: first segment is not "keyid": %s', akiString));
}
arr.shift(); // remove the 'keyid'
test('\n\n ** FabricCAClient: Test enroll With Static CSR **\n\n', (t) => {
const endpoint = FabricCAServices._parseURL(fabricCAEndpoint);
const client = new FabricCAClient({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
tlsOptions: tlsOptions,
caname: ORGS[userOrg].ca.name
});
return client.enroll(enrollmentID, enrollmentSecret, csr.toString())
.then((enrollResponse) => {
t.pass('Successfully invoked enroll API with enrollmentID \'' + enrollmentID + '\'');
// check that we got back the expected certificate
let subject;
try {
subject = X509.getSubject(FabricCAServices.normalizeX509(enrollResponse.enrollmentCert));
} catch (err) {
async function timeOutTest(signingIdentity, t) {
const CONNECTION_TIMEOUT = FabricCAServices.getConfigSetting('connection-timeout');
t.equal(CONNECTION_TIMEOUT, 3000, 'connection-timeout should have default value 3000');
const SO_TIMEOUT = FabricCAServices.getConfigSetting('socket-operation-timeout');
t.equal(SO_TIMEOUT, undefined, 'socket-operation-timeout should have default value undefined');
let start, end;
// test CONNECTION_TIMEOUT
// Connect to a non-routable IP address should throw error connection_timeout
try {
const caClient = new FabricCAServices('http://10.255.255.1:3000')._fabricCAClient;
start = Date.now();
await caClient.request('GET', '/aMethod', signingIdentity);
t.fail('Should throw error by CONNECTION_TIMEOUT');
} catch (e) {
end = Date.now();
logger.debug('Conection failed with error ' + e.toString());
if (e.message === 'Calling /aMethod endpoint failed, CONNECTION Timeout') {
function getFabricCAService() {
FabricCAServices.addConfigFile(path.join(__dirname, 'e2e', 'config.json'));
ORGS = FabricCAServices.getConfigSetting('test-network');
fabricCAEndpoint = ORGS[userOrg].ca.url;
FabricCAServices.getConfigSetting('crypto-keysize', '256');// force for npm test
FabricCAServices.setConfigSetting('crypto-hash-algo', 'SHA2');// force for npm test
return new FabricCAServices(fabricCAEndpoint, tlsOptions, ORGS[userOrg].ca.name);
}
async function timeOutTest(signingIdentity, t) {
const CONNECTION_TIMEOUT = FabricCAServices.getConfigSetting('connection-timeout');
t.equal(CONNECTION_TIMEOUT, 3000, 'connection-timeout should have default value 3000');
const SO_TIMEOUT = FabricCAServices.getConfigSetting('socket-operation-timeout');
t.equal(SO_TIMEOUT, undefined, 'socket-operation-timeout should have default value undefined');
let start, end;
// test CONNECTION_TIMEOUT
// Connect to a non-routable IP address should throw error connection_timeout
try {
const caClient = new FabricCAServices('http://10.255.255.1:3000')._fabricCAClient;
start = Date.now();
await caClient.request('GET', '/aMethod', signingIdentity);
t.fail('Should throw error by CONNECTION_TIMEOUT');
} catch (e) {
end = Date.now();
logger.debug('Conection failed with error ' + e.toString());
if (e.message === 'Calling /aMethod endpoint failed, CONNECTION Timeout') {
// for connection timeout, verify the timeout value
t.equal(Math.floor((end - start) / 1000), 3, 'should have duration roughly equals 3000');
function getFabricCAService() {
FabricCAServices.addConfigFile(path.join(__dirname, 'e2e', 'config.json'));
ORGS = FabricCAServices.getConfigSetting('test-network');
fabricCAEndpoint = ORGS[userOrg].ca.url;
FabricCAServices.getConfigSetting('crypto-keysize', '256');// force for npm test
FabricCAServices.setConfigSetting('crypto-hash-algo', 'SHA2');// force for npm test
return new FabricCAServices(fabricCAEndpoint, tlsOptions, ORGS[userOrg].ca.name);
}
// User tests //////////
var User = require('fabric-client/lib/User.js');
var memberName = 'Donald T. Duck';
var enrollmentID = 123454321;
var roles = ['admin', 'user'];
var memberCfg = {
'enrollmentID': enrollmentID,
'roles': roles
};
var _client = null;
// End: User tests //////
// FabricCoPServices tests /////////
var FabricCAServices = require('fabric-ca-client/lib/FabricCAClientImpl');
var FabricCAClient = FabricCAServices.FabricCAClient;
// End: FabricCoPServices tests ////
// GRPC Options tests ///////////////
var Remote = require('fabric-client/lib/Remote.js');
var Peer = require('fabric-client/lib/Peer.js');
var Orderer = require('fabric-client/lib/Orderer.js');
var Config = require('fabric-client/lib/Config.js');
var aPem = '-----BEGIN CERTIFICATE-----' +
'MIIBwTCCAUegAwIBAgIBATAKBggqhkjOPQQDAzApMQswCQYDVQQGEwJVUzEMMAoG' +
'A1UEChMDSUJNMQwwCgYDVQQDEwNPQkMwHhcNMTYwMTIxMjI0OTUxWhcNMTYwNDIw' +
'MjI0OTUxWjApMQswCQYDVQQGEwJVUzEMMAoGA1UEChMDSUJNMQwwCgYDVQQDEwNP' +
'QkMwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAR6YAoPOwMzIVi+P83V79I6BeIyJeaM' +
'meqWbmwQsTRlKD6g0L0YvczQO2vp+DbxRN11okGq3O/ctcPzvPXvm7Mcbb3whgXW' +
'RjbsX6wn25tF2/hU6fQsyQLPiJuNj/yxknSjQzBBMA4GA1UdDwEB/wQEAwIChDAP' +
'BgNVHRMBAf8EBTADAQH/MA0GA1UdDgQGBAQBAgMEMA8GA1UdIwQIMAaABAECAwQw' +
'CgYIKoZIzj0EAwMDaAAwZQIxAITGmq+x5N7Q1jrLt3QFRtTKsuNIosnlV4LR54l3' +
var path = require('path');
var hfc = require('fabric-client');
hfc.setLogger(logger);
var fs = require('fs');
var grpc = require('grpc');
var util = require('util');
var testUtil = require('./pte-util.js');
var utils = require('fabric-client/lib/utils.js');
var Peer = require('fabric-client/lib/Peer.js');
var Orderer = require('fabric-client/lib/Orderer.js');
var EventHub = require('fabric-client/lib/EventHub.js');
var FabricCAServices = require('fabric-ca-client/lib/FabricCAClientImpl');
var FabricCAClient = FabricCAServices.FabricCAClient;
var User = require('fabric-client/lib/User.js');
var Client = require('fabric-client/lib/Client.js');
var _commonProto = grpc.load(path.join(__dirname, 'node_modules/fabric-client/lib/protos/common/common.proto')).common;
const crypto = require('crypto');
utils.setConfigSetting('crypto-keysize', 256);
// local vars
var tmp;
var tCurr;
var tEnd;
var tLocal;
var i = 0;
var inv_m = 0; // counter of invoke move
t.fail('Calling non-routable endpoint failed with unexpected error: ' + e.toString());
}
}
// create a mock server, the mock server wait for 10 seconds until send response
const mockServer = http.createServer((req, res) => {
setTimeout(() => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Response');
res.end();
}, 10000);
});
mockServer.listen(3000);
// set SO_TIMEOUT to 5000
FabricCAServices.setConfigSetting('socket-operation-timeout', 5000);
// test SO_TIMEOUT
try {
const caClient = new FabricCAServices('http://localhost:3000')._fabricCAClient;
start = Date.now();
await caClient.request('GET', '/aMethod', signingIdentity);
t.fail('Should throw error by SO_TIMEOUT');
} catch (e) {
end = Date.now();
t.equal(Math.floor((end - start) / 1000), 5, 'should have duration roughly equals 5000');
if (e.message.includes('endpoint failed')) {
t.pass('Successfully throw error after SO_TIMEOUT');
} else {
t.fail('did not throw error after SO_TIMEOUT');
}
mockServer.close();