1
+ from collections .abc import Callable
2
+
1
3
from libp2p .abc import (
4
+ IPeerStore ,
2
5
IRawConnection ,
3
6
ISecureConn ,
4
7
)
5
8
from libp2p .crypto .exceptions import (
6
9
MissingDeserializerError ,
7
10
)
8
11
from libp2p .crypto .keys import (
12
+ KeyPair ,
9
13
PrivateKey ,
10
14
PublicKey ,
11
15
)
30
34
from libp2p .peer .id import (
31
35
ID ,
32
36
)
37
+ from libp2p .peer .peerstore import (
38
+ PeerStoreError ,
39
+ )
33
40
from libp2p .security .base_session import (
34
41
BaseSession ,
35
42
)
36
43
from libp2p .security .base_transport import (
37
44
BaseSecureTransport ,
45
+ default_secure_bytes_provider ,
38
46
)
39
47
from libp2p .security .exceptions import (
40
48
HandshakeFailure ,
@@ -102,6 +110,7 @@ async def run_handshake(
102
110
conn : IRawConnection ,
103
111
is_initiator : bool ,
104
112
remote_peer_id : ID | None ,
113
+ peerstore : IPeerStore | None = None ,
105
114
) -> ISecureConn :
106
115
"""Raise `HandshakeFailure` when handshake failed."""
107
116
msg = make_exchange_message (local_private_key .get_public_key ())
@@ -164,7 +173,14 @@ async def run_handshake(
164
173
conn = conn ,
165
174
)
166
175
167
- # TODO: Store `pubkey` and `peer_id` to `PeerStore`
176
+ # Store `pubkey` and `peer_id` to `PeerStore`
177
+ if peerstore is not None :
178
+ try :
179
+ peerstore .add_pubkey (received_peer_id , received_pubkey )
180
+ except PeerStoreError :
181
+ # If peer ID and pubkey don't match, it would have already been caught above
182
+ # This might happen if the peer is already in the store
183
+ pass
168
184
169
185
return secure_conn
170
186
@@ -175,6 +191,18 @@ class InsecureTransport(BaseSecureTransport):
175
191
transport does not add any additional security.
176
192
"""
177
193
194
+ def __init__ (
195
+ self ,
196
+ local_key_pair : KeyPair ,
197
+ secure_bytes_provider : Callable [[int ], bytes ] | None = None ,
198
+ peerstore : IPeerStore | None = None ,
199
+ ) -> None :
200
+ # If secure_bytes_provider is None, use the default one
201
+ if secure_bytes_provider is None :
202
+ secure_bytes_provider = default_secure_bytes_provider
203
+ super ().__init__ (local_key_pair , secure_bytes_provider )
204
+ self .peerstore = peerstore
205
+
178
206
async def secure_inbound (self , conn : IRawConnection ) -> ISecureConn :
179
207
"""
180
208
Secure the connection, either locally or by communicating with opposing
@@ -183,8 +211,9 @@ async def secure_inbound(self, conn: IRawConnection) -> ISecureConn:
183
211
184
212
:return: secure connection object (that implements secure_conn_interface)
185
213
"""
214
+ # For inbound connections, we don't know the remote peer ID yet
186
215
return await run_handshake (
187
- self .local_peer , self .local_private_key , conn , False , None
216
+ self .local_peer , self .local_private_key , conn , False , None , self . peerstore
188
217
)
189
218
190
219
async def secure_outbound (self , conn : IRawConnection , peer_id : ID ) -> ISecureConn :
@@ -195,7 +224,7 @@ async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureCon
195
224
:return: secure connection object (that implements secure_conn_interface)
196
225
"""
197
226
return await run_handshake (
198
- self .local_peer , self .local_private_key , conn , True , peer_id
227
+ self .local_peer , self .local_private_key , conn , True , peer_id , self . peerstore
199
228
)
200
229
201
230
0 commit comments