diff --git a/nssa/src/public_transaction/encoding.rs b/nssa/src/public_transaction/encoding.rs index 7f1ab62..3c004a0 100644 --- a/nssa/src/public_transaction/encoding.rs +++ b/nssa/src/public_transaction/encoding.rs @@ -92,9 +92,9 @@ impl Message { impl WitnessSet { pub(crate) fn to_bytes(&self) -> Vec { let mut bytes = Vec::new(); - let size = self.signatures_and_public_keys.len() as u32; + let size = self.signatures_and_public_keys().len() as u32; bytes.extend_from_slice(&size.to_le_bytes()); - for (signature, public_key) in &self.signatures_and_public_keys { + for (signature, public_key) in self.signatures_and_public_keys() { bytes.extend_from_slice(signature.to_bytes()); bytes.extend_from_slice(public_key.to_bytes()); } diff --git a/nssa/src/public_transaction/transaction.rs b/nssa/src/public_transaction/transaction.rs index 9315ba8..bd16644 100644 --- a/nssa/src/public_transaction/transaction.rs +++ b/nssa/src/public_transaction/transaction.rs @@ -37,7 +37,8 @@ impl PublicTransaction { pub(crate) fn signer_addresses(&self) -> Vec
{ self.witness_set - .iter_signatures() + .signatures_and_public_keys() + .iter() .map(|(_, public_key)| Address::from_public_key(public_key)) .collect() } @@ -115,7 +116,7 @@ impl PublicTransaction { } #[cfg(test)] -mod tests { +pub mod tests { use sha2::{Digest, digest::FixedOutput}; use crate::{ diff --git a/nssa/src/public_transaction/witness_set.rs b/nssa/src/public_transaction/witness_set.rs index 064887a..ae87080 100644 --- a/nssa/src/public_transaction/witness_set.rs +++ b/nssa/src/public_transaction/witness_set.rs @@ -2,7 +2,7 @@ use crate::{PrivateKey, PublicKey, Signature, public_transaction::Message}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct WitnessSet { - pub(crate) signatures_and_public_keys: Vec<(Signature, PublicKey)>, + pub(super) signatures_and_public_keys: Vec<(Signature, PublicKey)>, } impl WitnessSet { @@ -24,7 +24,7 @@ impl WitnessSet { pub fn is_valid_for(&self, message: &Message) -> bool { let message_bytes = message.to_bytes(); - for (signature, public_key) in self.iter_signatures() { + for (signature, public_key) in self.signatures_and_public_keys() { if !signature.is_valid_for(&message_bytes, public_key) { return false; } @@ -32,7 +32,41 @@ impl WitnessSet { true } - pub fn iter_signatures(&self) -> impl Iterator { - self.signatures_and_public_keys.iter() + pub fn signatures_and_public_keys(&self) -> &[(Signature, PublicKey)] { + &self.signatures_and_public_keys + } +} + +#[cfg(test)] +mod tests { + use crate::Address; + + use super::*; + + #[test] + fn test_for_message_constructor() { + let key1 = PrivateKey::try_new([1; 32]).unwrap(); + let key2 = PrivateKey::try_new([2; 32]).unwrap(); + let pubkey1 = PublicKey::new_from_private_key(&key1); + let pubkey2 = PublicKey::new_from_private_key(&key2); + let addr1 = Address::from_public_key(&pubkey1); + let addr2 = Address::from_public_key(&pubkey2); + let nonces = vec![1, 2]; + let instruction = vec![1, 2, 3, 4]; + let message = Message::try_new([0; 8], vec![addr1, addr2], nonces, instruction).unwrap(); + + let witness_set = WitnessSet::for_message(&message, &[&key1, &key2]); + + assert_eq!(witness_set.signatures_and_public_keys.len(), 2); + + let message_bytes = message.to_bytes(); + for ((signature, public_key), expected_public_key) in witness_set + .signatures_and_public_keys + .into_iter() + .zip([pubkey1, pubkey2]) + { + assert_eq!(public_key, expected_public_key); + assert!(signature.is_valid_for(&message_bytes, &expected_public_key)); + } } } diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index 0f66b4a..b56b34cc 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -232,9 +232,15 @@ mod tests { #[test] fn test_start_different_intial_accounts_balances() { - let acc1_addr = vec![27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143]; + let acc1_addr = vec![ + 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, + 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143, + ]; - let acc2_addr = vec![77, 75, 108, 209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, 216, 10, 201, 66, 51, 116, 196, 81, 167, 37, 77, 7, 102]; + let acc2_addr = vec![ + 77, 75, 108, 209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, + 216, 10, 201, 66, 51, 116, 196, 81, 167, 37, 77, 7, 102, + ]; let initial_acc1 = AccountInitialData { addr: hex::encode(acc1_addr),