Skip to content

Commit 30b5811

Browse files
committed
feat: store pubkey and peerid in peerstore
1 parent 67ca1d7 commit 30b5811

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

examples/doc-examples/example_encryption_insecure.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ async def main():
2727
# secure_bytes_provider: Optional function to generate secure random bytes
2828
# (defaults to secrets.token_bytes)
2929
secure_bytes_provider=None, # Use default implementation
30+
# peerstore: Optional peerstore to store peer IDs and public keys
31+
# (defaults to None)
32+
peerstore=None,
3033
)
3134

3235
# Create a security options dictionary mapping protocol ID to transport

libp2p/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ def new_swarm(
200200
key_pair, noise_privkey=noise_key_pair.private_key
201201
),
202202
TProtocol(secio.ID): secio.Transport(key_pair),
203-
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair),
203+
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(
204+
key_pair, peerstore=peerstore_opt
205+
),
204206
}
205207

206208
# Use given muxer preference if provided, otherwise use global default

libp2p/security/insecure/transport.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from typing import (
2+
Callable,
23
Optional,
34
)
45

56
from libp2p.abc import (
7+
IPeerStore,
68
IRawConnection,
79
ISecureConn,
810
)
911
from libp2p.crypto.exceptions import (
1012
MissingDeserializerError,
1113
)
1214
from libp2p.crypto.keys import (
15+
KeyPair,
1316
PrivateKey,
1417
PublicKey,
1518
)
@@ -34,6 +37,9 @@
3437
from libp2p.peer.id import (
3538
ID,
3639
)
40+
from libp2p.peer.peerstore import (
41+
PeerStoreError,
42+
)
3743
from libp2p.security.base_session import (
3844
BaseSession,
3945
)
@@ -106,6 +112,7 @@ async def run_handshake(
106112
conn: IRawConnection,
107113
is_initiator: bool,
108114
remote_peer_id: ID,
115+
peerstore: Optional[IPeerStore] = None,
109116
) -> ISecureConn:
110117
"""Raise `HandshakeFailure` when handshake failed."""
111118
msg = make_exchange_message(local_private_key.get_public_key())
@@ -159,7 +166,14 @@ async def run_handshake(
159166
conn=conn,
160167
)
161168

162-
# TODO: Store `pubkey` and `peer_id` to `PeerStore`
169+
# Store `pubkey` and `peer_id` to `PeerStore`
170+
if peerstore is not None:
171+
try:
172+
peerstore.add_pubkey(received_peer_id, received_pubkey)
173+
except PeerStoreError:
174+
# If peer ID and pubkey don't match, it would have already been caught above
175+
# This might happen if the peer is already in the store
176+
pass
163177

164178
return secure_conn
165179

@@ -170,6 +184,15 @@ class InsecureTransport(BaseSecureTransport):
170184
transport does not add any additional security.
171185
"""
172186

187+
def __init__(
188+
self,
189+
local_key_pair: KeyPair,
190+
secure_bytes_provider: Optional[Callable[[int], bytes]] = None,
191+
peerstore: Optional[IPeerStore] = None,
192+
) -> None:
193+
super().__init__(local_key_pair, secure_bytes_provider)
194+
self.peerstore = peerstore
195+
173196
async def secure_inbound(self, conn: IRawConnection) -> ISecureConn:
174197
"""
175198
Secure the connection, either locally or by communicating with opposing
@@ -179,7 +202,7 @@ async def secure_inbound(self, conn: IRawConnection) -> ISecureConn:
179202
:return: secure connection object (that implements secure_conn_interface)
180203
"""
181204
return await run_handshake(
182-
self.local_peer, self.local_private_key, conn, False, None
205+
self.local_peer, self.local_private_key, conn, False, None, self.peerstore
183206
)
184207

185208
async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureConn:
@@ -190,7 +213,7 @@ async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureCon
190213
:return: secure connection object (that implements secure_conn_interface)
191214
"""
192215
return await run_handshake(
193-
self.local_peer, self.local_private_key, conn, True, peer_id
216+
self.local_peer, self.local_private_key, conn, True, peer_id, self.peerstore
194217
)
195218

196219

tests/core/stream_muxer/test_yamux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async def secure_conn_pair(key_pair, peer_id):
7171
client_rw = TrioStreamAdapter(client_send, client_receive)
7272
server_rw = TrioStreamAdapter(server_send, server_receive)
7373

74-
insecure_transport = InsecureTransport(key_pair)
74+
insecure_transport = InsecureTransport(key_pair, peerstore=None)
7575

7676
async def run_outbound(nursery_results):
7777
with trio.move_on_after(5):

tests/utils/factories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ def noise_handshake_payload_factory() -> NoiseHandshakePayload:
159159
)
160160

161161

162-
def plaintext_transport_factory(key_pair: KeyPair) -> ISecureTransport:
163-
return InsecureTransport(key_pair)
162+
def plaintext_transport_factory(key_pair: KeyPair, peerstore=None) -> ISecureTransport:
163+
return InsecureTransport(key_pair, peerstore=peerstore)
164164

165165

166166
def secio_transport_factory(key_pair: KeyPair) -> ISecureTransport:

0 commit comments

Comments
 (0)