Skip to main content

miden_objects/conversion/
transaction.rs

1use miden_protocol::transaction::{
2    InputNoteCommitment,
3    OutputNote,
4    PrivateOutputNote,
5    ProvenTransaction,
6    PublicOutputNote,
7    TransactionArgs,
8    TransactionHeader,
9    TransactionId,
10    TransactionScript,
11    TxAccountUpdate,
12};
13
14use crate::proto;
15
16#[cfg(test)]
17mod tests;
18
19// TRANSACTION ARGUMENTS
20// ================================================================================================
21
22impl From<&TransactionScript> for proto::transaction::TransactionScript {
23    fn from(value: &TransactionScript) -> Self {
24        Self {
25            entrypoint: value.entrypoint().into(),
26            mast: Some(value.mast().as_ref().into()),
27        }
28    }
29}
30
31impl From<&TransactionArgs> for proto::transaction::TransactionArgs {
32    fn from(value: &TransactionArgs) -> Self {
33        Self {
34            tx_script: value.tx_script().map(Into::into),
35            tx_script_args: Some(value.tx_script_args().into()),
36            note_args: value
37                .note_args()
38                .iter()
39                .map(|(note_id, args)| proto::transaction::NoteArgument {
40                    note_id: Some(note_id.into()),
41                    args: Some(args.into()),
42                })
43                .collect(),
44            advice_inputs: Some(value.advice_inputs().into()),
45            auth_args: Some(value.auth_args().into()),
46        }
47    }
48}
49
50impl From<TransactionArgs> for proto::transaction::TransactionArgs {
51    fn from(value: TransactionArgs) -> Self {
52        (&value).into()
53    }
54}
55
56// TX ACCOUNT UPDATE
57// ================================================================================================
58
59impl From<&TxAccountUpdate> for proto::transaction::TxAccountUpdate {
60    fn from(value: &TxAccountUpdate) -> Self {
61        Self {
62            account_id: Some(value.account_id().into()),
63            initial_state_commitment: Some(value.initial_state_commitment().into()),
64            final_state_commitment: Some(value.final_state_commitment().into()),
65            account_patch_commitment: Some(value.account_patch_commitment().into()),
66            details: Some(value.details().into()),
67        }
68    }
69}
70
71// PROVEN TRANSACTION
72// ================================================================================================
73
74impl From<&ProvenTransaction> for proto::transaction::ProvenTransaction {
75    fn from(value: &ProvenTransaction) -> Self {
76        Self {
77            account_update: Some(value.account_update().into()),
78            input_notes: value.input_notes().iter().map(Into::into).collect(),
79            output_notes: value.output_notes().iter().map(Into::into).collect(),
80            reference_block_num: Some(value.ref_block_num().into()),
81            reference_block_commitment: Some(value.ref_block_commitment().into()),
82            expiration_block_num: Some(value.expiration_block_num().into()),
83            proof: Some(value.proof().into()),
84        }
85    }
86}
87
88impl From<ProvenTransaction> for proto::transaction::ProvenTransaction {
89    fn from(value: ProvenTransaction) -> Self {
90        Self::from(&value)
91    }
92}
93
94// FROM TRANSACTION ID
95// ================================================================================================
96
97impl From<&TransactionId> for proto::transaction::TransactionId {
98    fn from(value: &TransactionId) -> Self {
99        proto::transaction::TransactionId { id: Some(value.as_word().into()) }
100    }
101}
102
103impl From<TransactionId> for proto::transaction::TransactionId {
104    fn from(value: TransactionId) -> Self {
105        (&value).into()
106    }
107}
108
109// INTO TRANSACTION ID
110// ================================================================================================
111
112// INPUT NOTE COMMITMENT
113// ================================================================================================
114
115impl From<InputNoteCommitment> for proto::transaction::InputNoteCommitment {
116    fn from(value: InputNoteCommitment) -> Self {
117        Self::from(&value)
118    }
119}
120
121impl From<&InputNoteCommitment> for proto::transaction::InputNoteCommitment {
122    fn from(value: &InputNoteCommitment) -> Self {
123        Self {
124            nullifier: Some(value.nullifier().as_word().into()),
125            header: value.header().copied().map(Into::into),
126        }
127    }
128}
129
130// TRANSACTION HEADER
131// ================================================================================================
132
133impl From<&TransactionHeader> for proto::transaction::TransactionHeader {
134    fn from(header: &TransactionHeader) -> Self {
135        Self {
136            transaction_id: Some(header.id().into()),
137            account_id: Some(header.account_id().into()),
138            initial_state_commitment: Some(header.initial_state_commitment().into()),
139            final_state_commitment: Some(header.final_state_commitment().into()),
140            input_notes: header.input_notes().iter().map(Into::into).collect(),
141            output_notes: header.output_notes().iter().copied().map(Into::into).collect(),
142        }
143    }
144}
145
146impl From<TransactionHeader> for proto::transaction::TransactionHeader {
147    fn from(header: TransactionHeader) -> Self {
148        Self::from(&header)
149    }
150}
151
152// OUTPUT NOTES
153// ================================================================================================
154
155impl From<&PublicOutputNote> for proto::transaction::PublicOutputNote {
156    fn from(note: &PublicOutputNote) -> Self {
157        Self {
158            note: Some(note.as_note().clone().into()),
159        }
160    }
161}
162
163impl From<PublicOutputNote> for proto::transaction::PublicOutputNote {
164    fn from(note: PublicOutputNote) -> Self {
165        Self::from(&note)
166    }
167}
168
169impl From<&PrivateOutputNote> for proto::transaction::PrivateOutputNote {
170    fn from(note: &PrivateOutputNote) -> Self {
171        Self {
172            header: Some((*note.header()).into()),
173            attachments: Some(note.attachments().into()),
174        }
175    }
176}
177
178impl From<PrivateOutputNote> for proto::transaction::PrivateOutputNote {
179    fn from(note: PrivateOutputNote) -> Self {
180        Self::from(&note)
181    }
182}
183
184impl From<&OutputNote> for proto::transaction::OutputNote {
185    fn from(note: &OutputNote) -> Self {
186        use proto::transaction::output_note::Note;
187
188        let note = match note {
189            OutputNote::Public(note) => Note::Public(note.into()),
190            OutputNote::Private(note) => Note::Private(note.into()),
191        };
192        Self { note: Some(note) }
193    }
194}
195
196impl From<OutputNote> for proto::transaction::OutputNote {
197    fn from(note: OutputNote) -> Self {
198        Self::from(&note)
199    }
200}