From 44e5de636ff25cadbfef782d98dc5501df7c5ea3 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Sat, 24 Aug 2019 20:36:00 +0200 Subject: [PATCH] Add "friendly" peer ID string representation for debugging --- libp2p/peer/id.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libp2p/peer/id.py b/libp2p/peer/id.py index 303f519..c1b52f0 100644 --- a/libp2p/peer/id.py +++ b/libp2p/peer/id.py @@ -6,6 +6,11 @@ import multihash 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: _bytes: bytes @@ -32,7 +37,13 @@ class ID: def __repr__(self) -> str: return "" - __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: if isinstance(other, str):