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.
This commit is contained in:
Alex Stokes
2019-08-20 19:01:36 +02:00
parent 5d611801c7
commit 87d943aa39
3 changed files with 21 additions and 21 deletions

View File

@ -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())