Skip to content

Commit

Permalink
Remove asn1tools dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jannikluhn committed May 31, 2019
1 parent b46737e commit 69edf61
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
29 changes: 5 additions & 24 deletions eth_keys/backends/coincurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,32 @@
from eth_keys.validation import (
validate_uncompressed_public_key_bytes,
)
from eth_keys.utils import (
der,
)

from .base import BaseECCBackend


def is_coincurve_available() -> bool:
try:
import coincurve # noqa: F401
import asn1tools # noqa: F401
except ImportError:
return False
else:
return True


ASN1_ECDSA_SPEC_STRING = """\
ECDSASpec DEFINITIONS ::= BEGIN
ECDSASignature ::= SEQUENCE {
r INTEGER,
s INTEGER
}
END
"""


class CoinCurveECCBackend(BaseECCBackend):
def __init__(self) -> None:
try:
import coincurve
except ImportError:
raise ImportError("The CoinCurveECCBackend requires the coincurve \
library which is not available for import.")
try:
import asn1tools
except ImportError:
raise ImportError("The CoinCurveECCBackend requires the asn1tools \
library which is not available for import.")

self.keys = coincurve.keys
self.ecdsa = coincurve.ecdsa
self.asn1_ecdsa_spec = asn1tools.compile_string(ASN1_ECDSA_SPEC_STRING, "der")

super(CoinCurveECCBackend, self).__init__()

Expand All @@ -83,8 +68,7 @@ def ecdsa_sign_non_recoverable(self,
msg_hash,
hasher=None,
)
rs_dict = self.asn1_ecdsa_spec.decode("ECDSASignature", der_encoded_signature)
rs = (rs_dict["r"], rs_dict["s"])
rs = der.two_int_sequence_decoder(der_encoded_signature)

signature = NonRecoverableSignature(rs=rs, backend=self)
return signature
Expand All @@ -93,10 +77,7 @@ def ecdsa_verify(self,
msg_hash: bytes,
signature: BaseSignature,
public_key: PublicKey) -> bool:
der_encoded_signature = bytes(self.asn1_ecdsa_spec.encode("ECDSASignature", {
"r": signature.r,
"s": signature.s,
}))
der_encoded_signature = der.two_int_sequence_encoder((signature.r, signature.s))
coincurve_public_key = self.keys.PublicKey(b"\x04" + public_key.to_bytes())
return coincurve_public_key.verify(
der_encoded_signature,
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ commands=
backends: py.test {posargs:tests/backends}
deps = .[test]
backends: coincurve>=7.0.0,<8.0.0
asn1tools>=0.146.1,<1.0.0
setenv =
backends: REQUIRE_COINCURVE=True
basepython =
Expand Down

0 comments on commit 69edf61

Please sign in to comment.