mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-10 07:00:54 +00:00
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:
@ -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())
|
||||
|
||||
Reference in New Issue
Block a user