File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed
Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff 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
9498def sha256_digest (data : str | bytes ) -> bytes :
You can’t perform that action at this time.
0 commit comments