add message serialization roundtrip test

This commit is contained in:
Sergio Chouhy 2025-08-18 14:55:50 -03:00
parent a185e52203
commit 330d79379f
3 changed files with 38 additions and 18 deletions

View File

@ -47,7 +47,7 @@ impl Tag {
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct EncryptedAccountData;
pub struct EncryptedAccountData(u8);
impl EncryptedAccountData {
pub fn new(
@ -57,12 +57,13 @@ impl EncryptedAccountData {
Ivk: &IncomingViewingPublicKey,
) -> Self {
// TODO: implement
Self
Self(0)
}
#[cfg(feature = "host")]
pub fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Self, NssaCoreError> {
todo!()
let dummy_value = EncryptedAccountData(0);
Ok(dummy_value)
}
}

View File

@ -39,18 +39,21 @@ impl Message {
// Encrypted post states
let encrypted_accounts_post_states_len: u32 =
self.encrypted_private_post_states.len() as u32;
bytes.extend_from_slice(&encrypted_accounts_post_states_len.to_le_bytes());
for encrypted_account in &self.encrypted_private_post_states {
bytes.extend_from_slice(&encrypted_account.to_bytes());
}
// New commitments
let new_commitments_len: u32 = self.new_commitments.len() as u32;
bytes.extend_from_slice(&new_commitments_len.to_le_bytes());
for commitment in &self.new_commitments {
bytes.extend_from_slice(&commitment.to_byte_array());
}
// New nullifiers
let new_nullifiers_len: u32 = self.new_nullifiers.len() as u32;
bytes.extend_from_slice(&new_nullifiers_len.to_le_bytes());
for nullifier in &self.new_nullifiers {
bytes.extend_from_slice(&nullifier.to_byte_array());
}
@ -58,11 +61,6 @@ impl Message {
}
pub(crate) fn from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Self, NssaError> {
let prefix = {
let mut this = [0u8; MESSAGE_ENCODING_PREFIX_LEN];
cursor.read_exact(&mut this)?;
this
};
let prefix = {
let mut this = [0u8; MESSAGE_ENCODING_PREFIX_LEN];
cursor.read_exact(&mut this)?;
@ -138,3 +136,6 @@ impl Message {
})
}
}
#[cfg(test)]
mod tests {}

View File

@ -37,14 +37,15 @@ impl Message {
#[cfg(test)]
pub mod tests {
use std::io::Cursor;
use nssa_core::account::{
Account, Commitment, Nullifier, NullifierPublicKey, NullifierSecretKey,
};
use crate::{Address, privacy_preserving_transaction::message::Message};
#[test]
fn test_constructor() {
fn message_for_tests() -> Message {
let account1 = Account::default();
let account2 = Account::default();
@ -67,24 +68,41 @@ pub mod tests {
let old_commitment = Commitment::new(&Npk1, &account1);
let new_nullifiers = vec![Nullifier::new(&old_commitment, &nsk1)];
let expected_message = Message {
Message {
public_addresses: public_addresses.clone(),
nonces: nonces.clone(),
public_post_states: public_post_states.clone(),
encrypted_private_post_states: encrypted_private_post_states.clone(),
new_commitments: new_commitments.clone(),
new_nullifiers: new_nullifiers.clone(),
};
}
}
#[test]
fn test_constructor() {
let message = message_for_tests();
let expected_message = message.clone();
let message = Message::new(
public_addresses,
nonces,
public_post_states,
encrypted_private_post_states,
new_commitments,
new_nullifiers,
message.public_addresses,
message.nonces,
message.public_post_states,
message.encrypted_private_post_states,
message.new_commitments,
message.new_nullifiers,
);
assert_eq!(message, expected_message);
}
#[test]
fn test_message_serialization_roundtrip() {
let message = message_for_tests();
let bytes = message.to_bytes();
let mut cursor = Cursor::new(bytes.as_ref());
let message_from_cursor = Message::from_cursor(&mut cursor).unwrap();
assert_eq!(message, message_from_cursor);
}
}