@@ -10,10 +10,11 @@ def sha3_256(x): return _sha3.keccak_256(x).digest()
10
10
import sys
11
11
import rlp
12
12
from rlp .sedes import big_endian_int , BigEndianInt , Binary
13
- from rlp .utils import decode_hex , encode_hex , ascii_chr , str_to_bytes
13
+ from eth_utils import encode_hex as encode_hex_0x
14
+ from eth_utils import decode_hex , int_to_big_endian , big_endian_to_int
15
+ from rlp .utils import ALL_BYTES
14
16
import random
15
17
16
-
17
18
try :
18
19
import coincurve
19
20
except ImportError :
@@ -30,13 +31,6 @@ def __call__(self, *args):
30
31
self .memo [args ] = self .fn (* args )
31
32
return self .memo [args ]
32
33
33
- def big_endian_to_int (x ): return big_endian_int .deserialize (
34
- str_to_bytes (x ).lstrip (b'\x00 ' ))
35
-
36
-
37
- def int_to_big_endian (x ): return big_endian_int .serialize (x )
38
-
39
-
40
34
TT256 = 2 ** 256
41
35
TT256M1 = 2 ** 256 - 1
42
36
TT255 = 2 ** 255
@@ -68,6 +62,13 @@ def encode_int32(v):
68
62
def bytes_to_int (value ):
69
63
return big_endian_to_int (bytes ('' .join (chr (c ) for c in value )))
70
64
65
+ def str_to_bytes (value ):
66
+ if isinstance (value , (bytes , bytearray )):
67
+ return bytes (value )
68
+ elif isinstance (value , unicode ):
69
+ return codecs .encode (value , 'utf8' )
70
+ else :
71
+ raise TypeError ("Value must be text, bytes, or bytearray" )
71
72
else :
72
73
def is_numeric (x ): return isinstance (x , int )
73
74
@@ -99,6 +100,18 @@ def encode_int32(v):
99
100
def bytes_to_int (value ):
100
101
return int .from_bytes (value , byteorder = 'big' )
101
102
103
+ def str_to_bytes (value ):
104
+ if isinstance (value , bytearray ):
105
+ value = bytes (value )
106
+ if isinstance (value , bytes ):
107
+ return value
108
+ return bytes (value , 'utf-8' )
109
+
110
+ def ascii_chr (n ):
111
+ return ALL_BYTES [n ]
112
+
113
+ def encode_hex (n ):
114
+ return encode_hex_0x (n )[2 :]
102
115
103
116
def ecrecover_to_pub (rawhash , v , r , s ):
104
117
if coincurve and hasattr (coincurve , "PublicKey" ):
0 commit comments