Typing and linting fixes

This commit is contained in:
Alex Stokes
2019-08-23 23:43:36 +02:00
parent 0fa3331b8c
commit 1adef05e94
8 changed files with 53 additions and 33 deletions

View File

@ -98,7 +98,7 @@ def initialize_pair(
authenticator.update(tag)
tag = authenticator.digest()
half = len(result) / 2
half = int(len(result) / 2)
first_half = result[:half]
second_half = result[half:]

View File

@ -1,3 +1,5 @@
from typing import cast
from Crypto.PublicKey import ECC
from Crypto.PublicKey.ECC import EccKey
@ -9,7 +11,7 @@ class ECCPublicKey(PublicKey):
self.impl = impl
def to_bytes(self) -> bytes:
return self.impl.export_key("DER")
return cast(bytes, self.impl.export_key(format="DER"))
@classmethod
def from_bytes(cls, data: bytes) -> "ECCPublicKey":
@ -33,7 +35,7 @@ class ECCPrivateKey(PrivateKey):
return cls(private_key_impl)
def to_bytes(self) -> bytes:
return self.impl.export_key("DER")
return cast(bytes, self.impl.export_key(format="DER"))
def get_type(self) -> KeyType:
return KeyType.ECC_P256
@ -42,7 +44,7 @@ class ECCPrivateKey(PrivateKey):
raise NotImplementedError
def get_public_key(self) -> PublicKey:
return ECCPublicKey(self.impl.publickey())
return ECCPublicKey(self.impl.public_key())
def create_new_key_pair(curve: str) -> KeyPair:

View File

@ -1,8 +1,8 @@
from typing import Callable, Tuple
from typing import Callable, Tuple, cast
import Crypto.PublicKey.ECC as ECC
from libp2p.crypto.ecc import create_new_key_pair
from libp2p.crypto.ecc import ECCPrivateKey, create_new_key_pair
from libp2p.crypto.keys import PublicKey
SharedKeyGenerator = Callable[[bytes], bytes]
@ -20,7 +20,8 @@ def create_ephemeral_key_pair(curve_type: str) -> Tuple[PublicKey, SharedKeyGene
def _key_exchange(serialized_remote_public_key: bytes) -> bytes:
remote_public_key = ECC.import_key(serialized_remote_public_key)
curve_point = remote_public_key.pointQ
secret_point = curve_point * key_pair.private_key.impl.d
private_key = cast(ECCPrivateKey, key_pair.private_key)
secret_point = curve_point * private_key.impl.d
byte_size = secret_point.size_in_bytes()
return secret_point.x.to_bytes(byte_size, byteorder="big")

View File

@ -33,8 +33,10 @@ class Key(ABC):
"""
...
def __eq__(self, other: "Key") -> bool:
return self.impl == other.impl
def __eq__(self, other: object) -> bool:
if not isinstance(other, Key):
return NotImplemented
return self.to_bytes() == other.to_bytes()
class PublicKey(Key):
@ -66,9 +68,7 @@ class PublicKey(Key):
@classmethod
def deserialize_from_protobuf(cls, protobuf_data: bytes) -> protobuf.PublicKey:
protobuf_key = protobuf.PublicKey()
protobuf_key.ParseFromString(protobuf_data)
return protobuf_key
return protobuf.PublicKey.FromString(protobuf_data)
class PrivateKey(Key):
@ -110,9 +110,7 @@ class PrivateKey(Key):
@classmethod
def deserialize_from_protobuf(cls, protobuf_data: bytes) -> protobuf.PrivateKey:
protobuf_key = protobuf.PrivateKey()
protobuf_key.ParseFromString(protobuf_data)
return protobuf_key
return protobuf.PrivateKey.FromString(protobuf_data)
@dataclass(frozen=True)