diff --git a/libp2p/__init__.py b/libp2p/__init__.py index a1dca53..75b6764 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -105,6 +105,10 @@ def initialize_default_swarm( ) peerstore = peerstore_opt or PeerStore() + # Store our key pair in peerstore + peerstore.add_pubkey(id_opt, key_pair.public_key) + peerstore.add_privkey(id_opt, key_pair.private_key) + # TODO: Initialize discovery if not presented return Swarm(id_opt, peerstore, upgrader, transport) @@ -151,9 +155,9 @@ async def new_node( # TODO routing unimplemented host: IHost # If not explicitly typed, MyPy raises error if disc_opt: - host = RoutedHost(key_pair.public_key, swarm_opt, disc_opt) + host = RoutedHost(swarm_opt, disc_opt) else: - host = BasicHost(key_pair.public_key, swarm_opt) + host = BasicHost(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 7469d33..cd0d289 100644 --- a/libp2p/host/basic_host.py +++ b/libp2p/host/basic_host.py @@ -39,7 +39,6 @@ class BasicHost(IHost): right after a stream is initialized. """ - _public_key: PublicKey _network: INetwork peerstore: IPeerStore @@ -48,11 +47,9 @@ 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 @@ -68,7 +65,7 @@ class BasicHost(IHost): return self._network.get_peer_id() def get_public_key(self) -> PublicKey: - return self._public_key + return self.peerstore.pubkey(self.get_id()) def get_network(self) -> INetwork: """ diff --git a/libp2p/host/routed_host.py b/libp2p/host/routed_host.py index e253ce1..78b6fa5 100644 --- a/libp2p/host/routed_host.py +++ b/libp2p/host/routed_host.py @@ -1,4 +1,3 @@ -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 @@ -11,8 +10,8 @@ from libp2p.routing.interfaces import IPeerRouting class RoutedHost(BasicHost): _router: IPeerRouting - def __init__(self, public_key: PublicKey, network: INetwork, router: IPeerRouting): - super().__init__(public_key, network) + def __init__(self, network: INetwork, router: IPeerRouting): + super().__init__(network) self._router = router async def connect(self, peer_info: PeerInfo) -> None: