miden_objects/note/
mod.rs1use core::ops::Deref;
2
3use miden_crypto::{
4 utils::{ByteReader, ByteWriter, Deserializable, Serializable},
5 Word,
6};
7use vm_processor::DeserializationError;
8
9use crate::{account::AccountId, asset::Asset, Digest, Felt, Hasher, NoteError, WORD_SIZE, ZERO};
10
11mod assets;
12pub use assets::NoteAssets;
13
14mod details;
15pub use details::NoteDetails;
16
17mod header;
18pub use header::{compute_note_hash, NoteHeader};
19
20mod inputs;
21pub use inputs::NoteInputs;
22
23mod metadata;
24pub use metadata::NoteMetadata;
25
26mod execution_hint;
27pub use execution_hint::{AfterBlockNumber, NoteExecutionHint};
28
29mod note_id;
30pub use note_id::NoteId;
31
32mod note_tag;
33pub use note_tag::{NoteExecutionMode, NoteTag};
34
35mod note_type;
36pub use note_type::NoteType;
37
38mod nullifier;
39pub use nullifier::Nullifier;
40
41mod location;
42pub use location::{NoteInclusionProof, NoteLocation};
43
44mod partial;
45pub use partial::PartialNote;
46
47mod recipient;
48pub use recipient::NoteRecipient;
49
50mod script;
51pub use script::NoteScript;
52
53mod file;
54pub use file::NoteFile;
55
56#[derive(Clone, Debug, PartialEq, Eq)]
81pub struct Note {
82 header: NoteHeader,
83 details: NoteDetails,
84
85 nullifier: Nullifier,
86}
87
88impl Note {
89 pub fn new(assets: NoteAssets, metadata: NoteMetadata, recipient: NoteRecipient) -> Self {
94 let details = NoteDetails::new(assets, recipient);
95 let header = NoteHeader::new(details.id(), metadata);
96 let nullifier = details.nullifier();
97
98 Self { header, details, nullifier }
99 }
100
101 pub fn header(&self) -> &NoteHeader {
106 &self.header
107 }
108
109 pub fn id(&self) -> NoteId {
113 self.header.id()
114 }
115
116 pub fn metadata(&self) -> &NoteMetadata {
118 self.header.metadata()
119 }
120
121 pub fn assets(&self) -> &NoteAssets {
123 self.details.assets()
124 }
125
126 pub fn serial_num(&self) -> Word {
128 self.details.serial_num()
129 }
130
131 pub fn script(&self) -> &NoteScript {
133 self.details.script()
134 }
135
136 pub fn inputs(&self) -> &NoteInputs {
138 self.details.inputs()
139 }
140
141 pub fn recipient(&self) -> &NoteRecipient {
143 self.details.recipient()
144 }
145
146 pub fn nullifier(&self) -> Nullifier {
150 self.nullifier
151 }
152
153 pub fn hash(&self) -> Digest {
160 self.header.hash()
161 }
162}
163
164impl Deref for Note {
168 type Target = NoteDetails;
169
170 fn deref(&self) -> &Self::Target {
171 &self.details
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}