Skip to content

Commit

Permalink
Update datastructures.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dvmazur authored Nov 30, 2023
1 parent 065a3f9 commit f8ca047
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions hivemind/p2p/p2p_daemon_bindings/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@
# NOTE: On inlining...
# See: https://github.com/libp2p/specs/issues/138
# NOTE: enabling to be interoperable w/ the Go implementation
ENABLE_INLINING = False
# ENABLE_INLINING = False
MAX_INLINE_KEY_LENGTH = 42

IDENTITY_MULTIHASH_CODE = 0x00
# IDENTITY_MULTIHASH_CODE = 0x00

# if ENABLE_INLINING:

# class IdentityHash:
# def __init__(self) -> None:
# self._digest = bytearray()

# def update(self, input: bytes) -> None:
# self._digest += input

# def digest(self) -> bytes:
# return self._digest

# multihash.FuncReg.register(IDENTITY_MULTIHASH_CODE, "identity", hash_new=IdentityHash)


class PeerID:
Expand Down Expand Up @@ -82,7 +96,6 @@ def from_identity(cls, data: bytes) -> "PeerID":
[1] https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#peer-ids
"""

key_data = crypto_pb2.PrivateKey.FromString(data).data
private_key = serialization.load_der_private_key(key_data, password=None)

Expand All @@ -95,8 +108,11 @@ def from_identity(cls, data: bytes) -> "PeerID":
data=encoded_public_key,
).SerializeToString()

algo = multihash.Func.sha2_256
encoded_digest = multihash.digest(encoded_public_key, algo).encode()
#algo = multihash.Func.sha2_256
#if ENABLE_INLINING and len(encoded_public_key) <= MAX_INLINE_KEY_LENGTH:
# algo = IDENTITY_MULTIHASH_CODE
#encoded_digest = multihash.digest(encoded_public_key, algo).encode()
encoded_digest = multihash.encode(hashlib.sha256(encoded_public_key).digest(), multihash.coerce_code("sha2-256"))
return cls(encoded_digest)


Expand Down

0 comments on commit f8ca047

Please sign in to comment.