|
17 | 17 | valmap, |
18 | 18 | ) |
19 | 19 |
|
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 |
21 | 23 | 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 |
24 | 26 |
|
25 | 27 |
|
26 | 28 | def _prepare_selector_collision_msg(duplicates): |
@@ -105,3 +107,58 @@ def validate_abi_type(abi_type): |
105 | 107 | """ |
106 | 108 | if not is_recognized_type(abi_type): |
107 | 109 | 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