nym-node-requests 1.21.5-rc.2

Nym Node API endpoint definitions 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
// Copyright 2023-2025 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0

use crate::api::v1::node::models::{
    LegacyHostInformationV1, LegacyHostInformationV2, LegacyHostInformationV3,
};
use crate::error::Error;
use nym_crypto::asymmetric::ed25519;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::ops::Deref;

#[cfg(feature = "client")]
pub mod client;
pub mod helpers;
pub mod v1;
pub mod v2;

#[cfg(feature = "client")]
pub use client::Client;

// create the type alias manually if openapi is not enabled
pub type SignedHostInformation = SignedData<crate::api::v1::node::models::HostInformation>;
pub type SignedLewesProtocol = SignedData<crate::api::v1::lewes_protocol::models::LewesProtocol>;

#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct SignedDataHostInfo {
    // #[serde(flatten)]
    pub data: crate::api::v1::node::models::HostInformation,
    pub signature: String,
}

#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct SignedLewesProtocolInfo {
    // #[serde(flatten)]
    pub data: crate::api::v1::lewes_protocol::models::LewesProtocol,
    pub signature: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedData<T> {
    // #[serde(flatten)]
    pub data: T,

    #[serde(with = "ed25519::bs58_ed25519_signature")]
    pub signature: ed25519::Signature,
}

impl<T> SignedData<T> {
    pub fn new(data: T, key: &ed25519::PrivateKey) -> Result<Self, Error>
    where
        T: Serialize,
    {
        let plaintext = serde_json::to_string(&data)?;

        let signature = key.sign(plaintext);
        Ok(SignedData { data, signature })
    }

    pub fn verify(&self, key: &ed25519::PublicKey) -> bool
    where
        T: Serialize,
    {
        let Ok(plaintext) = serde_json::to_string(&self.data) else {
            return false;
        };

        key.verify(plaintext, &self.signature).is_ok()
    }
}

impl SignedHostInformation {
    pub fn verify_host_information(&self) -> bool {
        if self.verify(&self.keys.ed25519_identity) {
            return true;
        }

        // TODO: @JS: to remove downgrade support in future release(s)

        let legacy_v3 = SignedData {
            data: LegacyHostInformationV3::from(self.data.clone()),
            signature: self.signature,
        };

        if legacy_v3.verify(&self.keys.ed25519_identity) {
            return true;
        }

        // attempt to verify legacy signatures
        let legacy_v3 = SignedData {
            data: LegacyHostInformationV3::from(self.data.clone()),
            signature: self.signature,
        };

        if legacy_v3.verify(&self.keys.ed25519_identity) {
            return true;
        }

        let legacy_v2 = SignedData {
            data: LegacyHostInformationV2::from(legacy_v3.data),
            signature: self.signature,
        };

        if legacy_v2.verify(&self.keys.ed25519_identity) {
            return true;
        }

        SignedData {
            data: LegacyHostInformationV1::from(legacy_v2.data),
            signature: self.signature,
        }
        .verify(&self.keys.ed25519_identity)
    }
}

impl<T> Deref for SignedData<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct ErrorResponse {
    pub message: String,
}

impl Display for ErrorResponse {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.message.fmt(f)
    }
}

#[allow(deprecated)]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::v1::node::models::{HostKeys, SphinxKey};
    use nym_crypto::asymmetric::{ed25519, x25519};
    use nym_noise_keys::{NoiseVersion, VersionedNoiseKeyV1};
    use nym_test_utils::helpers::deterministic_rng;

    #[test]
    fn dummy_signed_host_verification() {
        let mut rng = deterministic_rng();
        let ed22519 = ed25519::KeyPair::new(&mut rng);
        let x25519_sphinx = x25519::KeyPair::new(&mut rng);
        let x25519_sphinx2 = x25519::KeyPair::new(&mut rng);
        let x25519_versioned_noise = VersionedNoiseKeyV1 {
            supported_version: NoiseVersion::V1,
            x25519_pubkey: *x25519::KeyPair::new(&mut rng).public_key(),
        };

        let current_rotation_id = 1234;

        // no pre-announced keys
        let host_info = crate::api::v1::node::models::HostInformation {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::HostKeys {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: current_rotation_id,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: None,
                x25519_versioned_noise: None,
            },
        };

        let signed_info = SignedHostInformation::new(host_info, ed22519.private_key()).unwrap();
        assert!(signed_info.verify(ed22519.public_key()));
        assert!(signed_info.verify_host_information());

        let host_info_with_noise = crate::api::v1::node::models::HostInformation {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::HostKeys {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: current_rotation_id,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: None,
                x25519_versioned_noise: Some(x25519_versioned_noise),
            },
        };

        let signed_info =
            SignedHostInformation::new(host_info_with_noise, ed22519.private_key()).unwrap();
        assert!(signed_info.verify(ed22519.public_key()));
        assert!(signed_info.verify_host_information());

        // with pre-announced keys
        let host_info = crate::api::v1::node::models::HostInformation {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::HostKeys {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: current_rotation_id,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: Some(SphinxKey {
                    rotation_id: current_rotation_id + 1,
                    public_key: *x25519_sphinx2.public_key(),
                }),
                x25519_versioned_noise: None,
            },
        };

        let signed_info = SignedHostInformation::new(host_info, ed22519.private_key()).unwrap();
        assert!(signed_info.verify(ed22519.public_key()));
        assert!(signed_info.verify_host_information());

        let host_info_with_noise = crate::api::v1::node::models::HostInformation {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::HostKeys {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: current_rotation_id,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: Some(SphinxKey {
                    rotation_id: current_rotation_id + 1,
                    public_key: *x25519_sphinx2.public_key(),
                }),
                x25519_versioned_noise: Some(x25519_versioned_noise),
            },
        };

        let signed_info =
            SignedHostInformation::new(host_info_with_noise, ed22519.private_key()).unwrap();
        assert!(signed_info.verify(ed22519.public_key()));
        assert!(signed_info.verify_host_information());
    }

    #[test]
    fn dummy_legacy_v3_signed_host_verification() {
        let mut rng = deterministic_rng();
        let ed22519 = ed25519::KeyPair::new(&mut rng);
        let x25519_sphinx = x25519::KeyPair::new(&mut rng);
        let x25519_noise = x25519::KeyPair::new(&mut rng);

        let legacy_info_no_noise = crate::api::v1::node::models::LegacyHostInformationV3 {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::LegacyHostKeysV3 {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                x25519_noise: None,
            },
        };

        // note the usage of u32::max rotation id (as that's what the legacy data would be deserialised into)
        let current_struct = crate::api::v1::node::models::HostInformation {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: HostKeys {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: u32::MAX,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: None,
                x25519_versioned_noise: None,
            },
        };

        // signature on legacy data
        let signature = SignedData::new(legacy_info_no_noise, ed22519.private_key())
            .unwrap()
            .signature;

        // signed blob with the 'current' structure
        let current_struct = SignedData {
            data: current_struct,
            signature,
        };

        assert!(!current_struct.verify(ed22519.public_key()));
        assert!(current_struct.verify_host_information());

        // //technically this variant should never happen
        let legacy_info_noise = crate::api::v1::node::models::LegacyHostInformationV3 {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::LegacyHostKeysV3 {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                x25519_noise: Some(*x25519_noise.public_key()),
            },
        };

        // note the usage of u32::max rotation id (as that's what the legacy data would be deserialised into)
        let current_struct_noise = crate::api::v1::node::models::HostInformation {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: HostKeys {
                ed25519_identity: *ed22519.public_key(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: u32::MAX,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: None,
                x25519_versioned_noise: Some(VersionedNoiseKeyV1 {
                    supported_version: NoiseVersion::V1,
                    x25519_pubkey: legacy_info_noise.keys.x25519_noise.unwrap(),
                }),
            },
        };

        // signature on legacy data

        let signature_noise = SignedData::new(legacy_info_noise, ed22519.private_key())
            .unwrap()
            .signature;

        // signed blob with the 'current' structure

        let current_struct_noise = SignedData {
            data: current_struct_noise,
            signature: signature_noise,
        };

        assert!(!current_struct_noise.verify(ed22519.public_key()));
        assert!(current_struct_noise.verify_host_information())
    }

    #[test]
    fn dummy_legacy_v2_signed_host_verification() {
        let mut rng = deterministic_rng();
        let ed22519 = ed25519::KeyPair::new(&mut rng);
        let x25519_sphinx = x25519::KeyPair::new(&mut rng);
        let x25519_noise = x25519::KeyPair::new(&mut rng);

        let legacy_info_no_noise = crate::api::v1::node::models::LegacyHostInformationV2 {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::LegacyHostKeysV2 {
                ed25519_identity: ed22519.public_key().to_base58_string(),
                x25519_sphinx: x25519_sphinx.public_key().to_base58_string(),
                x25519_noise: "".to_string(),
            },
        };

        let legacy_info_noise = crate::api::v1::node::models::LegacyHostInformationV2 {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::LegacyHostKeysV2 {
                ed25519_identity: ed22519.public_key().to_base58_string(),
                x25519_sphinx: x25519_sphinx.public_key().to_base58_string(),
                x25519_noise: x25519_noise.public_key().to_base58_string(),
            },
        };

        // note the usage of u32::max rotation id (as that's what the legacy data would be deserialised into)
        let host_info_no_noise = crate::api::v1::node::models::HostInformation {
            ip_address: legacy_info_no_noise.ip_address.clone(),
            hostname: legacy_info_no_noise.hostname.clone(),
            keys: crate::api::v1::node::models::HostKeys {
                ed25519_identity: legacy_info_no_noise.keys.ed25519_identity.parse().unwrap(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: u32::MAX,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: None,
                x25519_versioned_noise: None,
            },
        };

        // note the usage of u32::max rotation id (as that's what the legacy data would be deserialised into)
        let host_info_noise = crate::api::v1::node::models::HostInformation {
            ip_address: legacy_info_noise.ip_address.clone(),
            hostname: legacy_info_noise.hostname.clone(),
            keys: crate::api::v1::node::models::HostKeys {
                ed25519_identity: legacy_info_noise.keys.ed25519_identity.parse().unwrap(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: u32::MAX,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: None,
                x25519_versioned_noise: Some(VersionedNoiseKeyV1 {
                    supported_version: NoiseVersion::V1,
                    x25519_pubkey: legacy_info_noise.keys.x25519_noise.parse().unwrap(),
                }),
            },
        };

        // signature on legacy data
        let signature_no_noise = SignedData::new(legacy_info_no_noise, ed22519.private_key())
            .unwrap()
            .signature;

        let signature_noise = SignedData::new(legacy_info_noise, ed22519.private_key())
            .unwrap()
            .signature;

        // signed blob with the 'current' structure
        let current_struct_no_noise = SignedData {
            data: host_info_no_noise,
            signature: signature_no_noise,
        };

        let current_struct_noise = SignedData {
            data: host_info_noise,
            signature: signature_noise,
        };

        assert!(!current_struct_no_noise.verify(ed22519.public_key()));
        assert!(current_struct_no_noise.verify_host_information());

        assert!(!current_struct_noise.verify(ed22519.public_key()));
        assert!(current_struct_noise.verify_host_information())
    }

    #[test]
    fn dummy_legacy_v1_signed_host_verification() {
        let mut rng = deterministic_rng();
        let ed22519 = ed25519::KeyPair::new(&mut rng);
        let x25519_sphinx = x25519::KeyPair::new(&mut rng);

        let legacy_info = crate::api::v1::node::models::LegacyHostInformationV1 {
            ip_address: vec!["1.1.1.1".parse().unwrap()],
            hostname: Some("foomp.com".to_string()),
            keys: crate::api::v1::node::models::LegacyHostKeysV1 {
                ed25519: ed22519.public_key().to_base58_string(),
                x25519: x25519_sphinx.public_key().to_base58_string(),
            },
        };

        // note the usage of u32::max rotation id (as that's what the legacy data would be deserialised into)
        let host_info = crate::api::v1::node::models::HostInformation {
            ip_address: legacy_info.ip_address.clone(),
            hostname: legacy_info.hostname.clone(),
            keys: crate::api::v1::node::models::HostKeys {
                ed25519_identity: legacy_info.keys.ed25519.parse().unwrap(),
                x25519_sphinx: *x25519_sphinx.public_key(),
                primary_x25519_sphinx_key: SphinxKey {
                    rotation_id: u32::MAX,
                    public_key: *x25519_sphinx.public_key(),
                },
                pre_announced_x25519_sphinx_key: None,
                x25519_versioned_noise: None,
            },
        };

        // signature on legacy data
        let signature = SignedData::new(legacy_info, ed22519.private_key())
            .unwrap()
            .signature;

        // signed blob with the 'current' structure
        let current_struct = SignedData {
            data: host_info,
            signature,
        };

        assert!(!current_struct.verify(ed22519.public_key()));
        assert!(current_struct.verify_host_information())
    }
}