Skip to content

Commit b553747

Browse files
hackaugustoLefterisJP
authored andcommitted
hexaddress decoder test
1 parent f87ceb8 commit b553747

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

raiden/api/v1/encoding.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@
3535
)
3636

3737

38-
# type converter for the flask route
3938
class HexAddressConverter(BaseConverter):
40-
4139
def to_python(self, value):
42-
value = address_decoder(value)
43-
return value
40+
if value[:2] == '0x':
41+
return value[2:].decode('hex')
42+
43+
raise Exception('invalid address')
4444

4545
def to_url(self, value):
46-
value = address_encoder(value)
47-
return value
46+
return address_encoder(value)
4847

4948

5049
class AddressField(fields.Field):

raiden/tests/api/test_api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pyethapp.jsonrpc import address_encoder, address_decoder
99

10+
from raiden.api.v1.encoding import HexAddressConverter
1011
from raiden.tests.utils.apitestcontext import decode_response
1112
from raiden.utils import channel_to_api_dict
1213
from raiden.tests.utils.transfer import channel
@@ -40,6 +41,25 @@ def api_url_for(api_backend, endpoint, **kwargs):
4041
return url_for('v1_resources.{}'.format(endpoint), **kwargs)
4142

4243

44+
def test_hex_converter():
45+
converter = HexAddressConverter(map=None)
46+
47+
# invalid hex data
48+
with pytest.raises(Exception):
49+
converter.to_python('-')
50+
51+
# invalid address, too short
52+
with pytest.raises(Exception):
53+
converter.to_python('1234')
54+
55+
# missing prefix 0x
56+
with pytest.raises(Exception):
57+
converter.to_python('414d72a6f6e28f4950117696081450d63d56c354')
58+
59+
address = b'AMr\xa6\xf6\xe2\x8fIP\x11v\x96\x08\x14P\xd6=V\xc3T'
60+
assert converter.to_python('0x414d72a6f6e28f4950117696081450d63d56c354') == address
61+
62+
4363
@pytest.mark.parametrize('blockchain_type', ['geth'])
4464
@pytest.mark.parametrize('number_of_nodes', [2])
4565
def test_channel_to_api_dict(raiden_network, token_addresses, settle_timeout):

0 commit comments

Comments
 (0)