From 87d943aa39c2eac947f7d6451f3cffb3033c9099 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Tue, 20 Aug 2019 19:01:36 +0200 Subject: [PATCH 1/2] Internalize the protobuf serialization to the concept of a `Key` Given its use across various components of `libp2p` (not just peer IDs), it makes the abstraction cleaner to pull the serialization into the key class and expose the canonical serialization to bytes. --- libp2p/crypto/keys.py | 31 +++++++++++++++++++------------ libp2p/peer/id.py | 9 +-------- tests/peer/test_peerid.py | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/libp2p/crypto/keys.py b/libp2p/crypto/keys.py index b0dec3d9..f0485ad5 100644 --- a/libp2p/crypto/keys.py +++ b/libp2p/crypto/keys.py @@ -32,12 +32,29 @@ class Key(ABC): """ ... + def _serialize_to_protobuf(self) -> protobuf.PublicKey: + """ + Return the protobuf representation of this ``Key``. + """ + key_type = self.get_type().value + data = self.to_bytes() + protobuf_key = self.protobuf_constructor(key_type=key_type, data=data) + return protobuf_key + + def serialize(self) -> bytes: + """ + Return the canonical serialization of this ``Key``. + """ + return self._serialize_to_protobuf().SerializeToString() + class PublicKey(Key): """ A ``PublicKey`` represents a cryptographic public key. """ + protobuf_constructor = protobuf.PublicKey + @abstractmethod def verify(self, data: bytes, signature: bytes) -> bool: """ @@ -45,18 +62,14 @@ class PublicKey(Key): """ ... - def serialize_to_protobuf(self) -> protobuf.PublicKey: - key_type = self.get_type().value - data = self.to_bytes() - protobuf_key = protobuf.PublicKey(key_type=key_type, data=data) - return protobuf_key - class PrivateKey(Key): """ A ``PrivateKey`` represents a cryptographic private key. """ + protobuf_constructor = protobuf.PrivateKey + @abstractmethod def sign(self, data: bytes) -> bytes: ... @@ -65,12 +78,6 @@ class PrivateKey(Key): def get_public_key(self) -> PublicKey: ... - def serialize_to_protobuf(self) -> protobuf.PrivateKey: - key_type = self.get_type().value - data = self.to_bytes() - protobuf_key = protobuf.PrivateKey(key_type=key_type, data=data) - return protobuf_key - @dataclass(frozen=True) class KeyPair: diff --git a/libp2p/peer/id.py b/libp2p/peer/id.py index 26029b62..303f519f 100644 --- a/libp2p/peer/id.py +++ b/libp2p/peer/id.py @@ -7,13 +7,6 @@ import multihash from libp2p.crypto.keys import PublicKey -def _serialize_public_key(key: PublicKey) -> bytes: - """ - Serializes ``key`` in the way expected to form valid peer ids. - """ - return key.serialize_to_protobuf().SerializeToString() - - class ID: _bytes: bytes _xor_id: int = None @@ -62,7 +55,7 @@ class ID: @classmethod def from_pubkey(cls, key: PublicKey) -> "ID": - serialized_key = _serialize_public_key(key) + serialized_key = key.serialize() algo = multihash.Func.sha2_256 mh_digest = multihash.digest(serialized_key, algo) return cls(mh_digest.encode()) diff --git a/tests/peer/test_peerid.py b/tests/peer/test_peerid.py index 30b74c17..ea244ce8 100644 --- a/tests/peer/test_peerid.py +++ b/tests/peer/test_peerid.py @@ -92,7 +92,7 @@ def test_id_from_public_key(): key_pair = create_new_key_pair() public_key = key_pair.public_key - key_bin = public_key.serialize_to_protobuf().SerializeToString() + key_bin = public_key.serialize() algo = multihash.Func.sha2_256 mh_digest = multihash.digest(key_bin, algo) expected = ID(mh_digest.encode()) From e1d3f1601f25dec828e1830d2fbad375129bf0fe Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Tue, 20 Aug 2019 19:28:32 +0200 Subject: [PATCH 2/2] Satisfy mypy --- libp2p/crypto/keys.py | 47 +++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/libp2p/crypto/keys.py b/libp2p/crypto/keys.py index f0485ad5..31caca00 100644 --- a/libp2p/crypto/keys.py +++ b/libp2p/crypto/keys.py @@ -32,29 +32,12 @@ class Key(ABC): """ ... - def _serialize_to_protobuf(self) -> protobuf.PublicKey: - """ - Return the protobuf representation of this ``Key``. - """ - key_type = self.get_type().value - data = self.to_bytes() - protobuf_key = self.protobuf_constructor(key_type=key_type, data=data) - return protobuf_key - - def serialize(self) -> bytes: - """ - Return the canonical serialization of this ``Key``. - """ - return self._serialize_to_protobuf().SerializeToString() - class PublicKey(Key): """ A ``PublicKey`` represents a cryptographic public key. """ - protobuf_constructor = protobuf.PublicKey - @abstractmethod def verify(self, data: bytes, signature: bytes) -> bool: """ @@ -62,6 +45,21 @@ class PublicKey(Key): """ ... + def _serialize_to_protobuf(self) -> protobuf.PublicKey: + """ + Return the protobuf representation of this ``Key``. + """ + key_type = self.get_type().value + data = self.to_bytes() + protobuf_key = protobuf.PublicKey(key_type=key_type, data=data) + return protobuf_key + + def serialize(self) -> bytes: + """ + Return the canonical serialization of this ``Key``. + """ + return self._serialize_to_protobuf().SerializeToString() + class PrivateKey(Key): """ @@ -78,6 +76,21 @@ class PrivateKey(Key): def get_public_key(self) -> PublicKey: ... + def _serialize_to_protobuf(self) -> protobuf.PrivateKey: + """ + Return the protobuf representation of this ``Key``. + """ + key_type = self.get_type().value + data = self.to_bytes() + protobuf_key = protobuf.PrivateKey(key_type=key_type, data=data) + return protobuf_key + + def serialize(self) -> bytes: + """ + Return the canonical serialization of this ``Key``. + """ + return self._serialize_to_protobuf().SerializeToString() + @dataclass(frozen=True) class KeyPair: