Skip to content

Commit 9f3d2d1

Browse files
committed
added validate_abi_value()
1 parent d0c0a2d commit 9f3d2d1

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

tronapi/base/validation.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
valmap,
1818
)
1919

20-
from tronapi.base.abi import filter_by_type, abi_to_signature, is_recognized_type
20+
from tronapi.base.abi import filter_by_type, abi_to_signature, is_recognized_type, is_string_type, is_bytes_type, \
21+
is_address_type, is_int_type, is_uint_type, is_bool_type, sub_type_of_array_type, is_array_type, \
22+
length_of_array_type
2123
from tronapi.utils.help import hex_to_base58
22-
from tronapi.utils.hexadecimal import is_hex, encode_hex
23-
from tronapi.utils.types import is_text, is_list_like, is_dict
24+
from tronapi.utils.hexadecimal import is_hex, encode_hex, is_0x_prefixed
25+
from tronapi.utils.types import is_text, is_list_like, is_dict, is_string, is_bytes, is_boolean, is_integer
2426

2527

2628
def _prepare_selector_collision_msg(duplicates):
@@ -105,3 +107,58 @@ def validate_abi_type(abi_type):
105107
"""
106108
if not is_recognized_type(abi_type):
107109
raise ValueError("Unrecognized abi_type: {abi_type}".format(abi_type=abi_type))
110+
111+
112+
def validate_abi_value(abi_type, value):
113+
"""
114+
Helper function for validating a value against the expected abi_type
115+
Note: abi_type 'bytes' must either be python3 'bytes' object or ''
116+
"""
117+
if is_array_type(abi_type) and is_list_like(value):
118+
# validate length
119+
specified_length = length_of_array_type(abi_type)
120+
if specified_length is not None:
121+
if specified_length < 1:
122+
raise TypeError(
123+
"Invalid abi-type: {abi_type}. Length of fixed sized arrays"
124+
"must be greater than 0."
125+
.format(abi_type=abi_type)
126+
)
127+
if specified_length != len(value):
128+
raise TypeError(
129+
"The following array length does not the length specified"
130+
"by the abi-type, {abi_type}: {value}"
131+
.format(abi_type=abi_type, value=value)
132+
)
133+
134+
# validate sub_types
135+
sub_type = sub_type_of_array_type(abi_type)
136+
for v in value:
137+
validate_abi_value(sub_type, v)
138+
return
139+
elif is_bool_type(abi_type) and is_boolean(value):
140+
return
141+
elif is_uint_type(abi_type) and is_integer(value) and value >= 0:
142+
return
143+
elif is_int_type(abi_type) and is_integer(value):
144+
return
145+
elif is_address_type(abi_type):
146+
is_address(value)
147+
return
148+
elif is_bytes_type(abi_type):
149+
if is_bytes(value):
150+
return
151+
elif is_string(value):
152+
if is_0x_prefixed(value):
153+
return
154+
else:
155+
raise TypeError(
156+
"ABI values of abi-type 'bytes' must be either"
157+
"a python3 'bytes' object or an '0x' prefixed string."
158+
)
159+
elif is_string_type(abi_type) and is_string(value):
160+
return
161+
162+
raise TypeError(
163+
"The following abi value is not a '{abi_type}': {value}".format(abi_type=abi_type, value=value)
164+
)

0 commit comments

Comments
 (0)