1
0
mirror of synced 2025-01-11 08:15:48 +00:00

Dns or socket address for mixnet node config (#460)

This commit is contained in:
gusto 2023-10-16 14:57:43 +02:00 committed by GitHub
parent 9f69934fe2
commit 39d45d3c09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,11 +21,32 @@ pub struct Layer {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Node {
#[serde(with = "addr_serde")]
pub address: SocketAddr,
#[serde(with = "hex_serde")]
pub public_key: [u8; PUBLIC_KEY_SIZE],
}
mod addr_serde {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::net::{SocketAddr, ToSocketAddrs};
pub fn serialize<S: Serializer>(addr: &SocketAddr, serializer: S) -> Result<S::Ok, S::Error> {
addr.to_string().serialize(serializer)
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<SocketAddr, D::Error> {
let s = String::deserialize(deserializer)?;
// Try to convert the string (which might be a domain name) to a SocketAddr.
let mut addrs = s.to_socket_addrs().map_err(serde::de::Error::custom)?;
addrs
.next()
.ok_or_else(|| serde::de::Error::custom("Failed to resolve to a valid address"))
}
}
mod hex_serde {
use super::PUBLIC_KEY_SIZE;
use serde::{Deserialize, Deserializer, Serialize, Serializer};