Esempio n. 1
0
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
Esempio n. 2
0
    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()
Esempio n. 3
0
def decode_config_update_envelope(config_update_envelope_bytes):
    """Decode config update envelope

    :param config_update_envelope_bytes: Bytes of update envelope
    :type config_update_envelope_bytes: str
    :return: deserialized config update envelope signatures
    """
    config_update_envelope = {}
    proto_config_update_envelope = configtx_pb2.ConfigUpdateEnvelope()
    proto_config_update_envelope.ParseFromString(config_update_envelope_bytes)
    config_update_envelope['config_update'] = \
        decode_config_update(proto_config_update_envelope.config_update)
    signatures = []
    for signature in proto_config_update_envelope.signatures:
        proto_config_signature = signature
        config_signature = decode_config_signature(proto_config_signature)
        signatures.append(config_signature)
    config_update_envelope['signatures'] = signatures
    return config_update_envelope
Esempio n. 4
0
def decode_config_update_envelope(config_update_envelope_bytes):
    """Decode config update envelope

    Args:
        config_update_envelope_bytes (str): Bytes of update envelope

    Returns: deserialized config update envelope signatures
    """
    config_update_envelope = {}
    proto_config_update_envelope = configtx_pb2.ConfigUpdateEnvelope()
    proto_config_update_envelope.ParseFromString(config_update_envelope_bytes)
    config_update_envelope['config_update'] = \
        decode_config_update(proto_config_update_envelope.config_update)
    signatures = []
    for signature in proto_config_update_envelope.signatures:
        proto_config_signature = signature
        config_signature = decode_config_signature(proto_config_signature)
        signatures.push(config_signature)
    config_update_envelope['signatures'] = signatures
    return config_update_envelope
Esempio n. 5
0
    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)