Skip to content

Instantly share code, notes, and snippets.

@LefterisJP
Created June 2, 2020 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LefterisJP/cf0ccba5e915e76e62eb49203b503068 to your computer and use it in GitHub Desktop.
Save LefterisJP/cf0ccba5e915e76e62eb49203b503068 to your computer and use it in GitHub Desktop.
for_kelsakos.patch
diff --git a/rotkehlchen/api/v1/encoding.py b/rotkehlchen/api/v1/encoding.py
index 20e4829f..1d675c6c 100644
--- a/rotkehlchen/api/v1/encoding.py
+++ b/rotkehlchen/api/v1/encoding.py
@@ -3,7 +3,7 @@ from typing import Any, Callable, Dict, List, Mapping, Optional, Union
import marshmallow
import webargs
-from eth_utils import is_checksum_address
+from eth_utils import to_checksum_address
from marshmallow import Schema, fields, post_load, validates_schema
from marshmallow.exceptions import ValidationError
from webargs.compat import MARSHMALLOW_VERSION_INFO
@@ -822,13 +822,18 @@ def _validate_blockchain_account_schemas(
if data['blockchain'] == SupportedBlockchain.ETHEREUM:
for account_data in data['accounts']:
address = address_getter(account_data)
- if not is_checksum_address(address):
+ # Make sure that given value is an ethereum address
+ try:
+ address = to_checksum_address(address)
+ except (ValueError, TypeError):
raise ValidationError(
- f'Given value {address} is not a checksummed ethereum address',
+ f'Given value {address} is not an ethereum address',
+ field_name='address',
)
if address in given_addresses:
raise ValidationError(
f'Address {address} appears multiple times in the request data',
+ field_name='address',
)
given_addresses.add(address)
@@ -859,6 +864,17 @@ class BlockchainAccountsPatchSchema(Schema):
) -> None:
_validate_blockchain_account_schemas(data, lambda x: x['address'])
+ @post_load # type: ignore
+ def transform_data( # pylint: disable=no-self-use
+ self,
+ data: Dict[str, Any],
+ **_kwargs: Any,
+ ) -> Any:
+ if data['blockchain'] == SupportedBlockchain.ETHEREUM:
+ for idx, account in enumerate(data['accounts']):
+ data['accounts'][idx]['address'] = to_checksum_address(account['address'])
+ return data
+
class BlockchainAccountsPutSchema(BlockchainAccountsPatchSchema):
async_query = fields.Boolean(missing=False)
@@ -870,13 +886,23 @@ class BlockchainAccountsDeleteSchema(Schema):
async_query = fields.Boolean(missing=False)
@validates_schema # type: ignore
- def validate_blockchain_accounts_patch_schema( # pylint: disable=no-self-use
+ def validate_blockchain_accounts_delete_schema( # pylint: disable=no-self-use
self,
data: Dict[str, Any],
**_kwargs: Any,
) -> None:
_validate_blockchain_account_schemas(data, lambda x: x)
+ @post_load # type: ignore
+ def transform_data( # pylint: disable=no-self-use
+ self,
+ data: Dict[str, Any],
+ **_kwargs: Any,
+ ) -> Any:
+ if data['blockchain'] == SupportedBlockchain.ETHEREUM:
+ data['accounts'] = [to_checksum_address(x) for x in data['accounts']]
+ return data
+
class IgnoredAssetsSchema(Schema):
assets = fields.List(AssetField(), required=True)
diff --git a/rotkehlchen/tests/api/test_blockchain.py b/rotkehlchen/tests/api/test_blockchain.py
index c34e6484..e056c6df 100644
--- a/rotkehlchen/tests/api/test_blockchain.py
+++ b/rotkehlchen/tests/api/test_blockchain.py
@@ -5,6 +5,7 @@ from unittest.mock import patch
import pytest
import requests
+from eth_utils import to_checksum_address
from rotkehlchen.fval import FVal
from rotkehlchen.logging import RotkehlchenLogsAdapter
@@ -713,13 +714,11 @@ def test_add_blockchain_accounts_alethio(
@pytest.mark.parametrize('number_of_eth_accounts', [0])
-def test_addding_non_checksummed_eth_account_is_error(
- rotkehlchen_api_server,
-):
- """Test that adding a non checksummed eth account leads to an error"""
+def test_addding_non_checksummed_eth_account_works(rotkehlchen_api_server):
+ """Test that adding a non checksummed eth account can be handled properly"""
rotki = rotkehlchen_api_server.rest_api.rotkehlchen
account = '0x7bd904a3db59fa3879bd4c246303e6ef3ac3a4c6'
- new_eth_accounts = [account]
+ new_eth_accounts = [to_checksum_address(account)]
eth_balances = ['1000000']
setup = setup_balances(
rotki,
@@ -735,11 +734,7 @@ def test_addding_non_checksummed_eth_account_is_error(
"blockchainsaccountsresource",
blockchain='ETH',
), json=request_data)
- assert_error_response(
- response=response,
- contained_in_msg=f'Given value {account} is not a checksummed ethereum address',
- status_code=HTTPStatus.BAD_REQUEST,
- )
+ assert_proper_response(response)
@pytest.mark.parametrize('method', ['PUT', 'DELETE'])
@@ -798,7 +793,7 @@ def test_blockchain_accounts_endpoint_errors(rotkehlchen_api_server, api_port, m
if method == 'GET':
message = "'accounts': ['Not a valid list.'"
elif method == 'DELETE':
- message = 'Given value foo is not a checksummed ethereum address'
+ message = 'Given value foo is not an ethereum address'
else:
message = "'accounts': {0: {'_schema': ['Invalid input type.'"
assert_error_response(
@@ -822,7 +817,7 @@ def test_blockchain_accounts_endpoint_errors(rotkehlchen_api_server, api_port, m
# Provide invalid ETH account (more bytes)
invalid_eth_account = '0x554FFc77f4251a9fB3c0E3590a6a205f8d4e067d01'
- msg = f'Given value {invalid_eth_account} is not a checksummed ethereum address'
+ msg = f'Given value {invalid_eth_account} is not an ethereum address'
if method == 'PUT':
data = {'accounts': [{'address': invalid_eth_account}]}
else:
@@ -883,7 +878,7 @@ def test_blockchain_accounts_endpoint_errors(rotkehlchen_api_server, api_port, m
# Provide list with one valid and one invalid account and make sure that nothing
# is added / removed and the valid one is skipped
- msg = 'Given value 142 is not a checksummed ethereum address'
+ msg = 'Given value 142 is not an ethereum address'
if method == 'DELETE':
# Account should be an existing account
account = rotki.chain_manager.accounts.eth[0]
@@ -1235,7 +1230,7 @@ def test_edit_blockchain_account_errors(
), json=request_data)
assert_error_response(
response=response,
- contained_in_msg='Given value dsadsd is not a checksummed ethereum address',
+ contained_in_msg='Given value dsadsd is not an ethereum address',
status_code=HTTPStatus.BAD_REQUEST,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment