aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/util.rs14
1 files changed, 14 insertions, 0 deletions
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::<SocketAddr>().ok()?;
Some((pubkey, ip))
}
+
+/// Parse and resolve a peer's address including public key, written in the format:
+/// `<public key hex>@<ip or hostname>:<port>`
+pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr>)> {
+ 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::<Vec<_>>();
+ Some((pubkey, hosts))
+}