Skip to content

Commit

Permalink
Replace ABI Method is_encodable_type with the previous custom checkin…
Browse files Browse the repository at this point in the history
…g of types
  • Loading branch information
Bhargavasomu committed Mar 30, 2019
1 parent 8880e38 commit 816178a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 40 deletions.
39 changes: 2 additions & 37 deletions eth_account/_utils/structured_data/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from eth_abi import (
encode_abi,
is_encodable,
is_encodable_type,
)
from eth_abi.grammar import (
parse,
Expand Down Expand Up @@ -91,42 +92,6 @@ def hash_struct_type(primary_type, types):
return keccak(text=encode_type(primary_type, types))


def is_valid_abi_type(type_name):
"""
This function is used to make sure that the ``type_name`` is a valid ABI Type.
Please note that this is a temporary function and should be replaced by the corresponding
ABI function, once the following issue has been resolved.
https://github.com/ethereum/eth-abi/issues/125
"""
valid_abi_types = {"address", "bool", "bytes", "int", "string", "uint"}
is_bytesN = type_name.startswith("bytes") and 1 <= int(type_name[5:]) <= 32
is_intN = (
type_name.startswith("int") and
8 <= int(type_name[3:]) <= 256 and
int(type_name[3:]) % 8 == 0
)
is_uintN = (
type_name.startswith("uint") and
8 <= int(type_name[4:]) <= 256 and
int(type_name[4:]) % 8 == 0
)

if type_name in valid_abi_types:
return True
elif is_bytesN:
# bytes1 to bytes32
return True
elif is_intN:
# int8 to int256
return True
elif is_uintN:
# uint8 to uint256
return True

return False


def is_array_type(type):
# Identify if type such as "person[]" or "person[2]" is an array
abi_type = parse(type)
Expand Down Expand Up @@ -269,7 +234,7 @@ def _encode_data(primary_type, types, data):
yield "bytes32", hashed_value
else:
# First checking to see if type is valid as per abi
if not is_valid_abi_type(field["type"]):
if not is_encodable_type(field["type"]):
raise TypeError(
"Received Invalid type `{0}` in the struct `{1}`".format(
field["type"],
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
include_package_data=True,
install_requires=[
"attrdict>=2.0.0,<3",
"eth-abi>=2.0.0b7,<3",
"eth-abi>=2.0.0b8,<3",
"eth-keyfile>=0.5.0,<0.6.0",
"eth-keys>=0.2.0b3,<0.3.0",
"eth-utils>=1.0.2,<2",
Expand Down
6 changes: 4 additions & 2 deletions tests/core/test_structured_data_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import pytest
import re

from eth_abi.exceptions import (
ABITypeError,
)
from eth_utils import (
ValidationError,
keccak,
Expand Down Expand Up @@ -236,9 +239,8 @@ def test_invalid_structured_data_invalid_abi_type():
"tests/fixtures/invalid_message_invalid_abi_type.json"
).read()
invalid_structured_data = json.loads(invalid_structured_data_string)
with pytest.raises(TypeError) as e:
with pytest.raises(ABITypeError):
hash_message(invalid_structured_data)
assert str(e.value) == "Received Invalid type `uint25689` in the struct `Person`"


def test_structured_data_invalid_identifier_filtered_by_abi_encodable_function():
Expand Down

0 comments on commit 816178a

Please sign in to comment.