Skip to content

Commit

Permalink
network/types: Ensure peerID functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Nov 21, 2024
1 parent 281e1eb commit f09e25c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions substrate/client/network/types/src/multiaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ impl Multiaddr {

Ok(())
}

/// Ensure the peer ID is present in the multiaddress.
///
/// Returns None when the peer ID of the address is different from the local peer ID.
pub fn ensure_peer_id(self, local_peer_id: PeerId) -> Option<Multiaddr> {
if let Some(Protocol::P2p(peer_id)) = self.iter().last() {
// Invalid address if the reported peer ID is not the local peer ID.
if peer_id != *local_peer_id.as_ref() {
return None
}

return Some(self)
}

// Ensure the address contains the local peer ID.
Some(self.with(Protocol::P2p(local_peer_id.into())))
}
}

impl Display for Multiaddr {
Expand Down

0 comments on commit f09e25c

Please sign in to comment.