Skip to content

Commit ac30968

Browse files
committed
update encoding.py
1 parent 12eb7a1 commit ac30968

File tree

3 files changed

+103
-85
lines changed

3 files changed

+103
-85
lines changed

tronapi/base/encoding.py

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,108 @@
1+
import re
12
from 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
617
from tronapi.utils.types import is_boolean, is_integer
718
from 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-
39123
def 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)

tronapi/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
# --------------------------------------------------------------------
77

88
import ecdsa
9-
from eth_utils import apply_to_return_value
9+
from eth_utils import apply_to_return_value, to_hex
1010
from hexbytes import HexBytes
1111

1212
from tronapi.base.account import Account, PrivateKey
1313
from tronapi.base.datastructures import AttributeDict
1414
from tronapi.base.decorators import deprecated_for
15+
from tronapi.base.encoding import (
16+
to_bytes,
17+
to_int,
18+
to_text
19+
)
1520

1621
from tronapi.exceptions import InvalidTronError, TronError
1722
from tronapi.manager import TronManager
@@ -21,7 +26,6 @@
2126
from tronapi.base.validation import is_address
2227
from tronapi.utils.crypto import keccak as tron_keccak
2328
from tronapi.utils.currency import to_sun, from_sun
24-
from tronapi.utils.encoding import to_bytes, to_int, to_hex, to_text
2529
from tronapi.utils.types import is_integer
2630

2731
DEFAULT_MODULES = {

tronapi/utils/encoding.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)