nym-api-requests 3.0.0

Nym API request types and functions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2026 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: GPL-3.0-only

use crate::models::described::type_translation::LewesProtocolDetailsV1;
use crate::models::described::v1::NymNodeDescriptionV1;
use crate::models::described::v2::{
    AnnouncePortsV2, AuthenticatorDetailsV2, DeclaredRolesV2, DescribedNodeTypeV2,
    HostInformationV2, HostKeysV2, IpPacketRouterDetailsV2, NetworkRequesterDetailsV2,
    NymNodeAuxiliaryDetailsV2, NymNodeDataV2, NymNodeDescriptionV2, SphinxKeyV2,
    VersionedNoiseKeyV2, WebSocketsV2, WireguardDetailsV2,
};
use crate::models::{BinaryBuildInformationOwned, OffsetDateTimeJsonSchemaWrapper};
use crate::nym_nodes::{
    BasicEntryInformation, NodeRole, SemiSkimmedNodeV1, SemiSkimmedNodeV3, SkimmedNodeV1,
    SkimmedNodeV2,
};
use celes::Country;
use nym_crypto::asymmetric::{ed25519, x25519};
use nym_mixnet_contract_common::reward_params::Performance;
use nym_mixnet_contract_common::NodeId;
use nym_network_defaults::{DEFAULT_MIX_LISTENING_PORT, DEFAULT_VERLOC_LISTENING_PORT};
use serde::{Deserialize, Serialize};
use tracing::warn;
use utoipa::ToSchema;

// no changes for the following types
pub type AnnouncePortsV3 = AnnouncePortsV2;
pub type HostInformationV3 = HostInformationV2;
pub type DeclaredRolesV3 = DeclaredRolesV2;
pub type NetworkRequesterDetailsV3 = NetworkRequesterDetailsV2;
pub type IpPacketRouterDetailsV3 = IpPacketRouterDetailsV2;
pub type AuthenticatorDetailsV3 = AuthenticatorDetailsV2;
pub type WireguardDetailsV3 = WireguardDetailsV2;
pub type WebSocketsV3 = WebSocketsV2;
pub type DescribedNodeTypeV3 = DescribedNodeTypeV2;
pub type HostKeysV3 = HostKeysV2;
pub type SphinxKeyV3 = SphinxKeyV2;
pub type VersionedNoiseKeyV3 = VersionedNoiseKeyV2;
pub type LewesProtocolDetailsV3 = LewesProtocolDetailsV1;

#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct NymNodeDescriptionV3 {
    #[schema(value_type = u32)]
    pub node_id: NodeId,
    pub contract_node_type: DescribedNodeTypeV3,
    pub description: NymNodeDataV3,
}

impl NymNodeDescriptionV3 {
    pub fn version(&self) -> &str {
        &self.description.build_information.build_version
    }

    pub fn entry_information(&self) -> BasicEntryInformation {
        BasicEntryInformation {
            hostname: self.description.host_information.hostname.clone(),
            ws_port: self.description.mixnet_websockets.ws_port,
            wss_port: self.description.mixnet_websockets.wss_port,
        }
    }

    pub fn ed25519_identity_key(&self) -> ed25519::PublicKey {
        self.description.host_information.keys.ed25519
    }

    pub fn current_sphinx_key(&self, current_rotation_id: u32) -> x25519::PublicKey {
        let keys = &self.description.host_information.keys;

        if keys.current_x25519_sphinx_key.rotation_id == u32::MAX {
            // legacy case (i.e. node doesn't support rotation)
            return keys.current_x25519_sphinx_key.public_key;
        }

        if current_rotation_id == keys.current_x25519_sphinx_key.rotation_id {
            // it's the 'current' key
            return keys.current_x25519_sphinx_key.public_key;
        }

        if let Some(pre_announced) = &keys.pre_announced_x25519_sphinx_key {
            if pre_announced.rotation_id == current_rotation_id {
                return pre_announced.public_key;
            }
        }

        warn!(
            "unexpected key rotation {current_rotation_id} for node {}",
            self.node_id
        );
        // this should never be reached, but just in case, return the fallback option
        keys.current_x25519_sphinx_key.public_key
    }

    pub fn to_skimmed_node(
        &self,
        current_rotation_id: u32,
        role: NodeRole,
        performance: Performance,
    ) -> SkimmedNodeV1 {
        let keys = &self.description.host_information.keys;
        let entry = if self.description.declared_role.entry {
            Some(self.entry_information())
        } else {
            None
        };

        SkimmedNodeV1 {
            node_id: self.node_id,
            ed25519_identity_pubkey: keys.ed25519,
            ip_addresses: self.description.host_information.ip_address.clone(),
            mix_port: self.description.mix_port(),
            x25519_sphinx_pubkey: self.current_sphinx_key(current_rotation_id),
            // we can't use the declared roles, we have to take whatever was provided in the contract.
            // why? say this node COULD operate as an exit, but it might be the case the contract decided
            // to assign it an ENTRY role only. we have to use that one instead.
            role,
            supported_roles: self.description.declared_role,
            entry,
            performance,
        }
    }

    pub fn to_semi_skimmed_node(
        &self,
        current_rotation_id: u32,
        role: NodeRole,
        performance: Performance,
    ) -> SemiSkimmedNodeV1 {
        let skimmed_node = self.to_skimmed_node(current_rotation_id, role, performance);

        SemiSkimmedNodeV1 {
            basic: skimmed_node,
            x25519_noise_versioned_key: self
                .description
                .host_information
                .keys
                .x25519_versioned_noise,
        }
    }

    pub fn to_semi_skimmed_node_v3(
        &self,
        current_rotation_id: u32,
        role: NodeRole,
        performance: Performance,
    ) -> SemiSkimmedNodeV3 {
        let skimmed_node = self.to_skimmed_node(current_rotation_id, role, performance);

        SemiSkimmedNodeV3 {
            basic: skimmed_node,
            noise_key: self
                .description
                .host_information
                .keys
                .x25519_versioned_noise,
            build_version: self.description.build_information.build_version.clone(),
            lp: self.description.lewes_protocol.clone(),
        }
    }
}

pub trait NymNodeDescriptionConversion: Sized {
    fn from_description(
        node: &NymNodeDescriptionV3,
        current_rotation_id: u32,
        role: NodeRole,
        performance: Performance,
    ) -> Option<Self>;
}

impl NymNodeDescriptionConversion for SkimmedNodeV1 {
    fn from_description(
        node: &NymNodeDescriptionV3,
        current_rotation_id: u32,
        role: NodeRole,
        performance: Performance,
    ) -> Option<Self> {
        // The original implementation stays to avoid handling an option that can't actually fail all over the place
        Some(node.to_skimmed_node(current_rotation_id, role, performance))
    }
}

impl NymNodeDescriptionConversion for SkimmedNodeV2 {
    fn from_description(
        node: &NymNodeDescriptionV3,
        current_rotation_id: u32,
        role: NodeRole,
        performance: Performance,
    ) -> Option<Self> {
        let keys = &node.description.host_information.keys;
        let entry = if node.description.declared_role.entry {
            Some(node.entry_information())
        } else {
            None
        };

        let lp = node.description.lewes_protocol.clone()?;

        Some(SkimmedNodeV2 {
            node_id: node.node_id,
            ed25519_identity_pubkey: keys.ed25519,
            ip_addresses: node.description.host_information.ip_address.clone(),
            mix_port: node.description.mix_port(),
            x25519_sphinx_pubkey: node.current_sphinx_key(current_rotation_id),
            // we can't use the declared roles, we have to take whatever was provided in the contract.
            // why? say this node COULD operate as an exit, but it might be the case the contract decided
            // to assign it an ENTRY role only. we have to use that one instead.
            role,
            supported_roles: node.description.declared_role,
            entry,
            performance,
            lp,
            build_version: node.description.build_information.build_version.clone(),
        })
    }
}

impl From<NymNodeDescriptionV3> for NymNodeDescriptionV2 {
    fn from(value: NymNodeDescriptionV3) -> Self {
        NymNodeDescriptionV2 {
            node_id: value.node_id,
            contract_node_type: value.contract_node_type,
            description: value.description.into(),
        }
    }
}

impl From<NymNodeDescriptionV3> for NymNodeDescriptionV1 {
    fn from(value: NymNodeDescriptionV3) -> Self {
        NymNodeDescriptionV1 {
            node_id: value.node_id,
            contract_node_type: value.contract_node_type,
            description: NymNodeDataV2::from(value.description).into(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct NymNodeDataV3 {
    #[serde(default)]
    pub last_polled: OffsetDateTimeJsonSchemaWrapper,

    pub host_information: HostInformationV3,

    #[serde(default)]
    pub declared_role: DeclaredRolesV3,

    #[serde(default)]
    pub auxiliary_details: NymNodeAuxiliaryDetailsV3,

    // TODO: do we really care about ALL build info or just the version?
    pub build_information: BinaryBuildInformationOwned,

    #[serde(default)]
    pub network_requester: Option<NetworkRequesterDetailsV3>,

    #[serde(default)]
    pub ip_packet_router: Option<IpPacketRouterDetailsV3>,

    #[serde(default)]
    pub authenticator: Option<AuthenticatorDetailsV3>,

    #[serde(default)]
    pub wireguard: Option<WireguardDetailsV3>,

    // for now we only care about their ws/wss situation, nothing more
    pub mixnet_websockets: WebSocketsV3,

    #[serde(default)]
    pub lewes_protocol: Option<LewesProtocolDetailsV3>,
}

impl NymNodeDataV3 {
    pub fn mix_port(&self) -> u16 {
        self.auxiliary_details
            .announce_ports
            .mix_port
            .unwrap_or(DEFAULT_MIX_LISTENING_PORT)
    }

    pub fn verloc_port(&self) -> u16 {
        self.auxiliary_details
            .announce_ports
            .verloc_port
            .unwrap_or(DEFAULT_VERLOC_LISTENING_PORT)
    }
}

impl From<NymNodeDataV3> for NymNodeDataV2 {
    fn from(data: NymNodeDataV3) -> Self {
        NymNodeDataV2 {
            last_polled: data.last_polled,
            host_information: data.host_information,
            declared_role: data.declared_role,
            auxiliary_details: data.auxiliary_details.into(),
            build_information: data.build_information,
            network_requester: data.network_requester,
            ip_packet_router: data.ip_packet_router,
            authenticator: data.authenticator,
            wireguard: data.wireguard,
            mixnet_websockets: data.mixnet_websockets,
            lewes_protocol: data.lewes_protocol,
        }
    }
}

#[derive(
    Clone, Debug, Default, Serialize, Deserialize, schemars::JsonSchema, ToSchema, PartialEq,
)]
pub struct NymNodeAuxiliaryDetailsV3 {
    /// Optional ISO 3166 alpha-2 two-letter country code of the node's **physical** location
    #[schema(example = "PL", value_type = Option<String>)]
    #[schemars(with = "Option<String>")]
    #[schemars(length(equal = 2))]
    pub location: Option<Country>,

    /// On-chain address of this node
    #[serde(default)]
    pub address: Option<String>,

    #[serde(default)]
    pub announce_ports: AnnouncePortsV3,

    /// Specifies whether this node operator has agreed to the terms and conditions
    /// as defined at <https://nymtech.net/terms-and-conditions/operators/v1.0.0>
    // make sure to include the default deserialisation as this field hasn't existed when the struct was first created
    #[serde(default)]
    pub accepted_operator_terms_and_conditions: bool,
}

impl From<NymNodeAuxiliaryDetailsV3> for NymNodeAuxiliaryDetailsV2 {
    fn from(value: NymNodeAuxiliaryDetailsV3) -> Self {
        NymNodeAuxiliaryDetailsV2 {
            location: value.location,
            announce_ports: value.announce_ports,
            accepted_operator_terms_and_conditions: value.accepted_operator_terms_and_conditions,
        }
    }
}

#[cfg(any(test, feature = "mock-fixtures"))]
pub fn mock_nym_node_description(seed: u64) -> NymNodeDescriptionV3 {
    use nym_node_requests::api::v1::lewes_protocol::models::{LPHashFunction, LPKEM};
    use nym_test_utils::helpers::{u64_seeded_rng, Rng010};

    let mut rng = u64_seeded_rng(seed);

    let ed25519 = nym_crypto::asymmetric::ed25519::KeyPair::new(&mut rng);

    // just reuse the same x25519 key for everything - this is just a data mock
    let x25519 = nym_crypto::asymmetric::x25519::KeyPair::new(&mut rng);

    let mut dummy_kems = std::collections::BTreeMap::new();
    for kem in [LPKEM::McEliece, LPKEM::McEliece] {
        let mut kem_digests = std::collections::BTreeMap::new();
        for (i, sf) in [
            LPHashFunction::Blake3,
            LPHashFunction::Shake128,
            LPHashFunction::Shake256,
            LPHashFunction::Sha256,
        ]
        .iter()
        .enumerate()
        {
            kem_digests.insert(*sf, hex::encode([((seed + i as u64) % 256) as u8; 32]));
        }
        dummy_kems.insert(kem, kem_digests);
    }

    // make sure the serialisation stays the same and signature is still valid
    let dummy_lp = nym_node_requests::api::v1::lewes_protocol::models::LewesProtocol {
        enabled: false,
        control_port: 123,
        data_port: 345,
        x25519: (*x25519.public_key()).into(),
        kem_keys: dummy_kems,
    };
    let dummy_signed_lp =
        nym_node_requests::api::SignedLewesProtocol::new(dummy_lp, ed25519.private_key()).unwrap();

    NymNodeDescriptionV3 {
        node_id: rng.next_u32(),
        contract_node_type: DescribedNodeTypeV3::NymNode,
        description: NymNodeDataV3 {
            last_polled: time::OffsetDateTime::from_unix_timestamp(1767225600)
                .unwrap()
                .into(),
            host_information: HostInformationV3 {
                ip_address: vec![
                    std::net::IpAddr::V4(std::net::Ipv4Addr::new(1, 2, 3, (seed % 255) as u8)),
                ],
                hostname: Some(format!("my-awesome-node-{seed}.com")),
                keys: HostKeysV3 {
                    ed25519: *ed25519.public_key(),
                    x25519: *x25519.public_key(),
                    current_x25519_sphinx_key: SphinxKeyV3 {
                        rotation_id: 123,
                        public_key: *x25519.public_key(),
                    },
                    pre_announced_x25519_sphinx_key: None,
                    x25519_versioned_noise: Some(VersionedNoiseKeyV3 {
                        supported_version: nym_noise_keys::NoiseVersion::V1,
                        x25519_pubkey: *x25519.public_key(),
                    }),
                },
            },
            declared_role: DeclaredRolesV3 {
                mixnode: false,
                entry: true,
                exit_nr: true,
                exit_ipr: true,
            },
            auxiliary_details: NymNodeAuxiliaryDetailsV3 {
                location: Some(celes::Country::switzerland()),
                address: Some("n1jw6mp7d5xqc7w6xm79lha27glmd0vdt3l9artf".to_string()),
                announce_ports: Default::default(),
                accepted_operator_terms_and_conditions: true,
            },
            build_information: BinaryBuildInformationOwned {
                binary_name: "dummy-node".to_string(),
                build_timestamp: "2021-02-23T20:14:46.558472672+00:00".to_string(),
                build_version: "0.1.0-9-g46f83e1".to_string(),
                commit_sha: "46f83e112520533338245862d366f6a02cef07d4".to_string(),
                commit_timestamp: "2021-02-23T08:08:02-05:00".to_string(),
                commit_branch: "master".to_string(),
                rustc_version: "1.52.0-nightly".to_string(),
                rustc_channel: "nightly".to_string(),
                cargo_profile: "release".to_string(),
                cargo_triple: "wasm32-unknown-unknown".to_string(),
            },
            network_requester: Some(NetworkRequesterDetailsV3 {
                address: "FhtkzizQg2JbZ19kGkRKXdjV2QnFbT5ww88ZAKaD4nkF.7Remi4UVYzn1yL3qYtEcQBGh6tzTYxMdYB4uqyHVc5Z4@62F81C9GrHDRja9WCqozemRFSzFPMecY85MbGwn6efve".to_string(),
                uses_exit_policy: true,
            }),
            ip_packet_router: Some(IpPacketRouterDetailsV3 {
                address: "FhtkzizQg2JbZ19kGkRKXdjV2QnFbT5ww88ZAKaD4nkF.7Remi4UVYzn1yL3qYtEcQBGh6tzTYxMdYB4uqyHVc5Z4@62F81C9GrHDRja9WCqozemRFSzFPMecY85MbGwn6efve".to_string(),
            }),
            authenticator: Some(AuthenticatorDetailsV3 {
                address: "FhtkzizQg2JbZ19kGkRKXdjV2QnFbT5ww88ZAKaD4nkF.7Remi4UVYzn1yL3qYtEcQBGh6tzTYxMdYB4uqyHVc5Z4@62F81C9GrHDRja9WCqozemRFSzFPMecY85MbGwn6efve".to_string(),
            }),
            wireguard: Some(WireguardDetailsV3 {
                port: 123,
                tunnel_port: 234,
                metadata_port: 456,
                public_key: x25519.public_key().to_base58_string(),
            }),
            lewes_protocol: Some(dummy_signed_lp.into()),
            mixnet_websockets: WebSocketsV3 {
                ws_port: 9000,
                wss_port: None,
            },
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_skimmed_node_carries_the_published_lp_details() {
        let described = mock_nym_node_description(42);
        let published = described.description.lewes_protocol.clone().unwrap();

        let skimmed =
            SkimmedNodeV2::from_description(&described, 0, Default::default(), Default::default())
                .unwrap();

        assert_eq!(skimmed.lp.content, published.content);
        assert_ne!(skimmed.lp.content.data_port, skimmed.mix_port);
        assert_eq!(
            skimmed.build_version,
            described.description.build_information.build_version
        );
    }

    #[test]
    fn a_node_that_published_no_lp_details_yields_no_skimmed_node() {
        let mut described = mock_nym_node_description(42);
        described.description.lewes_protocol = None;

        // it has no address on an LP route, so it cannot be handed out as a hop at all
        assert!(SkimmedNodeV2::from_description(
            &described,
            0,
            Default::default(),
            Default::default()
        )
        .is_none());
    }
}