diff --git a/Cargo.lock b/Cargo.lock index 9974f92fa7..f0277eca1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5359,6 +5359,7 @@ dependencies = [ name = "nym-sphinx" version = "0.1.0" dependencies = [ + "bincode", "log", "nym-crypto", "nym-mixnet-contract-common", @@ -5375,6 +5376,7 @@ dependencies = [ "nym-topology", "rand 0.8.5", "rand_distr", + "serde", "thiserror", "tokio", ] diff --git a/common/nymsphinx/Cargo.toml b/common/nymsphinx/Cargo.toml index 40769d2ec6..6c3e33614a 100644 --- a/common/nymsphinx/Cargo.toml +++ b/common/nymsphinx/Cargo.toml @@ -12,6 +12,8 @@ log = { workspace = true } rand = { workspace = true } rand_distr = { workspace = true } thiserror = { workspace = true } +serde = { workspace = true, features = ["derive"] } +bincode = { workspace = true } nym-sphinx-acknowledgements = { path = "acknowledgements" } nym-sphinx-addressing = { path = "addressing" } @@ -30,7 +32,9 @@ nym-topology = { path = "../topology" } [dev-dependencies] nym-mixnet-contract-common = { path = "../cosmwasm-smart-contracts/mixnet-contract" } -nym-crypto = { path = "../crypto", version = "0.4.0", features = ["asymmetric"] } +nym-crypto = { path = "../crypto", version = "0.4.0", features = [ + "asymmetric", +] } # do not include this when compiling into wasm as it somehow when combined together with reqwest, it will require # net2 via tokio-util -> tokio -> mio -> net2 @@ -43,5 +47,13 @@ features = ["sync"] [features] default = ["sphinx"] -sphinx = ["nym-crypto/sphinx", "nym-sphinx-params/sphinx", "nym-sphinx-types/sphinx"] -outfox = ["nym-crypto/outfox", "nym-sphinx-params/outfox", "nym-sphinx-types/outfox"] +sphinx = [ + "nym-crypto/sphinx", + "nym-sphinx-params/sphinx", + "nym-sphinx-types/sphinx", +] +outfox = [ + "nym-crypto/outfox", + "nym-sphinx-params/outfox", + "nym-sphinx-types/outfox", +] diff --git a/common/nymsphinx/anonymous-replies/src/requests.rs b/common/nymsphinx/anonymous-replies/src/requests.rs index 9dd4c84dc0..1069543926 100644 --- a/common/nymsphinx/anonymous-replies/src/requests.rs +++ b/common/nymsphinx/anonymous-replies/src/requests.rs @@ -4,6 +4,7 @@ use crate::{ReplySurb, ReplySurbError}; use nym_sphinx_addressing::clients::{Recipient, RecipientFormattingError}; use rand::{CryptoRng, RngCore}; +use serde::{Deserialize, Serialize}; use std::fmt::{Display, Formatter}; use std::mem; use thiserror::Error; @@ -24,7 +25,7 @@ pub enum InvalidAnonymousSenderTagRepresentation { InvalidLength { received: usize, expected: usize }, } -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen)] pub struct AnonymousSenderTag([u8; SENDER_TAG_SIZE]); diff --git a/common/nymsphinx/src/receiver.rs b/common/nymsphinx/src/receiver.rs index ac1857fd82..7bb7a3a64f 100644 --- a/common/nymsphinx/src/receiver.rs +++ b/common/nymsphinx/src/receiver.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use crate::message::{NymMessage, NymMessageError, PaddedMessage, PlainMessage}; +use log::warn; use nym_crypto::aes::cipher::{KeyIvInit, StreamCipher}; use nym_crypto::asymmetric::encryption; use nym_crypto::shared_key::recompute_shared_key; @@ -16,10 +17,11 @@ use nym_sphinx_params::{ PacketEncryptionAlgorithm, PacketHkdfAlgorithm, ReplySurbEncryptionAlgorithm, DEFAULT_NUM_MIX_HOPS, }; +use serde::{Deserialize, Serialize}; use thiserror::Error; // TODO: should this live in this file? -#[derive(Debug)] +#[derive(Debug, Serialize, Deserialize)] pub struct ReconstructedMessage { /// The actual plaintext message that was received. pub message: Vec, @@ -29,6 +31,30 @@ pub struct ReconstructedMessage { pub sender_tag: Option, } +impl From for Vec { + fn from(msg: ReconstructedMessage) -> Vec { + match bincode::serialize(&msg) { + Ok(serialized) => serialized, + Err(err) => { + warn!("failed to serialize reconstructed message - {:?}", err); + Vec::new() + } + } + } +} + +impl From<&ReconstructedMessage> for Vec { + fn from(msg: &ReconstructedMessage) -> Vec { + match bincode::serialize(msg) { + Ok(serialized) => serialized, + Err(err) => { + warn!("failed to serialize reconstructed message - {:?}", err); + Vec::new() + } + } + } +} + impl From for (Vec, Option) { fn from(msg: ReconstructedMessage) -> Self { (msg.message, msg.sender_tag)