Add explicit exception if we are missing a deserializer
This commit is contained in:
parent
6e53849604
commit
bbd8279811
14
libp2p/crypto/exceptions.py
Normal file
14
libp2p/crypto/exceptions.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from libp2p.exceptions import BaseLibp2pError
|
||||
|
||||
|
||||
class CryptographyError(BaseLibp2pError):
|
||||
pass
|
||||
|
||||
|
||||
class MissingDeserializerError(CryptographyError):
|
||||
"""
|
||||
Raise if the requested deserialization routine is missing for
|
||||
some type of cryptographic key.
|
||||
"""
|
||||
|
||||
pass
|
|
@ -1,4 +1,5 @@
|
|||
from libp2p.crypto.ed25519 import Ed25519PublicKey
|
||||
from libp2p.crypto.exceptions import MissingDeserializerError
|
||||
from libp2p.crypto.keys import KeyType, PrivateKey, PublicKey
|
||||
from libp2p.crypto.rsa import RSAPublicKey
|
||||
from libp2p.crypto.secp256k1 import Secp256k1PrivateKey, Secp256k1PublicKey
|
||||
|
@ -16,11 +17,17 @@ key_type_to_private_key_deserializer = {
|
|||
|
||||
def deserialize_public_key(data: bytes) -> PublicKey:
|
||||
f = PublicKey.deserialize_from_protobuf(data)
|
||||
deserializer = key_type_to_public_key_deserializer[f.key_type]
|
||||
try:
|
||||
deserializer = key_type_to_public_key_deserializer[f.key_type]
|
||||
except KeyError:
|
||||
raise MissingDeserializerError({"key_type": f.key_type, "key": "public_key"})
|
||||
return deserializer(f.data)
|
||||
|
||||
|
||||
def deserialize_private_key(data: bytes) -> PrivateKey:
|
||||
f = PrivateKey.deserialize_from_protobuf(data)
|
||||
deserializer = key_type_to_private_key_deserializer[f.key_type]
|
||||
try:
|
||||
deserializer = key_type_to_private_key_deserializer[f.key_type]
|
||||
except KeyError:
|
||||
raise MissingDeserializerError({"key_type": f.key_type, "key": "private_key"})
|
||||
return deserializer(f.data)
|
||||
|
|
Loading…
Reference in New Issue
Block a user