2019-07-28 22:30:51 +08:00
|
|
|
import hashlib
|
2019-08-01 06:00:12 +08:00
|
|
|
from typing import Union
|
2019-07-27 16:27:01 +08:00
|
|
|
|
2018-11-19 00:22:56 +08:00
|
|
|
import base58
|
2018-11-29 23:06:40 +08:00
|
|
|
import multihash
|
|
|
|
|
2019-08-14 09:19:01 +08:00
|
|
|
from libp2p.crypto.keys import PublicKey
|
|
|
|
|
2019-09-04 04:14:04 +08:00
|
|
|
# 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()
|
|
|
|
)
|
|
|
|
|
2019-08-14 09:19:01 +08:00
|
|
|
|
2018-11-19 00:22:56 +08:00
|
|
|
class ID:
|
2019-07-31 19:26:13 +08:00
|
|
|
_bytes: bytes
|
2019-07-31 21:35:50 +08:00
|
|
|
_xor_id: int = None
|
2019-08-01 21:48:30 +08:00
|
|
|
_b58_str: str = None
|
2019-07-27 16:27:01 +08:00
|
|
|
|
2019-07-31 19:26:13 +08:00
|
|
|
def __init__(self, peer_id_bytes: bytes) -> None:
|
|
|
|
self._bytes = peer_id_bytes
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2019-07-31 19:26:13 +08:00
|
|
|
@property
|
|
|
|
def xor_id(self) -> int:
|
|
|
|
if not self._xor_id:
|
|
|
|
self._xor_id = int(digest(self._bytes).hex(), 16)
|
|
|
|
return self._xor_id
|
2019-07-24 22:33:32 +08:00
|
|
|
|
2019-07-31 19:26:13 +08:00
|
|
|
def to_bytes(self) -> bytes:
|
|
|
|
return self._bytes
|
2019-04-21 05:35:05 +08:00
|
|
|
|
2019-08-01 21:48:30 +08:00
|
|
|
def to_base58(self) -> str:
|
|
|
|
if not self._b58_str:
|
|
|
|
self._b58_str = base58.b58encode(self._bytes).decode()
|
|
|
|
return self._b58_str
|
2019-04-25 10:11:54 +08:00
|
|
|
|
2019-08-14 09:18:29 +08:00
|
|
|
def __repr__(self) -> str:
|
2019-09-07 23:04:20 +08:00
|
|
|
return f"<libp2p.peer.id.ID ({self.to_string()})>"
|
2019-08-25 02:36:00 +08:00
|
|
|
|
2019-09-07 23:04:20 +08:00
|
|
|
__str__ = pretty = to_string = to_base58
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def __eq__(self, other: object) -> bool:
|
2019-08-01 21:48:30 +08:00
|
|
|
if isinstance(other, str):
|
|
|
|
return self.to_base58() == other
|
|
|
|
elif isinstance(other, bytes):
|
2019-07-31 19:26:13 +08:00
|
|
|
return self._bytes == other
|
|
|
|
elif isinstance(other, ID):
|
|
|
|
return self._bytes == other._bytes
|
|
|
|
else:
|
2019-07-27 16:27:01 +08:00
|
|
|
return NotImplemented
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def __hash__(self) -> int:
|
2019-07-31 19:26:13 +08:00
|
|
|
return hash(self._bytes)
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2019-07-31 19:31:58 +08:00
|
|
|
@classmethod
|
2019-08-01 13:25:20 +08:00
|
|
|
def from_base58(cls, b58_encoded_peer_id_str: str) -> "ID":
|
2019-07-31 19:31:58 +08:00
|
|
|
peer_id_bytes = base58.b58decode(b58_encoded_peer_id_str)
|
|
|
|
pid = ID(peer_id_bytes)
|
|
|
|
return pid
|
|
|
|
|
|
|
|
@classmethod
|
2019-08-14 09:19:01 +08:00
|
|
|
def from_pubkey(cls, key: PublicKey) -> "ID":
|
2019-08-21 01:01:36 +08:00
|
|
|
serialized_key = key.serialize()
|
2019-08-03 07:03:40 +08:00
|
|
|
algo = multihash.Func.sha2_256
|
2019-09-04 04:14:04 +08:00
|
|
|
if ENABLE_INLINING and len(serialized_key) <= MAX_INLINE_KEY_LENGTH:
|
|
|
|
algo = IDENTITY_MULTIHASH_CODE
|
2019-08-14 09:19:01 +08:00
|
|
|
mh_digest = multihash.digest(serialized_key, algo)
|
2019-07-31 19:31:58 +08:00
|
|
|
return cls(mh_digest.encode())
|
|
|
|
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def digest(data: Union[str, bytes]) -> bytes:
|
2019-07-30 23:41:28 +08:00
|
|
|
if isinstance(data, str):
|
2019-08-01 06:00:12 +08:00
|
|
|
data = data.encode("utf8")
|
2019-07-29 12:42:13 +08:00
|
|
|
return hashlib.sha1(data).digest()
|