1+ import re
12from typing import Optional , Union
23
3- from eth_utils import hexstr_if_str , to_hex
4+ from eth_utils import hexstr_if_str , to_hex , big_endian_to_int , int_to_big_endian
5+
6+ from tronapi .utils .hexadecimal import (
7+ remove_0x_prefix ,
8+ decode_hex ,
9+ encode_hex ,
10+ add_0x_prefix
11+ )
12+
13+ from tronapi .base .toolz import (
14+ curry ,
15+ )
416
5- from tronapi .utils .hexadecimal import remove_0x_prefix , decode_hex , encode_hex , add_0x_prefix
617from tronapi .utils .types import is_boolean , is_integer
718from tronapi .utils .validation import assert_one_val
819
920
10- def to_bytes (primitive : Optional [Union [bytes , int ]] = None ,
11- hexstr : Optional [str ] = None ,
12- text : Optional [str ] = None ) -> bytes :
21+ def to_hex_twos_compliment (value , bit_size ):
22+ """
23+ Converts integer value to twos compliment hex representation with given bit_size
24+ """
25+ if value >= 0 :
26+ return to_hex_with_size (value , bit_size )
27+
28+ value = (1 << bit_size ) + value
29+ hex_value = hex (value )
30+ hex_value = hex_value .rstrip ("L" )
31+ return hex_value
32+
33+
34+ def to_hex_with_size (value , bit_size ):
35+ """Converts a value to hex with given bit_size:"""
36+ return pad_hex (to_hex (value ), bit_size )
37+
38+
39+ def pad_hex (value , bit_size ):
40+ """Pads a hex string up to the given bit_size"""
41+ value = remove_0x_prefix (value )
42+ return add_0x_prefix (value .zfill (int (bit_size / 4 )))
43+
44+
45+ def trim_hex (hexstr ):
46+ if hexstr .startswith ('0x0' ):
47+ hexstr = re .sub ('^0x0+' , '0x' , hexstr )
48+ if hexstr == '0x' :
49+ hexstr = '0x0'
50+ return hexstr
51+
52+
53+ def to_int (value = None , hexstr = None , text = None ):
54+ """Converts value to it's integer representation.
55+
56+ Values are converted this way:
57+
58+ * value:
59+ * bytes: big-endian integer
60+ * bool: True => 1, False => 0
61+ * hexstr: interpret hex as integer
62+ * text: interpret as string of digits, like '12' => 12
63+ """
64+ assert_one_val (value , hexstr = hexstr , text = text )
65+
66+ if hexstr is not None :
67+ return int (hexstr , 16 )
68+ elif text is not None :
69+ return int (text )
70+ elif isinstance (value , bytes ):
71+ return big_endian_to_int (value )
72+ elif isinstance (value , str ):
73+ raise TypeError ("Pass in strings with keyword hexstr or text" )
74+ else :
75+ return int (value )
76+
77+
78+ @curry
79+ def text_if_str (to_type , text_or_primitive ):
80+ """Convert to a type, assuming that strings can be only unicode text (not a hexstr)"""
81+ if isinstance (text_or_primitive , str ):
82+ (primitive , text ) = (None , text_or_primitive )
83+ else :
84+ (primitive , text ) = (text_or_primitive , None )
85+ return to_type (primitive , text = text )
86+
87+
88+ def to_text (primitive = None , hexstr = None , text = None ):
89+ assert_one_val (primitive , hexstr = hexstr , text = text )
90+
91+ if hexstr is not None :
92+ return to_bytes (hexstr = hexstr ).decode ('utf-8' )
93+ elif text is not None :
94+ return text
95+ elif isinstance (primitive , str ):
96+ return to_text (hexstr = primitive )
97+ elif isinstance (primitive , bytes ):
98+ return primitive .decode ('utf-8' )
99+ elif is_integer (primitive ):
100+ byte_encoding = int_to_big_endian (primitive )
101+ return to_text (byte_encoding )
102+ raise TypeError ("Expected an int, bytes or hexstr." )
103+
13104
105+ def to_bytes (primitive = None , hexstr = None , text = None ):
14106 assert_one_val (primitive , hexstr = hexstr , text = text )
15107
16108 if is_boolean (primitive ):
@@ -28,14 +120,6 @@ def to_bytes(primitive: Optional[Union[bytes, int]] = None,
28120 raise TypeError ("expected an int in first arg, or keyword of hexstr or text" )
29121
30122
31- def pad_hex (value : str , bit_size : int ) -> str :
32- """
33- Pads a hex string up to the given bit_size
34- """
35- value = remove_0x_prefix (value )
36- return add_0x_prefix (value .zfill (int (bit_size / 4 )))
37-
38-
39123def to_4byte_hex (hex_or_str_or_bytes : Union [int , str , bytes ]) -> str :
40124 size_of_4bytes = 4 * 8
41125 byte_str = hexstr_if_str (to_bytes , hex_or_str_or_bytes )
0 commit comments