Skip to content

Commit 4ec66a7

Browse files
Refactor serialization logic in persistent peerstore
- Simplified imports for public and private key serialization by removing unnecessary key type mappings. - Improved comments for clarity regarding the limitations of reconstructing PeerRecordState. - Enhanced code readability through consistent formatting and whitespace adjustments.
1 parent bc161f3 commit 4ec66a7

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

libp2p/peer/persistent/serialization.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,37 +151,24 @@ def serialize_keypair(keypair: KeyPair) -> bytes:
151151

152152
# Serialize public key
153153
if keypair.public_key:
154-
from libp2p.peer.pb.crypto_pb2 import (
155-
KeyType,
156-
PublicKey as PBPublicKey,
157-
)
154+
from libp2p.peer.pb.crypto_pb2 import PublicKey as PBPublicKey
158155

159156
pb_public_key = PBPublicKey()
160157

161158
# Map key types
162-
key_type_map = {
163-
"rsa": KeyType.RSA,
164-
"ed25519": KeyType.Ed25519,
165-
"secp256k1": KeyType.Secp256k1,
166-
"ecdsa": KeyType.ECDSA,
167-
}
168-
169159
key_type = keypair.public_key.get_type()
170-
pb_public_key.Type = int(key_type.value)
160+
pb_public_key.Type = key_type.value
171161
pb_public_key.Data = keypair.public_key.serialize()
172162
pb_keys.public_key.CopyFrom(pb_public_key)
173163

174164
# Serialize private key
175165
if keypair.private_key:
176-
from libp2p.peer.pb.crypto_pb2 import (
177-
KeyType,
178-
PrivateKey as PBPrivateKey,
179-
)
166+
from libp2p.peer.pb.crypto_pb2 import PrivateKey as PBPrivateKey
180167

181168
pb_private_key = PBPrivateKey()
182169

183170
key_type = keypair.private_key.get_type()
184-
pb_private_key.Type = int(key_type.value)
171+
pb_private_key.Type = key_type.value
185172
pb_private_key.Data = keypair.private_key.serialize()
186173
pb_keys.private_key.CopyFrom(pb_private_key)
187174

@@ -365,9 +352,10 @@ def deserialize_record_state(data: bytes) -> "PeerRecordState":
365352
pb_state = PBPeerRecordState()
366353
pb_state.ParseFromString(data)
367354

368-
# Since we can't reconstruct the full PeerRecordState without envelope and seq,
369-
# we'll need to return a placeholder. This is a limitation of the current design.
370-
# In practice, the record state should be stored with its envelope and seq.
355+
# Since we can't reconstruct the full PeerRecordState without the envelope
356+
# and sequence number (seq), we'll need to return a placeholder. This is a
357+
# limitation of the current design. In practice, the record state should
358+
# be stored together with its envelope and seq.
371359
from libp2p.crypto.ed25519 import Ed25519PublicKey
372360
from libp2p.peer.envelope import Envelope
373361
from libp2p.peer.peerstore import PeerRecordState

0 commit comments

Comments
 (0)