miden_objects/note/
mod.rs1use miden_crypto::Word;
2use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
3use miden_processor::DeserializationError;
4
5use crate::account::AccountId;
6use crate::{Felt, Hasher, NoteError, WORD_SIZE, ZERO};
7
8mod assets;
9pub use assets::NoteAssets;
10
11mod details;
12pub use details::NoteDetails;
13
14mod header;
15pub use header::{NoteHeader, compute_note_commitment};
16
17mod inputs;
18pub use inputs::NoteInputs;
19
20mod metadata;
21pub use metadata::NoteMetadata;
22
23mod execution_hint;
24pub use execution_hint::{AfterBlockNumber, NoteExecutionHint};
25
26mod note_id;
27pub use note_id::NoteId;
28
29mod note_tag;
30pub use note_tag::{NoteExecutionMode, NoteTag};
31
32mod note_type;
33pub use note_type::NoteType;
34
35mod nullifier;
36pub use nullifier::Nullifier;
37
38mod location;
39pub use location::{NoteInclusionProof, NoteLocation};
40
41mod partial;
42pub use partial::PartialNote;
43
44mod recipient;
45pub use recipient::NoteRecipient;
46
47mod script;
48pub use script::NoteScript;
49
50mod file;
51pub use file::NoteFile;
52
53#[derive(Clone, Debug, PartialEq, Eq)]
78pub struct Note {
79    header: NoteHeader,
80    details: NoteDetails,
81
82    nullifier: Nullifier,
83}
84
85impl Note {
86    pub fn new(assets: NoteAssets, metadata: NoteMetadata, recipient: NoteRecipient) -> Self {
91        let details = NoteDetails::new(assets, recipient);
92        let header = NoteHeader::new(details.id(), metadata);
93        let nullifier = details.nullifier();
94
95        Self { header, details, nullifier }
96    }
97
98    pub fn header(&self) -> &NoteHeader {
103        &self.header
104    }
105
106    pub fn id(&self) -> NoteId {
110        self.header.id()
111    }
112
113    pub fn metadata(&self) -> &NoteMetadata {
115        self.header.metadata()
116    }
117
118    pub fn assets(&self) -> &NoteAssets {
120        self.details.assets()
121    }
122
123    pub fn serial_num(&self) -> Word {
125        self.details.serial_num()
126    }
127
128    pub fn script(&self) -> &NoteScript {
130        self.details.script()
131    }
132
133    pub fn inputs(&self) -> &NoteInputs {
135        self.details.inputs()
136    }
137
138    pub fn recipient(&self) -> &NoteRecipient {
140        self.details.recipient()
141    }
142
143    pub fn nullifier(&self) -> Nullifier {
147        self.nullifier
148    }
149
150    pub fn commitment(&self) -> Word {
157        self.header.commitment()
158    }
159
160    pub fn is_network_note(&self) -> bool {
162        self.metadata().tag().execution_mode() == NoteExecutionMode::Network
163    }
164}
165
166impl AsRef<NoteRecipient> for Note {
170    fn as_ref(&self) -> &NoteRecipient {
171        self.recipient()
172    }
173}
174
175impl From<&Note> for NoteHeader {
179    fn from(note: &Note) -> Self {
180        note.header
181    }
182}
183
184impl From<Note> for NoteHeader {
185    fn from(note: Note) -> Self {
186        note.header
187    }
188}
189
190impl From<&Note> for NoteDetails {
191    fn from(note: &Note) -> Self {
192        note.details.clone()
193    }
194}
195
196impl From<Note> for NoteDetails {
197    fn from(note: Note) -> Self {
198        note.details
199    }
200}
201
202impl From<Note> for PartialNote {
203    fn from(note: Note) -> Self {
204        let (assets, recipient, ..) = note.details.into_parts();
205        PartialNote::new(*note.header.metadata(), recipient.digest(), assets)
206    }
207}
208
209impl From<&Note> for PartialNote {
210    fn from(note: &Note) -> Self {
211        PartialNote::new(
212            *note.header.metadata(),
213            note.details.recipient().digest(),
214            note.details.assets().clone(),
215        )
216    }
217}
218
219impl Serializable for Note {
223    fn write_into<W: ByteWriter>(&self, target: &mut W) {
224        let Self {
225            header,
226            details,
227
228            nullifier: _,
230        } = self;
231
232        header.metadata().write_into(target);
234        details.write_into(target);
235    }
236}
237
238impl Deserializable for Note {
239    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
240        let metadata = NoteMetadata::read_from(source)?;
241        let details = NoteDetails::read_from(source)?;
242        let (assets, recipient) = details.into_parts();
243
244        Ok(Self::new(assets, metadata, recipient))
245    }
246}