diff --git a/libp2p/__init__.py b/libp2p/__init__.py index 88ae3b54..5c738ed7 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -154,9 +154,9 @@ async def new_node( # TODO routing unimplemented host: IHost # If not explicitly typed, MyPy raises error if disc_opt: - host = RoutedHost(swarm_opt, disc_opt) + host = RoutedHost(key_pair.public_key, swarm_opt, disc_opt) else: - host = BasicHost(swarm_opt) + host = BasicHost(key_pair.public_key, swarm_opt) # Kick off cleanup job asyncio.ensure_future(cleanup_done_tasks()) diff --git a/libp2p/host/basic_host.py b/libp2p/host/basic_host.py index 0a5dae7b..73218d32 100644 --- a/libp2p/host/basic_host.py +++ b/libp2p/host/basic_host.py @@ -46,9 +46,11 @@ class BasicHost(IHost): def __init__( self, + public_key: PublicKey, network: INetwork, default_protocols: "OrderedDict[TProtocol, StreamHandlerFn]" = None, ) -> None: + self._public_key = public_key self._network = network self._network.set_stream_handler(self._swarm_stream_handler) self.peerstore = self._network.peerstore @@ -63,6 +65,9 @@ class BasicHost(IHost): """ return self._network.get_peer_id() + def get_public_key(self) -> PublicKey: + return self._public_key + def get_network(self) -> INetwork: """ :return: network instance of host diff --git a/libp2p/host/host_interface.py b/libp2p/host/host_interface.py index 6a326b97..9e557969 100644 --- a/libp2p/host/host_interface.py +++ b/libp2p/host/host_interface.py @@ -17,6 +17,12 @@ class IHost(ABC): :return: peer_id of host """ + @abstractmethod + def get_public_key(self) -> PublicKey: + """ + :return: the public key belonging to the peer + """ + @abstractmethod def get_network(self) -> INetwork: """ diff --git a/libp2p/host/routed_host.py b/libp2p/host/routed_host.py index 78b6fa54..e253ce1c 100644 --- a/libp2p/host/routed_host.py +++ b/libp2p/host/routed_host.py @@ -1,3 +1,4 @@ +from libp2p.crypto.keys import PublicKey from libp2p.host.basic_host import BasicHost from libp2p.host.exceptions import ConnectionFailure from libp2p.network.network_interface import INetwork @@ -10,8 +11,8 @@ from libp2p.routing.interfaces import IPeerRouting class RoutedHost(BasicHost): _router: IPeerRouting - def __init__(self, network: INetwork, router: IPeerRouting): - super().__init__(network) + def __init__(self, public_key: PublicKey, network: INetwork, router: IPeerRouting): + super().__init__(public_key, network) self._router = router async def connect(self, peer_info: PeerInfo) -> None: