Add option to inline "short" public keys for peer IDs

Added to remain interoperable w/ the Go implementation
This commit is contained in:
Alex Stokes 2019-09-03 13:14:04 -07:00
parent db858e467c
commit 345e696a7d
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086

View File

@ -11,6 +11,32 @@ from libp2p.crypto.keys import PublicKey
# intended for debugging, logging, etc. # intended for debugging, logging, etc.
FRIENDLY_IDS = True FRIENDLY_IDS = True
# NOTE: On inlining...
# See: https://github.com/libp2p/specs/issues/138
# NOTE: enabling to be interoperable w/ the Go implementation
ENABLE_INLINING = True
MAX_INLINE_KEY_LENGTH = 42
IDENTITY_MULTIHASH_CODE = 0x00
if ENABLE_INLINING:
class IdentityHash:
_digest: bytes
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=lambda: IdentityHash()
)
class ID: class ID:
_bytes: bytes _bytes: bytes
@ -68,6 +94,8 @@ class ID:
def from_pubkey(cls, key: PublicKey) -> "ID": def from_pubkey(cls, key: PublicKey) -> "ID":
serialized_key = key.serialize() serialized_key = key.serialize()
algo = multihash.Func.sha2_256 algo = multihash.Func.sha2_256
if ENABLE_INLINING and len(serialized_key) <= MAX_INLINE_KEY_LENGTH:
algo = IDENTITY_MULTIHASH_CODE
mh_digest = multihash.digest(serialized_key, algo) mh_digest = multihash.digest(serialized_key, algo)
return cls(mh_digest.encode()) return cls(mh_digest.encode())