def create_envelope(seek_payload_sig, seek_payload_bytes): envelope = common_pb2.Envelope() envelope.signature = seek_payload_sig envelope.payload = seek_payload_bytes return envelope
def extract_channel_config(configtx_proto_envelope): """Extracts the protobuf 'ConfigUpdate' object out ouf the 'ConfigEnvelope'. :param configtx_proto_envelope: The encoded bytes of the ConfigEnvelope protofbuf. :type configtx_proto_envelope: common_pb2.Envelope :return: (config_update) The encoded bytes of the ConfigUpdate protobuf, ready to be signed :rtype: configtx_pb2.ConfigUpadeEnvelope.config_update :raises ValueError: If there is an error in protobuf_decode due to a wrong or not valid profobuf file a ValueError is raised. """ _logger.debug('extract_channel_config - start') try: envelope = common_pb2.Envelope() envelope.ParseFromString(configtx_proto_envelope) payload = common_pb2.Payload() payload.ParseFromString(envelope.payload) configtx = configtx_pb2.ConfigUpdateEnvelope() configtx.ParseFromString(payload.data) except DecodeError as e: _logger.error('extract_channel_config - an error occurred decoding' ' the configtx_proto_envelope: {}'.format(e)) raise ValueError( 'The given configtx_proto_envelope was not valid: {}'.format(e)) return configtx.config_update
def extract_channel_config(config_envelope): """Extracts the protobuf 'ConfigUpdate' out of the 'ConfigEnvelope' that is produced by the configtxgen tool The returned object may then be signed using sign_channel_config() method. Once all the signatures have been collected, the 'ConfigUpdate' object and the signatures may be used on create_channel() or update_channel() calls Args: config_envelope (bytes): encoded bytes of the ConfigEnvelope protobuf Returns: config_update (bytes): encoded bytes of ConfigUpdate protobuf, ready to be signed """ _logger.debug('extract_channel_config start') envelope = common_pb2.Envelope() envelope.ParseFromString(config_envelope) payload = common_pb2.Payload() payload.ParseFromString(envelope.payload) configtx = configtx_pb2.ConfigUpdateEnvelope() configtx.ParseFromString(payload.data) config_update = configtx.ConfigUpdate return config_update.SerializeToString()
def _get_latest_block(self, tx_context, orderer): """ Get latest block from orderer. Args: tx_context (object): a tx_context instance orderer (object): a orderer instance """ seek_info = ab_pb2.SeekInfo() seek_info.start.newest = ab_pb2.SeekNewest() seek_info.stop.newest = ab_pb2.SeekNewest() seek_info.behavior = \ ab_pb2.SeekInfo.SeekBehavior.Value('BLOCK_UNTIL_READY') seek_info_header = self._build_channel_header( common_pb2.HeaderType.Value('DELIVER_SEEK_INFO'), tx_context.tx_id, self._name, current_timestamp(), tx_context.epoch) signature_header = common_pb2.SignatureHeader() signature_header.creator = tx_context.identity signature_header.nonce = tx_context.nonce seek_payload = common_pb2.Payload() seek_payload.header.signature_header = \ signature_header.SerializeToString() seek_payload.header.channel_header = \ seek_info_header.SerializeToString() seek_payload.data = seek_info.SerializeToString() envelope = common_pb2.Envelope() envelope.signature = tx_context.sign(seek_payload.SerializeToString()) envelope.payload = seek_payload.SerializeToString()
def sign_tran_payload(tx_context, tran_payload_bytes): """Sign a transaction payload :param signing_identity: id to sign with :param tran_payload: transaction payload to sign on :param tx_context: :param tran_payload_bytes: :return: Envelope """ sig = tx_context.sign(tran_payload_bytes) envelope = common_pb2.Envelope() envelope.signature = sig envelope.payload = tran_payload_bytes return envelope
def sign_tran_payload(tx_context, tran_payload_bytes): """Sign a transaction payload Args: signing_identity: id to sign with tran_payload: transaction payload to sign on Returns: Envelope """ sig = tx_context.sign(tran_payload_bytes) envelope = common_pb2.Envelope() envelope.signature = sig envelope.payload = tran_payload_bytes return envelope
def sign_tran_payload(signing_identity, tran_payload): """Sign a transaction payload Args: signing_identity: id to sign with tran_payload: transaction payload to sign on Returns: Envelope """ tran_payload_bytes = tran_payload.SerializeToString() sig = signing_identity.sign(tran_payload_bytes) envelope = common_pb2.Envelope() envelope.signature = sig envelope.payload = tran_payload_bytes 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 decode_block_data(proto_block_data, not_proto=False): """Decodes the data of Block. :param proto_block_data: Block Data proto. :type proto_block_data: str :param not_proto: Boolean for if proto. :return: deserialized block_data (Default value = False) :type not_proto: bool """ data = {} data['data'] = [] for i in proto_block_data.data: proto_envelope = None if not_proto: proto_envelope = common_pb2.Envelope() proto_envelope.ParseFromString(i) if proto_envelope: envelope = decode_block_data_envelope(proto_envelope) data['data'].append(envelope) return data
def decode_block_data(proto_block_data, not_proto=False): """Decodes the data of Block. Args: proto_block_data (str): Block Data proto. not_proto (bool): Boolean for if proto. Returns: deserialized block_data """ data = {} data['data'] = [] for i in proto_block_data.data: proto_envelope = None if not_proto: proto_envelope = common_pb2.Envelope() proto_envelope.ParseFromString(i) if proto_envelope: envelope = decode_block_data_envelope(proto_envelope) data['data'].append(envelope) return data
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)