Skip to content

Commit

Permalink
Get rid of secp256k1 python packet
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsereda committed Jul 12, 2024
1 parent 4ba8e1d commit 482e893
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/pytezos/crypto/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ def __call__(self, *args, **kwargs):
import fastecdsa.encoding.sec1 # type: ignore
import fastecdsa.keys # type: ignore
import pysodium # type: ignore
from coincurve import ecdsa # type: ignore
from fastecdsa.encoding.util import bytes_to_int # type: ignore
except ImportError as e:
coincurve = CryptoExtraFallback()
coincurve = CryptoExtraFallback() # type: ignore
ecdsa = CryptoExtraFallback() # type: ignore
pysodium = CryptoExtraFallback()
fastecdsa = CryptoExtraFallback()
bytes_to_int = CryptoExtraFallback()
Expand Down Expand Up @@ -152,7 +154,7 @@ def from_secret_exponent(
# Secp256k1
elif curve == b'sp':
sk = coincurve.PrivateKey(secret_exponent)
public_point = sk.public_key.format(compressed=True)
public_point = sk.public_key.format()
# P256
elif curve == b'p2':
pk = fastecdsa.keys.get_public_key(bytes_to_int(secret_exponent), curve=fastecdsa.curve.P256)
Expand Down Expand Up @@ -445,8 +447,9 @@ def sign(self, message: Union[str, bytes], generic: bool = False):
# Secp256k1
elif self.curve == b"sp":
pk = coincurve.PrivateKey(self.secret_exponent)
signature = pk.sign(encoded_message, hasher=lambda x: blake2b_32(x).digest())

signature = ecdsa.serialize_compact(
ecdsa.der_to_cdata(pk.sign(encoded_message, hasher=lambda x: blake2b_32(x).digest()))
)
# P256
elif self.curve == b"p2":
r, s = fastecdsa.ecdsa.sign(msg=encoded_message, d=bytes_to_int(self.secret_exponent), hashfunc=blake2b_32)
Expand Down Expand Up @@ -492,7 +495,7 @@ def verify(self, signature: Union[str, bytes], message: Union[str, bytes]) -> bo
elif self.curve == b"sp":
pk = coincurve.PublicKey(self.public_point)
if not pk.verify(
signature=decoded_signature,
signature=ecdsa.cdata_to_der(ecdsa.deserialize_compact(decoded_signature)),
message=encoded_message,
hasher=lambda x: blake2b_32(x).digest(),
):
Expand Down

0 comments on commit 482e893

Please sign in to comment.