Add "friendly" peer ID string representation for debugging

This commit is contained in:
Alex Stokes 2019-08-24 20:36:00 +02:00
parent 852609c85d
commit 44e5de636f
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086

View File

@ -6,6 +6,11 @@ import multihash
from libp2p.crypto.keys import PublicKey from libp2p.crypto.keys import PublicKey
# NOTE: ``FRIENDLY_IDS`` renders a ``str`` representation of ``ID`` as a
# short string of a prefix of the base58 representation. This feature is primarily
# intended for debugging, logging, etc.
FRIENDLY_IDS = True
class ID: class ID:
_bytes: bytes _bytes: bytes
@ -32,7 +37,13 @@ class ID:
def __repr__(self) -> str: def __repr__(self) -> str:
return "<libp2p.peer.id.ID 0x" + self._bytes.hex() + ">" return "<libp2p.peer.id.ID 0x" + self._bytes.hex() + ">"
__str__ = pretty = to_string = to_base58 pretty = to_string = to_base58
def __str__(self) -> str:
if FRIENDLY_IDS:
return self.to_string()[2:8]
else:
return self.to_string()
def __eq__(self, other: object) -> bool: def __eq__(self, other: object) -> bool:
if isinstance(other, str): if isinstance(other, str):