diff --git a/common/crypto/Cargo.toml b/common/crypto/Cargo.toml index d84a721241..c0b7d29bcc 100644 --- a/common/crypto/Cargo.toml +++ b/common/crypto/Cargo.toml @@ -32,7 +32,7 @@ config = { path="../../common/config" } rand_chacha = "0.2" [features] -serde = ["serde_crate", "serde_bytes", "ed25519-dalek/serde"] +serde = ["serde_crate", "serde_bytes", "ed25519-dalek/serde", "x25519-dalek/serde"] asymmetric = ["x25519-dalek", "ed25519-dalek"] hashing = ["blake3", "digest", "hkdf", "hmac", "generic-array"] symmetric = ["aes", "ctr", "cipher", "generic-array"] diff --git a/common/crypto/src/asymmetric/encryption/mod.rs b/common/crypto/src/asymmetric/encryption/mod.rs index 5e3a4897e4..5c7b5398ad 100644 --- a/common/crypto/src/asymmetric/encryption/mod.rs +++ b/common/crypto/src/asymmetric/encryption/mod.rs @@ -4,6 +4,8 @@ use pemstore::traits::{PemStorableKey, PemStorableKeyPair}; #[cfg(feature = "rand")] use rand::{CryptoRng, RngCore}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::fmt::{self, Display, Formatter}; /// Size of a X25519 private key @@ -127,6 +129,28 @@ impl PublicKey { } } +#[cfg(feature = "serde")] +impl Serialize for PublicKey { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.0.serialize(serializer) + } +} + +#[cfg(feature = "serde")] +impl<'d> Deserialize<'d> for PublicKey { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'d>, + { + Ok(PublicKey(x25519_dalek::PublicKey::deserialize( + deserializer, + )?)) + } +} + impl PemStorableKey for PublicKey { type Error = KeyRecoveryError; @@ -143,7 +167,6 @@ impl PemStorableKey for PublicKey { } } -#[derive(Clone)] pub struct PrivateKey(x25519_dalek::StaticSecret); impl Display for PrivateKey { @@ -187,6 +210,28 @@ impl PrivateKey { } } +#[cfg(feature = "serde")] +impl Serialize for PrivateKey { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.0.serialize(serializer) + } +} + +#[cfg(feature = "serde")] +impl<'d> Deserialize<'d> for PrivateKey { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'d>, + { + Ok(PrivateKey(x25519_dalek::StaticSecret::deserialize( + deserializer, + )?)) + } +} + impl PemStorableKey for PrivateKey { type Error = KeyRecoveryError; diff --git a/common/crypto/src/asymmetric/identity/mod.rs b/common/crypto/src/asymmetric/identity/mod.rs index 39927e1325..8ba1de8ec6 100644 --- a/common/crypto/src/asymmetric/identity/mod.rs +++ b/common/crypto/src/asymmetric/identity/mod.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 pub use ed25519_dalek::ed25519::signature::Signature as SignatureTrait; -use ed25519_dalek::SecretKey; pub use ed25519_dalek::SignatureError; pub use ed25519_dalek::{Verifier, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, SIGNATURE_LENGTH}; use nymsphinx_types::{DestinationAddressBytes, DESTINATION_ADDRESS_LENGTH}; @@ -247,7 +246,9 @@ impl<'d> Deserialize<'d> for PrivateKey { where D: Deserializer<'d>, { - Ok(PrivateKey(SecretKey::deserialize(deserializer)?)) + Ok(PrivateKey(ed25519_dalek::SecretKey::deserialize( + deserializer, + )?)) } }