wasm_multisig/transaction.rs
1/// A struct representing a transaction with a nonce and associated data.
2/// The `Transaction` structure is used to manage operations with specific data and unique nonces.
3pub struct Transaction {
4 pub nonce: u64, // A unique identifier for the transaction to prevent replay attacks
5 pub data: Vec<u8>, // The transaction's associated data (with a limit on size)
6}
7
8impl Transaction {
9 /// Creates a new `Transaction` with the given data and nonce.
10 /// The data size is limited to 1024 bytes to ensure the transaction payload is reasonable.
11 ///
12 /// # Arguments
13 /// * `data` - A vector of bytes representing the transaction's data.
14 /// * `nonce` - A unique number (nonce) to differentiate this transaction from others.
15 ///
16 /// # Panics
17 /// Panics if the `data` length exceeds 1024 bytes.
18 pub fn new(data: Vec<u8>, nonce: u64) -> Self {
19 assert!(data.len() < 1024, "Data too large"); // Ensure the data size is within the limit
20 Transaction { data, nonce }
21 }
22
23 /// Validates whether the provided nonce matches the transaction's nonce.
24 ///
25 /// # Arguments
26 /// * `current_nonce` - The nonce to be compared with the transaction's nonce.
27 ///
28 /// # Returns
29 /// * `Ok(())` if the nonce is valid.
30 /// * `Err(&'static str)` if the nonce does not match, with an error message.
31 pub fn is_valid_nonce(&self, current_nonce: u64) -> Result<(), &'static str> {
32 if self.nonce != current_nonce {
33 Err("Invalid nonce") // Return an error if the nonces do not match
34 } else {
35 Ok(()) // Return Ok if the nonce matches
36 }
37 }
38}