Add parse_and_resolve_peer_addr

This commit is contained in:
Alex 2021-10-18 12:39:19 +02:00
parent e621ba49de
commit 238c0162c0
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1

View file

@ -1,4 +1,5 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use serde::Serialize; use serde::Serialize;
@ -72,3 +73,16 @@ pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> {
let ip = ip[1..].parse::<SocketAddr>().ok()?; let ip = ip[1..].parse::<SocketAddr>().ok()?;
Some((pubkey, ip)) 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))
}