2019-07-27 16:27:01 +08:00
|
|
|
from typing import (
|
|
|
|
Union,
|
|
|
|
)
|
|
|
|
|
|
|
|
from Crypto.PublicKey.RSA import (
|
|
|
|
RsaKey,
|
|
|
|
)
|
2019-04-25 10:18:03 +08:00
|
|
|
import hashlib
|
2018-11-19 00:22:56 +08:00
|
|
|
import base58
|
2018-11-29 23:06:40 +08:00
|
|
|
import multihash
|
|
|
|
|
|
|
|
# MaxInlineKeyLength is the maximum length a key can be for it to be inlined in
|
|
|
|
# the peer ID.
|
|
|
|
# * When `len(pubKey.Bytes()) <= MaxInlineKeyLength`, the peer ID is the
|
|
|
|
# identity multihash hash of the public key.
|
|
|
|
# * When `len(pubKey.Bytes()) > MaxInlineKeyLength`, the peer ID is the
|
|
|
|
# sha2-256 multihash of the public key.
|
|
|
|
MAX_INLINE_KEY_LENGTH = 42
|
|
|
|
|
2018-11-19 00:22:56 +08:00
|
|
|
|
|
|
|
class ID:
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
_id_str: str
|
|
|
|
|
|
|
|
def __init__(self, id_str: str) -> None:
|
2018-11-19 00:22:56 +08:00
|
|
|
self._id_str = id_str
|
|
|
|
|
2019-07-24 22:33:32 +08:00
|
|
|
def to_bytes(self) -> bytes:
|
|
|
|
return self._id_str
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def get_raw_id(self) -> str:
|
2019-04-21 05:35:05 +08:00
|
|
|
return self._id_str
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def pretty(self) -> str:
|
2018-11-19 00:22:56 +08:00
|
|
|
return base58.b58encode(self._id_str).decode()
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def get_xor_id(self) -> int:
|
2019-04-25 10:11:54 +08:00
|
|
|
return int(digest(self.get_raw_id()).hex(), 16)
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def __str__(self) -> str:
|
2018-11-19 00:22:56 +08:00
|
|
|
pid = self.pretty()
|
2019-05-22 10:25:08 +08:00
|
|
|
return pid
|
2018-11-19 00:22:56 +08:00
|
|
|
|
2018-11-27 07:24:29 +08:00
|
|
|
__repr__ = __str__
|
2018-11-29 23:06:40 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def __eq__(self, other: object) -> bool:
|
2018-11-29 23:06:40 +08:00
|
|
|
#pylint: disable=protected-access
|
2019-07-27 16:27:01 +08:00
|
|
|
if not isinstance(other, ID):
|
|
|
|
return NotImplemented
|
2018-11-29 23:06:40 +08:00
|
|
|
return self._id_str == other._id_str
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def __hash__(self) -> int:
|
2018-11-29 23:06:40 +08:00
|
|
|
return hash(self._id_str)
|
|
|
|
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def id_b58_encode(peer_id: ID) -> str:
|
2018-11-29 23:06:40 +08:00
|
|
|
"""
|
|
|
|
return a b58-encoded string
|
|
|
|
"""
|
|
|
|
#pylint: disable=protected-access
|
2019-07-27 16:27:01 +08:00
|
|
|
return base58.b58encode(peer_id.get_raw_id()).decode()
|
2018-11-29 23:06:40 +08:00
|
|
|
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def id_b58_decode(peer_id_str: str) -> ID:
|
2018-11-29 23:06:40 +08:00
|
|
|
"""
|
|
|
|
return a base58-decoded peer ID
|
|
|
|
"""
|
|
|
|
return ID(base58.b58decode(peer_id_str))
|
|
|
|
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def id_from_public_key(key: RsaKey) -> ID:
|
2018-11-29 23:06:40 +08:00
|
|
|
# export into binary format
|
|
|
|
key_bin = key.exportKey("DER")
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
algo: int = multihash.Func.sha2_256
|
2018-11-29 23:06:40 +08:00
|
|
|
# TODO: seems identity is not yet supported in pymultihash
|
|
|
|
# if len(b) <= MAX_INLINE_KEY_LENGTH:
|
|
|
|
# algo multihash.func.identity
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
mh_digest: multihash.Multihash = multihash.digest(key_bin, algo)
|
2018-11-29 23:06:40 +08:00
|
|
|
return ID(mh_digest.encode())
|
|
|
|
|
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def id_from_private_key(key: RsaKey) -> ID:
|
2018-11-29 23:06:40 +08:00
|
|
|
return id_from_public_key(key.publickey())
|
2019-04-25 10:11:54 +08:00
|
|
|
|
2019-07-27 16:27:01 +08:00
|
|
|
def digest(data: Union[str, bytes]) -> bytes:
|
|
|
|
if not isinstance(data, bytes):
|
|
|
|
data_bytes = str(data).encode('utf8')
|
|
|
|
return hashlib.sha1(data_bytes).digest()
|