def test_fee_info():
    message: Dict = dict(
        message_type='FeeInfo',
        token_network_address='0x82dd0e0eA3E84D00Cc119c46Ee22060939E5D1FC',
        chain_id=1,
        channel_identifier=123,
        nonce=1,
        percentage_fee=10000,
        signature='signature'
    )
    assert message == Message.deserialize(message).serialize_data()
    message['message_type'] = 'FeeInfo'
    assert isinstance(Message.deserialize(message), FeeInfo)
Beispiel #2
0
def test_paths_request():
    message: Dict = dict(
        message_type='PathsRequest',
        token_network_address='0x82dd0e0eA3E84D00Cc119c46Ee22060939E5D1FC',
        source_address='0x82dd0e0eA3E84D00Cc119c46Ee22060939E5D1FC',
        target_address='0x82dd0e0eA3E84D00Cc119c46Ee22060939E5D1FC',
        value=1000,
        num_paths=1,
        chain_id=1,
        nonce=1,
        signature='signature')

    assert message == Message.deserialize(message).serialize_data()
    message['message_type'] = 'PathsRequest'
    assert isinstance(Message.deserialize(message), PathsRequest)
Beispiel #3
0
def test_serialize_deserialize(get_random_bp, get_random_privkey):
    bp = get_random_bp()
    privkey = get_random_privkey()
    bp.signature = encode_hex(sign_data(privkey, bp.serialize_bin()))
    serialized_message = bp.serialize_full()

    deserialized_message = Message.deserialize(serialized_message)
    assert isinstance(deserialized_message, BalanceProof)
def test_deserialize_with_required_type():
    message: Dict = dict(
        message_type='FeeInfo',
        token_network_address='0x82dd0e0eA3E84D00Cc119c46Ee22060939E5D1FC',
        chain_id=1,
        channel_identifier=123,
        nonce=1,
        percentage_fee=1000,
        signature='signature'
    )

    deserialized_message = Message.deserialize(message, FeeInfo)
    assert isinstance(deserialized_message, FeeInfo)

    # during deserialization the `message_type` is removed, add it back
    message['message_type'] = 'FeeInfo'
    with pytest.raises(MessageTypeError):
        Message.deserialize(message, BalanceProof)
Beispiel #5
0
def test_paths_reply():
    message: Dict = dict(
        message_type='PathsReply',
        token_network_address='0x82dd0e0eA3E84D00Cc119c46Ee22060939E5D1FC',
        target_address='0x82dd0e0eA3E84D00Cc119c46Ee22060939E5D1FC',
        value=1000,
        chain_id=1,
        nonce=1,
        paths_and_fees=[{
            'estimated_fee': 10000,
            'paths': [['0x82dd0e0eA3E84D00Cc119c46Ee220609', '0xA3E84D00Cc119c46Ee22060939E5D1FC']]
        }],
        signature='signature'
    )

    assert message == Message.deserialize(message).serialize_data()
    message['message_type'] = 'PathsReply'
    assert isinstance(Message.deserialize(message), PathsReply)
Beispiel #6
0
    def put(self, token_network_address: str, channel_id: str):
        token_network_error = self._validate_token_network_argument(
            token_network_address)
        if token_network_error is not None:
            return token_network_error

        channel_id_error = self._validate_channel_id_argument(channel_id)
        if channel_id_error is not None:
            return channel_id_error

        body = request.json
        try:
            balance_proof: BalanceProof = Message.deserialize(
                body, BalanceProof)
        except MessageTypeError:
            return {'error', 'Not a BalanceProof message'}, 400
        except ValidationError as val_err:
            return {'error': val_err.message}, 400

        # token_network_address and channel_id info is duplicate
        # check that both versions match
        if not is_same_address(token_network_address,
                               balance_proof.token_network_address):
            error = 'The token network address from the balance proof ({}) ' \
                    'and the request ({}) do not match'
            return {
                'error':
                error.format(balance_proof.token_network_address,
                             token_network_address)
            }, 400

        if not channel_id == str(balance_proof.channel_identifier):
            error = 'The channel identifier from the balance proof ({}) ' \
                    'and the request ({}) do not match'
            return {
                'error': error.format(balance_proof.channel_identifier,
                                      channel_id)
            }, 400

        # Note: signature check is performed by the PathfindingService
        if not self.pathfinding_service.follows_token_network(
                balance_proof.token_network_address):
            return {
                'error':
                'Unsupported token network: {}'.format(token_network_address)
            }, 400

        try:
            self.pathfinding_service.on_balance_proof_message(balance_proof)
        except ValueError as error:
            return {
                'error':
                'Error while updating balance proof: {}'.format(str(error))
            }, 400
Beispiel #7
0
 def run_message_callbacks(self, data):
     """Called whenever a message is received"""
     # ignore if message is not a JSON
     try:
         json_msg = json.loads(data)
     except json.decoder.JSONDecodeError:
         return
     # ignore message if JSON schema validation fails
     try:
         deserialized_msg = Message.deserialize(json_msg)
     except (jsonschema.exceptions.ValidationError, MessageSignatureError,
             MessageFormatError):
         return
     for callback in self.message_callbacks:
         callback(deserialized_msg)
Beispiel #8
0
    def run_message_callbacks(self, data):
        """Called whenever a message is received"""
        # ignore if message is not a JSON
        try:
            json_msg = json.loads(data)
        except json.decoder.JSONDecodeError as ex:
            log.error('Error when reading JSON: %s', str(ex))
            return

        # ignore message if JSON schema validation fails
        try:
            deserialized_msg = Message.deserialize(json_msg)
        except (jsonschema.exceptions.ValidationError,
                MessageFormatError) as ex:
            log.error('Error when deserializing message: %s', str(ex))
            return

        for callback in self.message_callbacks:
            callback(deserialized_msg)