+impl Display for SocketAddress {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ match self {
+ SocketAddress::TcpIpV4{addr, port} => write!(
+ f, "{}.{}.{}.{}:{}", addr[0], addr[1], addr[2], addr[3], port)?,
+ SocketAddress::TcpIpV6{addr, port} => write!(
+ f,
+ "[{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}]:{}",
+ addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15], port
+ )?,
+ SocketAddress::OnionV2(bytes) => write!(f, "OnionV2({:?})", bytes)?,
+ SocketAddress::OnionV3 {
+ ed25519_pubkey,
+ checksum,
+ version,
+ port,
+ } => {
+ let [first_checksum_flag, second_checksum_flag] = checksum.to_be_bytes();
+ let mut addr = vec![*version, first_checksum_flag, second_checksum_flag];
+ addr.extend_from_slice(ed25519_pubkey);
+ let onion = base32::Alphabet::RFC4648 { padding: false }.encode(&addr);
+ write!(f, "{}.onion:{}", onion, port)?
+ },
+ SocketAddress::Hostname { hostname, port } => write!(f, "{}:{}", hostname, port)?,
+ }
+ Ok(())
+ }
+}
+