|
18 | 18 | import ecdsa |
19 | 19 | from eth_utils import apply_to_return_value, to_hex |
20 | 20 | from hexbytes import HexBytes |
| 21 | +from tronapi.base.abi import map_abi_data |
21 | 22 |
|
22 | 23 | from tronapi.base.account import Account, PrivateKey |
23 | 24 | from tronapi.base.datastructures import AttributeDict |
| 25 | +from tronapi.base.normalizers import abi_resolver |
24 | 26 | from tronapi.base.encoding import ( |
25 | 27 | to_bytes, |
26 | 28 | to_int, |
27 | 29 | to_text, |
28 | | - to_json |
| 30 | + to_json, |
| 31 | + hex_encode_abi_type |
29 | 32 | ) |
30 | 33 |
|
31 | 34 | from tronapi.exceptions import InvalidTronError, TronError |
|
36 | 39 | from tronapi.base.validation import is_address |
37 | 40 | from tronapi.utils.crypto import keccak |
38 | 41 | from tronapi.utils.currency import to_sun, from_sun |
| 42 | +from tronapi.utils.hexadecimal import remove_0x_prefix, add_0x_prefix |
39 | 43 | from tronapi.utils.types import is_integer |
40 | 44 |
|
41 | 45 | DEFAULT_MODULES = { |
@@ -268,6 +272,37 @@ def is_valid_provider(provider) -> bool: |
268 | 272 | """ |
269 | 273 | return isinstance(provider, HttpProvider) |
270 | 274 |
|
| 275 | + def solidity_sha3(self, abi_types, values): |
| 276 | + """ |
| 277 | + Executes keccak256 exactly as Solidity does. |
| 278 | + Takes list of abi_types as inputs -- `[uint24, int8[], bool]` |
| 279 | + and list of corresponding values -- `[20, [-1, 5, 0], True]` |
| 280 | +
|
| 281 | + Args: |
| 282 | + abi_types (any): types abi |
| 283 | + values (any): values |
| 284 | +
|
| 285 | + Examples: |
| 286 | + >>> tron = Tron() |
| 287 | + >>> sol = tron.solidity_sha3(['uint8[]'], [[1, 2, 3, 4, 5]]) |
| 288 | + >>> assert sol.hex() == '0x5917e5a395fb9b454434de59651d36822a9e29c5ec57474df3e67937b969460c' |
| 289 | +
|
| 290 | + """ |
| 291 | + if len(abi_types) != len(values): |
| 292 | + raise ValueError( |
| 293 | + "Length mismatch between provided abi types and values. Got " |
| 294 | + "{0} types and {1} values.".format(len(abi_types), len(values)) |
| 295 | + ) |
| 296 | + |
| 297 | + normalized_values = map_abi_data([abi_resolver()], abi_types, values) |
| 298 | + |
| 299 | + hex_string = add_0x_prefix(''.join( |
| 300 | + remove_0x_prefix(hex_encode_abi_type(abi_type, value)) |
| 301 | + for abi_type, value |
| 302 | + in zip(abi_types, normalized_values) |
| 303 | + )) |
| 304 | + return self.sha3(hexstr=hex_string) |
| 305 | + |
271 | 306 | @staticmethod |
272 | 307 | @apply_to_return_value(HexBytes) |
273 | 308 | def sha3(primitive=None, text=None, hexstr=None): |
|
0 commit comments