1414
1515from hivemind .proto import crypto_pb2 , p2pd_pb2
1616
17- # NOTE: On inlining...
18- # See: https://github.com/libp2p/specs/issues/138
19- # NOTE: enabling to be interoperable w/ the Go implementation
20- ENABLE_INLINING = True
21- MAX_INLINE_KEY_LENGTH = 42
22-
23- IDENTITY_MULTIHASH_CODE = 0x00
24-
25- if ENABLE_INLINING :
26-
27- class IdentityHash :
28- def __init__ (self ) -> None :
29- self ._digest = bytearray ()
30-
31- def update (self , input : bytes ) -> None :
32- self ._digest += input
33-
34- def digest (self ) -> bytes :
35- return self ._digest
36-
37- multihash .FuncReg .register (IDENTITY_MULTIHASH_CODE , "identity" , hash_new = IdentityHash )
38-
3917
4018class PeerID :
4119 def __init__ (self , peer_id_bytes : bytes ) -> None :
@@ -96,7 +74,6 @@ def from_identity(cls, data: bytes) -> "PeerID":
9674
9775 [1] https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#peer-ids
9876 """
99-
10077 key_data = crypto_pb2 .PrivateKey .FromString (data ).data
10178 private_key = serialization .load_der_private_key (key_data , password = None )
10279
@@ -109,10 +86,10 @@ def from_identity(cls, data: bytes) -> "PeerID":
10986 data = encoded_public_key ,
11087 ).SerializeToString ()
11188
112- algo = multihash .Func . sha2_256
113- if ENABLE_INLINING and len (encoded_public_key ) <= MAX_INLINE_KEY_LENGTH :
114- algo = IDENTITY_MULTIHASH_CODE
115- encoded_digest = multihash . digest ( encoded_public_key , algo ). encode ( )
89+ encoded_digest = multihash .encode (
90+ hashlib . sha256 (encoded_public_key ). digest (),
91+ multihash . coerce_code ( "sha2-256" ),
92+ )
11693 return cls (encoded_digest )
11794
11895
0 commit comments