2022-10-09 16:50:40 +02:00
|
|
|
//! Waku node [configuration](https://rfc.vac.dev/spec/36/#jsonconfig-type) related items
|
|
|
|
|
|
2022-10-03 15:21:19 +02:00
|
|
|
// std
|
|
|
|
|
// crates
|
2022-10-17 19:30:07 +02:00
|
|
|
use secp256k1::SecretKey;
|
2022-10-03 15:21:19 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-12-21 00:33:08 +13:00
|
|
|
use smart_default::SmartDefault;
|
2022-10-03 15:21:19 +02:00
|
|
|
// internal
|
|
|
|
|
|
|
|
|
|
/// Waku node configuration
|
2022-12-21 00:33:08 +13:00
|
|
|
#[derive(Clone, SmartDefault, Serialize, Deserialize, Debug)]
|
2022-10-03 15:21:19 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct WakuNodeConfig {
|
|
|
|
|
/// Listening IP address. Default `0.0.0.0`
|
2022-12-21 00:33:08 +13:00
|
|
|
#[default(Some(std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0))))]
|
2022-10-17 19:30:07 +02:00
|
|
|
pub host: Option<std::net::IpAddr>,
|
2022-10-03 15:21:19 +02:00
|
|
|
/// Libp2p TCP listening port. Default `60000`. Use `0` for **random**
|
2022-12-21 00:33:08 +13:00
|
|
|
#[default(Some(60000))]
|
2022-10-17 19:30:07 +02:00
|
|
|
pub port: Option<usize>,
|
2022-10-03 15:21:19 +02:00
|
|
|
/// Secp256k1 private key in Hex format (`0x123...abc`). Default random
|
2024-02-08 17:16:22 -04:00
|
|
|
#[serde(with = "secret_key_serde", rename = "key")]
|
2022-10-17 19:30:07 +02:00
|
|
|
pub node_key: Option<SecretKey>,
|
2022-10-03 15:21:19 +02:00
|
|
|
/// Enable relay protocol. Default `true`
|
2022-12-21 00:33:08 +13:00
|
|
|
#[default(Some(true))]
|
2022-10-17 19:30:07 +02:00
|
|
|
pub relay: Option<bool>,
|
2024-02-14 16:52:21 -04:00
|
|
|
pub relay_topics: Vec<String>,
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod secret_key_serde {
|
2022-10-17 19:30:07 +02:00
|
|
|
use secp256k1::SecretKey;
|
2022-10-03 15:21:19 +02:00
|
|
|
use serde::de::Error;
|
|
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
|
|
|
|
|
|
pub fn serialize<S>(key: &Option<SecretKey>, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
|
where
|
|
|
|
|
S: Serializer,
|
|
|
|
|
{
|
2022-10-17 19:30:07 +02:00
|
|
|
let as_string: Option<String> = key.as_ref().map(|key| hex::encode(key.secret_bytes()));
|
2022-10-03 15:21:19 +02:00
|
|
|
as_string.serialize(serializer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<SecretKey>, D::Error>
|
|
|
|
|
where
|
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
|
{
|
|
|
|
|
let as_string: Option<String> = Option::<String>::deserialize(deserializer)?;
|
|
|
|
|
match as_string {
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
Some(s) => {
|
|
|
|
|
let key_bytes = hex::decode(s).map_err(|e| D::Error::custom(format!("{e}")))?;
|
|
|
|
|
Ok(Some(
|
2022-10-17 19:30:07 +02:00
|
|
|
SecretKey::from_slice(&key_bytes)
|
2022-10-03 15:21:19 +02:00
|
|
|
.map_err(|e| D::Error::custom(format!("{e}")))?,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|