diff --git a/core/archipelago/src/peers.rs b/core/archipelago/src/peers.rs index 8d2f3a44..80edfa80 100644 --- a/core/archipelago/src/peers.rs +++ b/core/archipelago/src/peers.rs @@ -50,6 +50,13 @@ pub async fn save_peers(data_dir: &Path, peers: &[KnownPeer]) -> Result<()> { } pub async fn add_peer(data_dir: &Path, peer: KnownPeer) -> Result> { + // Self-add guard: skip if the candidate pubkey matches our own identity. + // Auto-peering paths (e.g., node-message receive handler) can otherwise + // echo-back our own pubkey when messages bounce through federation, which + // ended up in users' peer lists as a phantom "self-peer" entry. + if is_own_pubkey(data_dir, &peer.pubkey).await { + return load_peers(data_dir).await; + } let mut peers = load_peers(data_dir).await?; let exists = peers.iter().any(|p| p.pubkey == peer.pubkey); if !exists { @@ -59,6 +66,20 @@ pub async fn add_peer(data_dir: &Path, peer: KnownPeer) -> Result Ok(peers) } +/// Reads `data_dir/identity/node_key.pub` and compares to `candidate` (hex). +/// Returns false on any I/O or format issue — guard is best-effort; a real +/// peer with a colliding pubkey is impossible, so a false negative just +/// means the entry is added normally. +async fn is_own_pubkey(data_dir: &Path, candidate: &str) -> bool { + let path = data_dir.join("identity/node_key.pub"); + let bytes = match tokio::fs::read(&path).await { + Ok(b) => b, + Err(_) => return false, + }; + let own_hex = hex::encode(&bytes); + own_hex.eq_ignore_ascii_case(candidate.trim()) +} + pub async fn remove_peer(data_dir: &Path, pubkey: &str) -> Result> { let mut peers = load_peers(data_dir).await?; peers.retain(|p| p.pubkey != pubkey);