@@ -20,7 +20,7 @@ public abstract class IdentifyProtocolBase(IProtocolStackSettings protocolStackS
2020 private readonly IProtocolStackSettings _protocolStackSettings = protocolStackSettings ;
2121 private readonly IdentifyProtocolSettings _settings = settings ?? new IdentifyProtocolSettings ( ) ;
2222
23- protected async Task ReadAndVerifyIndentity ( IChannel channel , ISessionContext context )
23+ protected async Task ReadAndVerifyIdentity ( IChannel channel , ISessionContext context )
2424 {
2525 ArgumentNullException . ThrowIfNull ( context . State . RemotePublicKey ) ;
2626 ArgumentNullException . ThrowIfNull ( context . State . RemotePeerId ) ;
@@ -34,11 +34,28 @@ protected async Task ReadAndVerifyIndentity(IChannel channel, ISessionContext co
3434 throw new PeerConnectionException ( "Malformed peer identity: the remote public key corresponds to a different peer id" ) ;
3535 }
3636
37- ulong seq = 0 ;
37+ VerifySignedPeerRecordOrThrow ( identify . SignedPeerRecord , context . State . RemotePublicKey , context . State . RemotePeerId , out ulong seq ) ;
3838
39- if ( identify . SignedPeerRecord is not null )
39+ if ( _peerStore is not null )
40+ {
41+ PeerStore . PeerInfo peerInfo = _peerStore . GetPeerInfo ( context . State . RemotePeerId ) ;
42+
43+ if ( identify . SignedPeerRecord is not null && peerInfo . Seq >= seq )
44+ {
45+ // do nothing if seq is for an older record
46+ return ;
47+ }
48+ peerInfo . SupportedProtocols = identify . Protocols . ToArray ( ) ;
49+ peerInfo . SignedPeerRecord = identify . SignedPeerRecord ;
50+ peerInfo . Seq = seq ;
51+ }
52+ }
53+
54+ private void VerifySignedPeerRecordOrThrow ( ByteString ? signedPeerRecordBytes , PublicKey remotePublicKey , PeerId remotePeerId , out ulong seq )
55+ {
56+ if ( signedPeerRecordBytes is not null )
4057 {
41- if ( ! SigningHelper . VerifyPeerRecord ( identify . SignedPeerRecord , context . State . RemotePublicKey , out seq ) )
58+ if ( ! SigningHelper . VerifyPeerRecord ( signedPeerRecordBytes , remotePublicKey , out seq ) )
4259 {
4360 if ( _settings ? . PeerRecordsVerificationPolicy == PeerRecordsVerificationPolicy . RequireCorrect )
4461 {
@@ -51,43 +68,38 @@ protected async Task ReadAndVerifyIndentity(IChannel channel, ISessionContext co
5168 }
5269 else
5370 {
54- _logger ? . LogDebug ( "Confirmed peer record: {peerId}" , context . State . RemotePeerId ) ;
71+ _logger ? . LogDebug ( "Confirmed peer record: {peerId}" , remotePeerId ) ;
5572 }
5673 }
5774 else if ( _settings . PeerRecordsVerificationPolicy != PeerRecordsVerificationPolicy . DoesNotRequire )
5875 {
5976 throw new PeerConnectionException ( "Malformed peer identity: there is no peer record which is required" ) ;
6077 }
6178
62- if ( _peerStore is not null )
63- {
64- PeerStore . PeerInfo peerInfo = _peerStore . GetPeerInfo ( context . State . RemotePeerId ) ;
65-
66- if ( identify . SignedPeerRecord is not null && peerInfo . Seq >= seq )
67- {
68- // do nothing if seq is for an older record
69- return ;
70- }
71- peerInfo . SupportedProtocols = identify . Protocols . ToArray ( ) ;
72- peerInfo . SignedPeerRecord = identify . SignedPeerRecord ;
73- peerInfo . Seq = seq ;
74- }
79+ seq = 0 ;
7580 }
7681
7782 protected async Task SendIdentity ( IChannel channel , ISessionContext context , ulong idVersion = 1 )
7883 {
84+ ArgumentNullException . ThrowIfNull ( context . State . RemoteAddress ) ;
85+ Libp2pSetupException . ThrowIfNull ( _protocolStackSettings . Protocols ) ;
86+
7987 Identify . Dto . Identify identify = new ( )
8088 {
8189 ProtocolVersion = _settings . ProtocolVersion ,
8290 AgentVersion = _settings . AgentVersion ,
8391 PublicKey = context . Peer . Identity . PublicKey . ToByteString ( ) ,
8492 ListenAddrs = { } ,
85- ObservedAddr = ByteString . CopyFrom ( context . State . RemoteAddress ! . ToEndPoint ( out ProtocolType proto ) . ToMultiaddress ( proto ) . ToBytes ( ) ) ,
86- Protocols = { _protocolStackSettings . Protocols ! . Select ( r => r . Key . Protocol ) . OfType < ISessionListenerProtocol > ( ) . Select ( p => p . Id ) } ,
93+ ObservedAddr = ByteString . CopyFrom ( context . State . RemoteAddress . GetEndpointPart ( ) . ToBytes ( ) ) ,
94+ Protocols = { _protocolStackSettings . Protocols . Select ( r => r . Key . Protocol ) . OfType < ISessionListenerProtocol > ( ) . Select ( p => p . Id ) } ,
8795 SignedPeerRecord = SigningHelper . CreateSignedEnvelope ( context . Peer . Identity , [ .. context . Peer . ListenAddresses ] , idVersion ) ,
8896 } ;
8997
90- ByteString [ ] endpoints = context . Peer . ListenAddresses . Where ( a => ! a . ToEndPoint ( ) . Address . IsPrivate ( ) ) . Select ( a => a . ToEndPoint ( out ProtocolType proto ) . ToMultiaddress ( proto ) ) . Select ( a => ByteString . CopyFrom ( a . ToBytes ( ) ) ) . ToArray ( ) ;
98+ ByteString [ ] endpoints = context . Peer . ListenAddresses
99+ . Where ( a => ! a . ToEndPoint ( ) . Address . IsPrivate ( ) )
100+ . Select ( a => a . ToEndPoint ( out ProtocolType proto ) . ToMultiaddress ( proto ) )
101+ . Select ( a => ByteString . CopyFrom ( a . ToBytes ( ) ) ) . ToArray ( ) ;
102+
91103 identify . ListenAddrs . AddRange ( endpoints ) ;
92104
93105 await channel . WriteSizeAndProtobufAsync ( identify ) ;
0 commit comments