From 238c0162c0036f767dbf5672f8856def754625a3 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 18 Oct 2021 12:39:19 +0200 Subject: [PATCH] Add parse_and_resolve_peer_addr --- src/util.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/util.rs b/src/util.rs index 81ee89c..4f6e352 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,5 @@ use std::net::SocketAddr; +use std::net::ToSocketAddrs; use serde::Serialize; @@ -72,3 +73,16 @@ pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> { let ip = ip[1..].parse::().ok()?; Some((pubkey, ip)) } + +/// Parse and resolve a peer's address including public key, written in the format: +/// `@:` +pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec)> { + let delim = peer.find('@')?; + let (key, host) = peer.split_at(delim); + let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?; + let hosts = host[1..] + .to_socket_addrs() + .ok()? + .collect::>(); + Some((pubkey, hosts)) +}