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.
86 lines
1.8 KiB
Python
86 lines
1.8 KiB
Python
from abc import ABC, abstractmethod
|
|
from dataclasses import dataclass
|
|
from enum import Enum, unique
|
|
|
|
from .pb import crypto_pb2 as protobuf
|
|
|
|
|
|
@unique
|
|
class KeyType(Enum):
|
|
RSA = 0
|
|
Ed25519 = 1
|
|
Secp256k1 = 2
|
|
ECDSA = 3
|
|
|
|
|
|
class Key(ABC):
|
|
"""
|
|
A ``Key`` represents a cryptographic key.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def to_bytes(self) -> bytes:
|
|
"""
|
|
Returns the byte representation of this key.
|
|
"""
|
|
...
|
|
|
|
@abstractmethod
|
|
def get_type(self) -> KeyType:
|
|
"""
|
|
Returns the ``KeyType`` for ``self``.
|
|
"""
|
|
...
|
|
|
|
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:
|
|
"""
|
|
Verify that ``signature`` is the cryptographic signature of the hash of ``data``.
|
|
"""
|
|
...
|
|
|
|
|
|
class PrivateKey(Key):
|
|
"""
|
|
A ``PrivateKey`` represents a cryptographic private key.
|
|
"""
|
|
|
|
protobuf_constructor = protobuf.PrivateKey
|
|
|
|
@abstractmethod
|
|
def sign(self, data: bytes) -> bytes:
|
|
...
|
|
|
|
@abstractmethod
|
|
def get_public_key(self) -> PublicKey:
|
|
...
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class KeyPair:
|
|
private_key: PrivateKey
|
|
public_key: PublicKey
|