Skip to content

Commit 0de222c

Browse files
Revert 97366de commit
1 parent a230062 commit 0de222c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

libp2p/peer/id.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,15 @@ def from_base58(cls, b58_encoded_peer_id_str: str) -> "ID":
8484
@classmethod
8585
def from_pubkey(cls, key: PublicKey) -> "ID":
8686
serialized_key = key.serialize()
87-
algo = multihash.Func.sha2_256
87+
# Use identity hash (no hashing) for small keys, otherwise use SHA2-256
8888
if ENABLE_INLINING and len(serialized_key) <= MAX_INLINE_KEY_LENGTH:
89-
algo = IDENTITY_MULTIHASH_CODE
90-
mh_digest = multihash.digest(serialized_key, algo)
91-
return cls(mh_digest.encode())
89+
# Identity multihash: just encode the key directly with code 0x00
90+
mh_bytes = multihash.encode(serialized_key, IDENTITY_MULTIHASH_CODE)
91+
else:
92+
# SHA2-256: hash first, then encode
93+
digest = hashlib.sha256(serialized_key).digest()
94+
mh_bytes = multihash.encode(digest, "sha2-256")
95+
return cls(mh_bytes)
9296

9397

9498
def sha256_digest(data: str | bytes) -> bytes:

0 commit comments

Comments
 (0)