nym_types/
gateway.rs

1use crate::currency::{DecCoin, RegisteredCoins};
2use crate::error::TypesError;
3use nym_mixnet_contract_common::{
4    Gateway as MixnetContractGateway, GatewayBond as MixnetContractGatewayBond,
5};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::fmt;
9
10#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
11#[cfg_attr(
12    feature = "generate-ts",
13    ts(export, export_to = "ts-packages/types/src/types/rust/Gateway.ts")
14)]
15#[derive(Clone, Debug, Deserialize, PartialEq, Eq, PartialOrd, Serialize, JsonSchema)]
16pub struct Gateway {
17    pub host: String,
18    pub mix_port: u16,
19    pub clients_port: u16,
20    pub location: String,
21    pub sphinx_key: String,
22    /// Base58 encoded ed25519 EdDSA public key of the gateway used to derive shared keys with clients
23    pub identity_key: String,
24    pub version: String,
25}
26
27impl From<MixnetContractGateway> for Gateway {
28    fn from(value: MixnetContractGateway) -> Self {
29        let MixnetContractGateway {
30            host,
31            mix_port,
32            clients_port,
33            location,
34            sphinx_key,
35            identity_key,
36            version,
37        } = value;
38
39        Gateway {
40            host,
41            mix_port,
42            clients_port,
43            location,
44            sphinx_key,
45            identity_key,
46            version,
47        }
48    }
49}
50
51#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
52#[cfg_attr(
53    feature = "generate-ts",
54    ts(export, export_to = "ts-packages/types/src/types/rust/GatewayBond.ts")
55)]
56#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, JsonSchema)]
57pub struct GatewayBond {
58    pub pledge_amount: DecCoin,
59    pub owner: String,
60    pub block_height: u64,
61    pub gateway: Gateway,
62    pub proxy: Option<String>,
63}
64
65impl GatewayBond {
66    pub fn from_mixnet_contract_gateway_bond(
67        bond: MixnetContractGatewayBond,
68        reg: &RegisteredCoins,
69    ) -> Result<GatewayBond, TypesError> {
70        Ok(GatewayBond {
71            pledge_amount: reg.attempt_convert_to_display_dec_coin(bond.pledge_amount.into())?,
72            owner: bond.owner.to_string(),
73            block_height: bond.block_height,
74            gateway: bond.gateway.into(),
75            proxy: bond.proxy.map(|p| p.into_string()),
76        })
77    }
78}
79
80#[derive(Debug, Serialize, Deserialize)]
81pub struct GatewayNodeDetailsResponse {
82    pub identity_key: String,
83    pub sphinx_key: String,
84    pub bind_address: String,
85    pub mix_port: u16,
86    pub clients_port: u16,
87    pub config_path: String,
88    pub data_store: String,
89
90    pub network_requester: Option<GatewayNetworkRequesterDetails>,
91    pub ip_packet_router: Option<GatewayIpPacketRouterDetails>,
92}
93
94impl fmt::Display for GatewayNodeDetailsResponse {
95    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96        writeln!(f, "config path: {}", self.config_path)?;
97        writeln!(f, "identity key: {}", self.identity_key)?;
98        writeln!(f, "sphinx key: {}", self.sphinx_key)?;
99        writeln!(f, "bind address: {}", self.bind_address)?;
100        writeln!(
101            f,
102            "mix port: {}, clients port: {}",
103            self.mix_port, self.clients_port
104        )?;
105
106        writeln!(f, "data store is at: {}", self.data_store)?;
107
108        if let Some(nr) = &self.network_requester {
109            nr.fmt(f)?;
110        }
111
112        if let Some(ipr) = &self.ip_packet_router {
113            ipr.fmt(f)?;
114        }
115        Ok(())
116    }
117}
118
119#[derive(Debug, Serialize, Deserialize)]
120pub struct GatewayNetworkRequesterDetails {
121    pub enabled: bool,
122
123    pub identity_key: String,
124    pub encryption_key: String,
125
126    pub open_proxy: bool,
127
128    // just a convenience wrapper around all the keys
129    pub address: String,
130
131    pub config_path: String,
132}
133
134impl fmt::Display for GatewayNetworkRequesterDetails {
135    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136        writeln!(f, "Network requester:")?;
137        writeln!(f, "\tenabled: {}", self.enabled)?;
138        writeln!(f, "\tconfig path: {}", self.config_path)?;
139
140        writeln!(f, "\tidentity key: {}", self.identity_key)?;
141        writeln!(f, "\tencryption key: {}", self.encryption_key)?;
142        writeln!(f, "\taddress: {}", self.address)?;
143
144        writeln!(f, "\tuses open proxy: {}", self.open_proxy)
145    }
146}
147
148#[derive(Debug, Serialize, Deserialize)]
149pub struct GatewayIpPacketRouterDetails {
150    pub enabled: bool,
151
152    pub identity_key: String,
153    pub encryption_key: String,
154
155    // just a convenience wrapper around all the keys
156    pub address: String,
157
158    pub config_path: String,
159}
160
161impl fmt::Display for GatewayIpPacketRouterDetails {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        writeln!(f, "ip packet router:")?;
164        writeln!(f, "\tenabled: {}", self.enabled)?;
165        writeln!(f, "\tconfig path: {}", self.config_path)?;
166
167        writeln!(f, "\tidentity key: {}", self.identity_key)?;
168        writeln!(f, "\tencryption key: {}", self.encryption_key)?;
169        writeln!(f, "\taddress: {}", self.address)
170    }
171}