1313
1414
1515try :
16- import secp256k1
16+ import coincurve
1717except ImportError :
18- import warnings
19- warnings .warn ('could not import secp256k1 ' , ImportWarning )
20- secp256k1 = None
18+ import warning
19+ warning .warn ('could not import coincurve ' , ImportWarning )
20+ coincurve = None
2121
2222big_endian_to_int = lambda x : big_endian_int .deserialize (str_to_bytes (x ).lstrip (b'\x00 ' ))
2323int_to_big_endian = lambda x : big_endian_int .serialize (x )
@@ -85,22 +85,15 @@ def bytes_to_int(value):
8585
8686
8787def 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" ):
9389 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 ),
9593 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 ,
10295 )
103- pub = pk .serialize (compressed = False )[1 :]
96+ pub = pk .format (compressed = False )[1 :]
10497 except :
10598 pub = b"\x00 " * 64
10699 else :
@@ -111,12 +104,9 @@ def ecrecover_to_pub(rawhash, v, r, s):
111104
112105
113106def 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 )
120110 v = safe_ord (signature [64 ]) + 27
121111 r = big_endian_to_int (signature [0 :32 ])
122112 s = big_endian_to_int (signature [32 :64 ])
0 commit comments