mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Add abstraction for a cryptographic key
This commit is contained in:
75
libp2p/crypto/keys.py
Normal file
75
libp2p/crypto/keys.py
Normal file
@ -0,0 +1,75 @@
|
||||
from abc import ABC, abstractmethod
|
||||
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:
|
||||
"""
|
||||
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``.
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class PublicKey(ABC, Key):
|
||||
"""
|
||||
A ``PublicKey`` represents a cryptographic public key.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def verify(self, data: bytes, signature: bytes) -> bool:
|
||||
"""
|
||||
Verify that ``signature`` is the cryptographic signature of the hash of ``data``.
|
||||
"""
|
||||
...
|
||||
|
||||
def serialize_to_protobuf(self) -> protobuf.PublicKey:
|
||||
_type = self.get_type()
|
||||
data = self.to_bytes()
|
||||
protobuf_key = protobuf.PublicKey()
|
||||
protobuf_key.key_type = _type.value
|
||||
protobuf_key.data = data
|
||||
return protobuf_key
|
||||
|
||||
|
||||
class PrivateKey(ABC, Key):
|
||||
"""
|
||||
A ``PrivateKey`` represents a cryptographic private key.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def sign(self, data: bytes) -> bytes:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def get_public_key(self) -> PublicKey:
|
||||
...
|
||||
|
||||
def serialize_to_protobuf(self) -> protobuf.PrivateKey:
|
||||
_type = self.get_type()
|
||||
data = self.to_bytes()
|
||||
protobuf_key = protobuf.PrivateKey()
|
||||
protobuf_key.key_type = _type.value
|
||||
protobuf_key.data = data
|
||||
return protobuf_key
|
||||
Reference in New Issue
Block a user