mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
Add basic support for multiaddr addresses and improvement around peer id (#75)
* Improved peer ID construction and usage * peer id object is directly passed to the network no need to cast from a string to an ID * don't base64 encode the peer id when loading from public key * use proper multiaddr address - keep multiaddr object into peerstore instead of string - update network code to use new multiaddr lib - update tests and example * don't instanciate peerstore object in constructor This has side effect where the same peerstore is used for different instance of Libp2p * add connect method to basic_host * use zaibon's fork of sbuss/py-multiaddr * lint
This commit is contained in:
committed by
Robert Zajac
parent
7fa674dee2
commit
611de28aca
49
peer/id.py
49
peer/id.py
@ -1,4 +1,14 @@
|
||||
import base58
|
||||
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
|
||||
|
||||
|
||||
class ID:
|
||||
|
||||
@ -15,3 +25,42 @@ class ID:
|
||||
return "<peer.ID %s*%s>" % (pid[:2], pid[len(pid)-6:])
|
||||
|
||||
__repr__ = __str__
|
||||
|
||||
def __eq__(self, other):
|
||||
#pylint: disable=protected-access
|
||||
return self._id_str == other._id_str
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self._id_str)
|
||||
|
||||
|
||||
def id_b58_encode(peer_id):
|
||||
"""
|
||||
return a b58-encoded string
|
||||
"""
|
||||
#pylint: disable=protected-access
|
||||
return base58.b58encode(peer_id._id_str).decode()
|
||||
|
||||
|
||||
def id_b58_decode(peer_id_str):
|
||||
"""
|
||||
return a base58-decoded peer ID
|
||||
"""
|
||||
return ID(base58.b58decode(peer_id_str))
|
||||
|
||||
|
||||
def id_from_public_key(key):
|
||||
# export into binary format
|
||||
key_bin = key.exportKey("DER")
|
||||
|
||||
algo = multihash.Func.sha2_256
|
||||
# TODO: seems identity is not yet supported in pymultihash
|
||||
# if len(b) <= MAX_INLINE_KEY_LENGTH:
|
||||
# algo multihash.func.identity
|
||||
|
||||
mh_digest = multihash.digest(key_bin, algo)
|
||||
return ID(mh_digest.encode())
|
||||
|
||||
|
||||
def id_from_private_key(key):
|
||||
return id_from_public_key(key.publickey())
|
||||
|
||||
@ -1,5 +1,42 @@
|
||||
import multiaddr
|
||||
import multiaddr.util
|
||||
from peer.id import id_b58_decode
|
||||
from peer.peerdata import PeerData
|
||||
|
||||
|
||||
class PeerInfo:
|
||||
# pylint: disable=too-few-public-methods
|
||||
def __init__(self, peer_id, peer_data):
|
||||
self.peer_id = peer_id
|
||||
self.addrs = peer_data.get_addrs()
|
||||
|
||||
|
||||
def info_from_p2p_addr(addr):
|
||||
if not addr:
|
||||
raise InvalidAddrError()
|
||||
|
||||
parts = multiaddr.util.split(addr)
|
||||
if not parts:
|
||||
raise InvalidAddrError()
|
||||
|
||||
ipfspart = parts[-1]
|
||||
if ipfspart.protocols()[0].code != multiaddr.protocols.P_IPFS:
|
||||
raise InvalidAddrError()
|
||||
|
||||
# make sure the /ipfs value parses as a peer.ID
|
||||
peer_id_str = ipfspart.value_for_protocol(multiaddr.protocols.P_IPFS)
|
||||
peer_id = id_b58_decode(peer_id_str)
|
||||
|
||||
# we might have received just an / ipfs part, which means there's no addr.
|
||||
if len(parts) > 1:
|
||||
addr = multiaddr.util.join(parts[:-1])
|
||||
|
||||
peer_data = PeerData()
|
||||
peer_data.addrs = [addr]
|
||||
peer_data.protocols = [p.code for p in addr.protocols()]
|
||||
|
||||
return PeerInfo(peer_id, peer_data)
|
||||
|
||||
|
||||
class InvalidAddrError(ValueError):
|
||||
pass
|
||||
|
||||
@ -42,7 +42,7 @@ class PeerStore(IPeerStore):
|
||||
peer.set_protocols(protocols)
|
||||
|
||||
def peers(self):
|
||||
return self.peer_map.keys()
|
||||
return list(self.peer_map.keys())
|
||||
|
||||
def get(self, peer_id, key):
|
||||
if peer_id in self.peer_map:
|
||||
|
||||
Reference in New Issue
Block a user