13
13
14
14
15
15
try :
16
- import secp256k1
16
+ import coincurve
17
17
except ImportError :
18
18
import warnings
19
- warnings .warn ('could not import secp256k1 ' , ImportWarning )
20
- secp256k1 = None
19
+ warnings .warn ('could not import coincurve ' , ImportWarning )
20
+ coincurve = None
21
21
22
22
big_endian_to_int = lambda x : big_endian_int .deserialize (str_to_bytes (x ).lstrip (b'\x00 ' ))
23
23
int_to_big_endian = lambda x : big_endian_int .serialize (x )
@@ -85,22 +85,15 @@ def bytes_to_int(value):
85
85
86
86
87
87
def ecrecover_to_pub (rawhash , v , r , s ):
88
- if secp256k1 and hasattr (secp256k1 , "PublicKey" ):
89
- # Legendre symbol check; the secp256k1 library does not seem to do this
90
- pk = secp256k1 .PublicKey (flags = secp256k1 .ALL_FLAGS )
91
- xc = r * r * r + 7
92
- assert pow (xc , (SECP256K1P - 1 ) // 2 , SECP256K1P ) == 1
88
+ if coincurve and hasattr (coincurve , "PublicKey" ):
93
89
try :
94
- pk .public_key = pk .ecdsa_recover (
90
+ pk = coincurve .PublicKey .from_signature_and_message (
91
+ zpad (utils .bytearray_to_bytestr (int_to_32bytearray (r )), 32 ) + zpad (utils .bytearray_to_bytestr (int_to_32bytearray (s )), 32 ) +
92
+ utils .ascii_chr (v - 27 ),
95
93
rawhash ,
96
- pk .ecdsa_recoverable_deserialize (
97
- zpad (bytearray_to_bytestr (int_to_32bytearray (r )), 32 ) +
98
- zpad (bytearray_to_bytestr (int_to_32bytearray (s )), 32 ),
99
- v - 27
100
- ),
101
- raw = True
94
+ hasher = None ,
102
95
)
103
- pub = pk .serialize (compressed = False )[1 :]
96
+ pub = pk .format (compressed = False )[1 :]
104
97
except :
105
98
pub = b"\x00 " * 64
106
99
else :
@@ -111,12 +104,9 @@ def ecrecover_to_pub(rawhash, v, r, s):
111
104
112
105
113
106
def ecsign (rawhash , key ):
114
- if secp256k1 and hasattr (secp256k1 , 'PrivateKey' ):
115
- pk = secp256k1 .PrivateKey (key , raw = True )
116
- signature = pk .ecdsa_recoverable_serialize (
117
- pk .ecdsa_sign_recoverable (rawhash , raw = True )
118
- )
119
- signature = signature [0 ] + bytearray_to_bytestr ([signature [1 ]])
107
+ if coincurve and hasattr (coincurve , 'PrivateKey' ):
108
+ pk = coincurve .PrivateKey (priv )
109
+ signature = pk .sign_recoverable (msghash , hasher = None )
120
110
v = safe_ord (signature [64 ]) + 27
121
111
r = big_endian_to_int (signature [0 :32 ])
122
112
s = big_endian_to_int (signature [32 :64 ])
0 commit comments