def create_seek_payload(seek_header, seek_info): seek_payload = common_pb2.Payload() seek_payload.header.CopyFrom(seek_header) seek_payload.data = seek_info.SerializeToString() seek_payload_bytes = seek_payload.SerializeToString() return seek_payload_bytes
def decode_block_data_envelope(proto_envelope): """Decodes the envelope contents of Block Args: proto_envelope (str): Envelope proto Returns: deserialized block envelope """ envelope = {} envelope['signature'] = proto_envelope.signature envelope['payload'] = {} proto_payload = common_pb2.Payload() proto_payload.ParseFromString(proto_envelope.payload) envelope['payload']['header'] = decode_header(proto_payload.header) # TODO: add envelope['payload']['data'] & ['payload']['header'] return envelope
def get_channel_config(self, requestor, channel_name, peer_names, decode=True): """ Get configuration block for the channel :param requestor: User role who issue the request :param channel_name: name of channel to query :param peer_names: Names of the peers to query :param deocode: Decode the response payload :return: A `ChaincodeQueryResponse` or `ProposalResponse` """ peers = [] for peer_name in peer_names: peer = self.get_peer(peer_name) peers.append(peer) channel = self.get_channel(channel_name) tx_context = create_tx_context(requestor, ecies(), TXProposalRequest()) responses = channel.get_channel_config(tx_context, peers) try: if responses[0][0].response and decode: _logger.debug('response status {}'.format( responses[0][0].response.status)) block = common_pb2.Block() block.ParseFromString(responses[0][0].response.payload) envelope = common_pb2.Envelope() envelope.ParseFromString(block.data.data[0]) payload = common_pb2.Payload() payload.ParseFromString(envelope.payload) config_envelope = configtx_pb2.ConfigEnvelope() config_envelope.ParseFromString(payload.data) return config_envelope return responses[0][0] except Exception: _logger.error("Failed to get channel config block: {}", sys.exc_info()[0]) raise
def create_tx_payload(endorsements, tran_req): cc_action_payload = transaction_pb2.ChaincodeActionPayload() response, _ = tran_req.responses[0] cc_action_payload.action.proposal_response_payload = \ response.payload cc_action_payload.action.endorsements.extend(endorsements) cc_action_payload.chaincode_proposal_payload = tran_req.proposal.payload tran = transaction_pb2.Transaction() cc_tran_action = tran.actions.add() cc_tran_action.header = tran_req.header.signature_header cc_tran_action.payload = cc_action_payload.SerializeToString() tran_payload = common_pb2.Payload() tran_payload.header.channel_header = tran_req.header.channel_header tran_payload.header.signature_header = tran_req.header.signature_header tran_payload.data = tran.SerializeToString() tran_payload_bytes = tran_payload.SerializeToString() return tran_payload_bytes
def decode_block_data_envelope(proto_envelope): """Decodes the envelope contents of Block :param proto_envelope: Envelope proto :return: deserialized block envelope :type proto_envelope: str """ envelope = {} envelope['signature'] = proto_envelope.signature envelope['payload'] = {} proto_payload = common_pb2.Payload() proto_payload.ParseFromString(proto_envelope.payload) envelope['payload']['header'] = decode_header(proto_payload.header) envelope['payload']['data'] = \ HeaderType.decode_payload_based_on_type( proto_payload.data, envelope['payload']['header']['channel_header']['type']) envelope['payload']['header']['channel_header']['type_string'] = \ HeaderType.convert_to_string( envelope['payload']['header']['channel_header']['type']) return envelope
def decode_config_envelope(config_envelope_bytes): """Decodes configuration envelope :param config_envelope_bytes: byte of config envelope :return: deserialized config envelope """ config_envelope = {} proto_config_envelope = configtx_pb2.ConfigEnvelope() proto_config_envelope.ParseFromString(config_envelope_bytes) config_envelope['config'] = decode_config(proto_config_envelope.config) config_envelope['last_update'] = {} proto_last_update = proto_config_envelope.last_update if proto_last_update: config_envelope['last_update']['payload'] = {} proto_payload = common_pb2.Payload() proto_payload.ParseFromString(proto_last_update.payload) config_envelope['last_update']['payload']['header'] = \ decode_header(proto_payload.header) config_envelope['last_update']['payload']['data'] = \ decode_config_update_envelope(proto_payload.data) config_envelope['last_update']['signature'] = \ proto_last_update.signature return config_envelope
def _create_or_update_channel_request(self, request, have_envelope): """Inits the create of update channel process. Args: request (dct): A create_update channel request. have_envelope (bool): Signals if the requests contains a finished protobuf envelope. Returns: rx.Observable: An observable for the orderer_response or an error. """ _logger.debug('_create_or_update_channel - start') error_msg = None if 'config' not in request and not have_envelope: error_msg = 'Missing config request parameter containing ' \ 'the configuration of the channel' if 'signatures' not in request and not have_envelope: error_msg = 'Missing signatures request parameter for the ' \ 'new channel' elif 'signatures' in request and \ not isinstance(request['signatures'], list) \ and not have_envelope: error_msg = 'Signatures request parameter must be an array ' \ 'of signatures' if 'tx_id' not in request and not have_envelope: error_msg = 'Missing tx_id request parameter' if 'nonce' not in request and not have_envelope: error_msg = 'Missing nonce request parameter' if 'orderer' not in request: error_msg = 'Missing orderer request parameter' if 'channel_name' not in request: error_msg = 'Missing channel_name request parameter' if error_msg: _logger.error( '_create_or_update_channel error: {}'.format(error_msg)) raise ValueError(error_msg) if have_envelope: _logger.debug('_create_or_update_channel - have envelope') envelope = common_pb2.Envelope() envelope.ParseFromString(request['envelope']) signature = envelope.signature payload = envelope.payload else: _logger.debug('_create_or_update_channel - have config_update') proto_config_update_envelope = configtx_pb2.ConfigUpdateEnvelope() proto_config_update_envelope.config_update = request['config'] # convert signatures to protobuf signature signatures = request['signatures'] proto_signatures = utils.string_to_signature(signatures) proto_config_update_envelope.signatures.extend(proto_signatures) proto_channel_header = utils.build_channel_header( common_pb2.HeaderType.Value('CONFIG_UPDATE'), request['tx_id'], request['channel_name'], utils.current_timestamp()) proto_header = utils.build_header(self.tx_context.identity, proto_channel_header, request['nonce']) proto_payload = common_pb2.Payload() proto_payload.header.CopyFrom(proto_header) proto_payload.data = proto_config_update_envelope \ .SerializeToString() payload_bytes = proto_payload.SerializeToString() signature_bytes = self.tx_context.sign(payload_bytes) signature = signature_bytes payload = payload_bytes # assemble the final envelope out_envelope = common_pb2.Envelope() out_envelope.signature = signature out_envelope.payload = payload orderer = request['orderer'] return orderer.broadcast(out_envelope)
def send_transaction(orderers, tran_req, signing_identity, scheduler=None): """Send a transaction to the chain's orderer service (one or more orderer endpoints) for consensus and committing to the ledger. This call is asynchronous and the successful transaction commit is notified via a BLOCK or CHAINCODE event. This method must provide a mechanism for applications to attach event listeners to handle 'transaction submitted', 'transaction complete' and 'error' events. Args: scheduler: scheduler signing_identity: signing identity orderers: orderers tran_req (TransactionRequest): The transaction object Returns: result (EventEmitter): an handle to allow the application to attach event handlers on 'submitted', 'complete', and 'error'. """ if not tran_req: return rx.Observable.just(ValueError( "Missing input request object on the transaction request" )) if not tran_req.responses or len(tran_req.responses) < 1: return rx.Observable.just(ValueError( "Missing 'proposalResponses' parameter in transaction request" )) if not tran_req.proposal: return rx.Observable.just(ValueError( "Missing 'proposalResponses' parameter in transaction request" )) if len(orderers) < 1: return rx.Observable.just(ValueError( "Missing orderer objects on this chain" )) endorsements = map(lambda res: res[0].endorsement, tran_req.responses) cc_action_payload = transaction_pb2.ChaincodeActionPayload() response, _ = tran_req.responses[0] cc_action_payload.action.proposal_response_payload = \ response.payload cc_action_payload.action.endorsements.extend(endorsements) cc_action_payload.chaincode_proposal_payload = tran_req.proposal.payload tran = transaction_pb2.Transaction() cc_tran_action = tran.actions.add() cc_tran_action.header = tran_req.header.signature_header cc_tran_action.payload = cc_action_payload.SerializeToString() tran_payload = common_pb2.Payload() tran_payload.header.channel_header = tran_req.header.channel_header tran_payload.header.signature_header = tran_req.header.signature_header tran_payload.data = tran.SerializeToString() envelope = sign_tran_payload(signing_identity, tran_payload) if sys.version_info < (3, 0): orderer = random.choice(orderers.values()) else: orderer = random.choice(list(orderers.values())) return orderer.broadcast(envelope, scheduler)