Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'fabric-protos' 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.
const tape = require('tape');
const _test = require('tape-promise').default;
const test = _test(tape);
const util = require('util');
const fs = require('fs');
const path = require('path');
const e2eUtils = require('../e2e/e2eUtils.js');
const Client = require('fabric-client');
const testUtil = require('../../unit/util.js');
let ORGS;
const commonProto = require('fabric-protos').common;
const ccProto = require('fabric-protos').protos;
const client = new Client();
const org = 'org1';
const total = 1000;
const proposals = [];
const DESC = '\n\n** gRPC peer client low-level API performance **';
test(DESC, (t) => {
perfTest3(t);
t.end();
});
async function perfTest3(t) {
testUtil.resetDefaults();
Client.setConfigSetting('key-value-store', 'fabric-ca-client/lib/impl/FileKeyValueStore.js');// force for 'gulp test'
'use strict';
const tape = require('tape');
const _test = require('tape-promise').default;
const test = _test(tape);
const rewire = require('rewire');
const fs = require('fs-extra');
const path = require('path');
const utils = rewire('fabric-client/lib/utils.js');
const testutil = require('./util.js');
const MSP = require('fabric-client/lib/msp/msp.js');
const MSPM = require('fabric-client/lib/msp/msp-manager.js');
const {Identity} = require('fabric-common');
const mspProto = require('fabric-protos').msp;
const FABRIC = 0;
const TEST_CERT_PEM = require('./constants').TEST_CERT_PEM;
test('\n\n** MSP Tests **\n\n', async (t) => {
testutil.resetDefaults();
// construct MSP config objects for org0 and org1
const configs = [];
const mspm = new MSPM();
let config = loadMSPConfig('peerOrg0', 'org0');
t.pass('Successfully loaded msp config for org0');
configs.push(config);
test('\n\n***** READ in the genesis block *****\n\n', (t) => {
testUtil.resetDefaults();
// readin the envelope to send to the orderer
const normalPath = path.normalize(path.join(__dirname, '../fixtures/crypto-material/config-base/twoorgs.genesis.block'));
const data = fs.readFileSync(normalPath);
const channel = new Channel('test', 'fake');
const block = _commonProto.Block.decode(data);
const envelope = _commonProto.Envelope.decode(block.data.data[0]);
const payload = _commonProto.Payload.decode(envelope.payload);
const channel_header = _commonProto.ChannelHeader.decode(payload.header.channel_header);
if (channel_header.type !== _commonProto.HeaderType.CONFIG) {
logger.error('Block must be of type "CONFIG"');
}
const config_envelope = _configtxProto.ConfigEnvelope.decode(payload.data);
channel.loadConfigEnvelope(config_envelope);
t.pass(' Loaded the geneisis block from the configtx tool');
t.end();
});
const chaincodeProposalPayloadNoTrans = new fabprotos.protos.ChaincodeProposalPayload();
chaincodeProposalPayloadNoTrans.setInput(originalChaincodeProposalPayload.input); // only set the input field, skipping the TransientMap
chaincodeActionPayload.setChaincodeProposalPayload(chaincodeProposalPayloadNoTrans.toBuffer());
const transactionAction = new fabprotos.protos.TransactionAction();
transactionAction.setHeader(header.getSignatureHeader());
transactionAction.setPayload(chaincodeActionPayload.toBuffer());
const actions = [];
actions.push(transactionAction);
const transaction = new fabprotos.protos.Transaction();
transaction.setActions(actions);
const payload = new fabprotos.common.Payload();
payload.setHeader(header);
payload.setData(transaction.toBuffer());
return payload;
}
const originalChaincodeProposalPayload = fabprotos.protos.ChaincodeProposalPayload.decode(this._endorsement._action.proposal.getPayload());
const chaincodeProposalPayloadNoTrans = new fabprotos.protos.ChaincodeProposalPayload();
chaincodeProposalPayloadNoTrans.setInput(originalChaincodeProposalPayload.input); // only set the input field, skipping the TransientMap
chaincodeActionPayload.setChaincodeProposalPayload(chaincodeProposalPayloadNoTrans.toBuffer());
const transactionAction = new fabprotos.protos.TransactionAction();
transactionAction.setHeader(this._endorsement._action.header.getSignatureHeader());
transactionAction.setPayload(chaincodeActionPayload.toBuffer());
const actions = [];
actions.push(transactionAction);
const transaction = new fabprotos.protos.Transaction();
transaction.setActions(actions);
this._action.payload = new fabprotos.common.Payload();
this._action.payload.setHeader(this._endorsement._action.header);
this._action.payload.setData(transaction.toBuffer());
this._payload = this._action.payload.toBuffer();
return this._payload;
}
deliver.on('data', (response) => {
logger.debug('sendDeliver - on data'); // response: %j', response);
// check the type of the response
if (response.Type === 'block') {
const blockHeader = new fabprotos.common.BlockHeader();
blockHeader.setNumber(response.block.header.number);
blockHeader.setPreviousHash(response.block.header.previous_hash);
blockHeader.setDataHash(response.block.header.data_hash);
const blockData = new fabprotos.common.BlockData();
blockData.setData(response.block.data.data);
const blockMetadata = new fabprotos.common.BlockMetadata();
blockMetadata.setMetadata(response.block.metadata.metadata);
const block = new fabprotos.common.Block();
block.setHeader(blockHeader);
block.setData(blockData);
block.setMetadata(blockMetadata);
return_block = block;
logger.debug('sendDeliver - wait for success, keep this block number %s', return_block.header.number);
} else if (response.Type === 'status') {
module.exports.buildChannelHeader = (type, channel_id, tx_id, epoch, chaincode_id, time_stamp, client_cert_hash) => {
logger.debug(
'buildChannelHeader - type %s channel_id %s tx_id %d epoch %s chaincode_id %s',
type, channel_id, tx_id, epoch, chaincode_id);
const channelHeader = new fabprotos.common.ChannelHeader();
channelHeader.setType(type); // int32
channelHeader.setVersion(1); // int32
if (!time_stamp) {
time_stamp = module.exports.buildCurrentTimestamp();
}
channelHeader.setChannelId(channel_id); // string
channelHeader.setTxId(tx_id.toString()); // string
if (epoch) {
channelHeader.setEpoch(epoch); // uint64
}
if (chaincode_id) {
const chaincodeID = new fabprotos.protos.ChaincodeID();
chaincodeID.setName(chaincode_id);
const headerExt = new fabprotos.protos.ChaincodeHeaderExtension();
headerExt.setChaincodeId(chaincodeID);
static buildPolicy(msps, policy, returnProto) {
const principals = [];
const envelope = new fabprotos.common.SignaturePolicyEnvelope();
if (!policy) {
// no policy was passed in, construct a 'Signed By any member of an organization by mspid' policy
// construct a list of msp principals to select from using the 'n out of' operator
const signedBys = [];
let index = 0;
for (const name in msps) {
if (Object.prototype.hasOwnProperty.call(msps, name)) {
const onePrn = new fabprotos.common.MSPPrincipal();
onePrn.setPrincipalClassification(fabprotos.common.MSPPrincipal.Classification.ROLE);
const memberRole = new fabprotos.common.MSPRole();
memberRole.setRole(fabprotos.common.MSPRole.MSPRoleType.MEMBER);
memberRole.setMspIdentifier(name);
onePrn.setPrincipal(memberRole.toBuffer());
function decodeMSPPrincipal(proto_msp_principal) {
let msp_principal = {};
msp_principal.principal_classification = proto_msp_principal.getPrincipalClassification();
let proto_principal = null;
switch (msp_principal.principal_classification) {
case fabprotos.common.MSPPrincipal.Classification.ROLE:
proto_principal = fabprotos.common.MSPRole.decode(proto_msp_principal.getPrincipal());
msp_principal.msp_identifier = proto_principal.getMspIdentifier();
if (proto_principal.getRole() === 0) {
msp_principal.Role = 'MEMBER';
} else if (proto_principal.getRole() === 1) {
msp_principal.Role = 'ADMIN';
}
break;
case fabprotos.common.MSPPrincipal.Classification.ORGANIZATION_UNIT:
proto_principal = fabprotos.common.OrganizationUnit.decode(proto_msp_principal.getPrincipal());
msp_principal.msp_identifier = proto_principal.getMspIdentifier(); // string
msp_principal.organizational_unit_identifier = proto_principal.getOrganizationalUnitIdentifier(); // string
msp_principal.certifiers_identifier = proto_principal.getCertifiersIdentifier().toBuffer(); // bytes
break;
case fabprotos.common.MSPPrincipal.Classification.IDENTITY:
msp_principal = decodeIdentity(proto_msp_principal.getPrincipal());
break;
}
return msp_principal;
}
function decodeBlockDataEnvelope(proto_envelope) {
const envelope = {};
envelope.signature = proto_envelope.getSignature().toBuffer(); // leave as bytes
envelope.payload = {};
const proto_payload = fabprotos.common.Payload.decode(proto_envelope.getPayload().toBuffer());
envelope.payload.header = decodeHeader(proto_payload.getHeader());
envelope.payload.data = HeaderType.decodePayloadBasedOnType(proto_payload.getData().toBuffer(), envelope.payload.header.channel_header.type);
// let's also have the type as the enum string value so it is easier to read
envelope.payload.header.channel_header.typeString = HeaderType.convertToString(envelope.payload.header.channel_header.type);
return envelope;
}