Skip to main content

miden_protocol/transaction/
tx_summary.rs

1use alloc::vec::Vec;
2
3use crate::account::AccountDelta;
4use crate::crypto::SequentialCommit;
5use crate::transaction::{InputNote, InputNotes, RawOutputNotes};
6use crate::utils::serde::{
7    ByteReader,
8    ByteWriter,
9    Deserializable,
10    DeserializationError,
11    Serializable,
12};
13use crate::{Felt, Word};
14
15/// The summary of the changes that result from executing a transaction.
16///
17/// These are the account delta and the consumed and created notes. Because this data is intended to
18/// be used for signing a transaction a user-defined salt is included as well.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct TransactionSummary {
21    account_delta: AccountDelta,
22    input_notes: InputNotes<InputNote>,
23    output_notes: RawOutputNotes,
24    salt: Word,
25}
26
27impl TransactionSummary {
28    // CONSTRUCTORS
29    // --------------------------------------------------------------------------------------------
30
31    /// Creates a new [`TransactionSummary`] from the provided parts.
32    pub fn new(
33        account_delta: AccountDelta,
34        input_notes: InputNotes<InputNote>,
35        output_notes: RawOutputNotes,
36        salt: Word,
37    ) -> Self {
38        Self {
39            account_delta,
40            input_notes,
41            output_notes,
42            salt,
43        }
44    }
45
46    // PUBLIC ACCESSORS
47    // --------------------------------------------------------------------------------------------
48
49    /// Returns the account delta of this transaction summary.
50    pub fn account_delta(&self) -> &AccountDelta {
51        &self.account_delta
52    }
53
54    /// Returns the input notes of this transaction summary.
55    pub fn input_notes(&self) -> &InputNotes<InputNote> {
56        &self.input_notes
57    }
58
59    /// Returns the output notes of this transaction summary.
60    pub fn output_notes(&self) -> &RawOutputNotes {
61        &self.output_notes
62    }
63
64    /// Returns the salt of this transaction summary.
65    pub fn salt(&self) -> Word {
66        self.salt
67    }
68
69    /// Computes the commitment to the [`TransactionSummary`].
70    ///
71    /// This can be used to sign the transaction.
72    pub fn to_commitment(&self) -> Word {
73        <Self as SequentialCommit>::to_commitment(self)
74    }
75}
76
77impl SequentialCommit for TransactionSummary {
78    type Commitment = Word;
79
80    fn to_elements(&self) -> Vec<Felt> {
81        let mut elements = Vec::with_capacity(16);
82        elements.extend_from_slice(self.account_delta.to_commitment().as_elements());
83        elements.extend_from_slice(self.input_notes.commitment().as_elements());
84        elements.extend_from_slice(self.output_notes.commitment().as_elements());
85        elements.extend_from_slice(self.salt.as_elements());
86        elements
87    }
88}
89
90impl Serializable for TransactionSummary {
91    fn write_into<W: ByteWriter>(&self, target: &mut W) {
92        self.account_delta.write_into(target);
93        self.input_notes.write_into(target);
94        self.output_notes.write_into(target);
95        self.salt.write_into(target);
96    }
97}
98
99impl Deserializable for TransactionSummary {
100    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
101        let account_delta = source.read()?;
102        let input_notes = source.read()?;
103        let output_notes = source.read()?;
104        let salt = source.read()?;
105
106        Ok(Self::new(account_delta, input_notes, output_notes, salt))
107    }
108}