Skip to main content

nym_noise_keys/
lib.rs

1// Copyright 2025 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: GPL-3.0-only
3
4use nym_crypto::asymmetric::x25519;
5use nym_crypto::asymmetric::x25519::serde_helpers::bs58_x25519_pubkey;
6use serde::{Deserialize, Serialize};
7
8#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
9#[serde(from = "u8", into = "u8")]
10pub enum NoiseVersion {
11    V1,
12    Unknown(u8), //Implies a newer version we don't know
13}
14
15impl From<u8> for NoiseVersion {
16    fn from(value: u8) -> Self {
17        match value {
18            1 => NoiseVersion::V1,
19            other => NoiseVersion::Unknown(other),
20        }
21    }
22}
23
24impl From<NoiseVersion> for u8 {
25    fn from(version: NoiseVersion) -> Self {
26        match version {
27            NoiseVersion::V1 => 1,
28            NoiseVersion::Unknown(other) => other,
29        }
30    }
31}
32
33// to whoever is thinking of modifying this struct.
34// you MUST NOT change its structure in any way - adding, removing or changing fields
35// otherwise, it will break old clients as bincode serialisation is not backwards compatible
36// even if you put `#[serde(default)]` all over the place
37#[derive(
38    Copy, Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, utoipa::ToSchema, PartialEq,
39)]
40pub struct VersionedNoiseKeyV1 {
41    #[schemars(with = "u8")]
42    #[schema(value_type = u8)]
43    pub supported_version: NoiseVersion,
44
45    #[schemars(with = "String")]
46    #[serde(with = "bs58_x25519_pubkey")]
47    #[schema(value_type = String)]
48    pub x25519_pubkey: x25519::PublicKey,
49}