Skip to content

Commit

Permalink
network/types: Move verification of external addr
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 109bb1c commit 281e1eb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions substrate/client/network/types/src/multiaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use protocol::Protocol;

// Re-export the macro under shorter name under `multiaddr`.
pub use crate::build_multiaddr as multiaddr;
use crate::PeerId;

/// [`Multiaddr`] type used in Substrate. Converted to libp2p's `Multiaddr`
/// or litep2p's `Multiaddr` when passed to the corresponding network backend.
Expand Down Expand Up @@ -76,6 +77,41 @@ impl Multiaddr {
pub fn to_vec(&self) -> Vec<u8> {
self.multiaddr.to_vec()
}

/// Verify the external address is valid.
///
/// An external address address discovered by the network is valid when:
/// - the address is not empty
/// - the address contains a valid IP address
/// - the address is for the local peer ID
pub fn verify_external_address(&self, local_peer_id: PeerId) -> Result<(), ParseError> {
if self.is_empty() {
return Err(ParseError::InvalidMultiaddr);
}

// Expect the address to contain a valid IP address.
if !std::matches!(
self.iter().next(),
Some(
Protocol::Dns(_) |
Protocol::Dns4(_) |
Protocol::Dns6(_) |
Protocol::Ip6(_) |
Protocol::Ip4(_),
)
) {
return Err(ParseError::InvalidMultiaddr);
}

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 Err(ParseError::InvalidMultiaddr);
}
}

Ok(())
}
}

impl Display for Multiaddr {
Expand Down

0 comments on commit 281e1eb

Please sign in to comment.