mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Dispatch serialization of keys based on key type
- Add some tests to check high-level roundtrip
This commit is contained in:
@ -33,6 +33,9 @@ class Key(ABC):
|
||||
"""
|
||||
...
|
||||
|
||||
def __eq__(self, other: "Key") -> bool:
|
||||
return self.impl == other.impl
|
||||
|
||||
|
||||
class PublicKey(Key):
|
||||
"""
|
||||
@ -61,14 +64,18 @@ class PublicKey(Key):
|
||||
"""
|
||||
return self._serialize_to_protobuf().SerializeToString()
|
||||
|
||||
@classmethod
|
||||
def deserialize_from_protobuf(cls, protobuf_data: bytes) -> protobuf.PublicKey:
|
||||
protobuf_key = protobuf.PublicKey()
|
||||
protobuf_key.ParseFromString(protobuf_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:
|
||||
...
|
||||
@ -92,6 +99,21 @@ class PrivateKey(Key):
|
||||
"""
|
||||
return self._serialize_to_protobuf().SerializeToString()
|
||||
|
||||
def _protobuf_from_serialization(self, data: bytes) -> 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
|
||||
|
||||
@classmethod
|
||||
def deserialize_from_protobuf(cls, protobuf_data: bytes) -> protobuf.PrivateKey:
|
||||
protobuf_key = protobuf.PrivateKey()
|
||||
protobuf_key.ParseFromString(protobuf_data)
|
||||
return protobuf_key
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class KeyPair:
|
||||
|
||||
Reference in New Issue
Block a user