miden_objects/transaction/
tx_summary.rs

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