serde for ReconstructedMessage

This commit is contained in:
durch
2024-06-07 11:44:18 +02:00
parent 70af84b6b1
commit 34e9822f1d
4 changed files with 46 additions and 5 deletions
Generated
+2
View File
@@ -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",
]
+15 -3
View File
@@ -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",
]
@@ -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]);
+27 -1
View File
@@ -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<u8>,
@@ -29,6 +31,30 @@ pub struct ReconstructedMessage {
pub sender_tag: Option<AnonymousSenderTag>,
}
impl From<ReconstructedMessage> for Vec<u8> {
fn from(msg: ReconstructedMessage) -> Vec<u8> {
match bincode::serialize(&msg) {
Ok(serialized) => serialized,
Err(err) => {
warn!("failed to serialize reconstructed message - {:?}", err);
Vec::new()
}
}
}
}
impl From<&ReconstructedMessage> for Vec<u8> {
fn from(msg: &ReconstructedMessage) -> Vec<u8> {
match bincode::serialize(msg) {
Ok(serialized) => serialized,
Err(err) => {
warn!("failed to serialize reconstructed message - {:?}", err);
Vec::new()
}
}
}
}
impl From<ReconstructedMessage> for (Vec<u8>, Option<AnonymousSenderTag>) {
fn from(msg: ReconstructedMessage) -> Self {
(msg.message, msg.sender_tag)