sequencer/network/
libp2p.rs

1use anyhow::Result;
2use libp2p::{multiaddr::Protocol, Multiaddr, PeerId};
3
4/// Split off the peer ID from a multiaddress, returning the shortened address and the peer ID.
5///
6/// # Errors
7/// - If the last protocol in the address is not a peer ID.
8pub fn split_off_peer_id(mut address: Multiaddr) -> Result<(PeerId, Multiaddr)> {
9    let Some(Protocol::P2p(peer_id)) = address.pop() else {
10        return Err(anyhow::anyhow!("Failed to parse peer ID from address"));
11    };
12
13    Ok((peer_id, address))
14}