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
@ -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
|
||||
|
||||
Reference in New Issue
Block a user