miden_protocol/note/
mod.rs1use miden_crypto::Word;
2
3use crate::account::AccountId;
4use crate::errors::NoteError;
5use crate::utils::serde::{
6 ByteReader,
7 ByteWriter,
8 Deserializable,
9 DeserializationError,
10 Serializable,
11};
12use crate::{Felt, Hasher, ZERO};
13
14mod assets;
15pub use assets::NoteAssets;
16
17mod details;
18pub use details::NoteDetails;
19
20mod header;
21pub use header::NoteHeader;
22
23mod storage;
24pub use storage::NoteStorage;
25
26mod metadata;
27pub use metadata::{NoteMetadata, PartialNoteMetadata};
28
29mod attachment;
30pub use attachment::{
31 NoteAttachment,
32 NoteAttachmentContent,
33 NoteAttachmentHeader,
34 NoteAttachmentScheme,
35 NoteAttachments,
36};
37
38mod note_id;
39pub use note_id::NoteId;
40
41mod note_details_commitment;
42pub use note_details_commitment::NoteDetailsCommitment;
43
44mod note_tag;
45pub use note_tag::NoteTag;
46
47mod note_type;
48pub use note_type::NoteType;
49
50mod nullifier;
51pub use nullifier::Nullifier;
52
53mod location;
54pub use location::{NoteInclusionProof, NoteLocation};
55
56mod partial;
57pub use partial::PartialNote;
58
59mod recipient;
60pub use recipient::NoteRecipient;
61
62mod script;
63pub use script::{NoteScript, NoteScriptRoot};
64
65#[derive(Clone, Debug, PartialEq, Eq)]
91pub struct Note {
92 header: NoteHeader,
93 details: NoteDetails,
94 attachments: NoteAttachments,
95
96 nullifier: Nullifier,
97}
98
99impl Note {
100 pub fn new(
105 assets: NoteAssets,
106 partial_metadata: PartialNoteMetadata,
107 recipient: NoteRecipient,
108 ) -> Self {
109 Self::with_attachments(assets, partial_metadata, recipient, NoteAttachments::default())
110 }
111
112 pub fn with_attachments(
114 assets: NoteAssets,
115 partial_metadata: PartialNoteMetadata,
116 recipient: NoteRecipient,
117 attachments: NoteAttachments,
118 ) -> Self {
119 let details = NoteDetails::new(assets, recipient);
120 let metadata = NoteMetadata::new(partial_metadata, &attachments);
121 let header = NoteHeader::new(details.commitment(), metadata);
122 let nullifier = Nullifier::from_details_and_metadata(&details, &metadata);
123
124 Self { header, details, attachments, nullifier }
125 }
126
127 pub fn header(&self) -> &NoteHeader {
132 &self.header
133 }
134
135 pub fn id(&self) -> NoteId {
139 self.header.id()
140 }
141
142 pub fn details_commitment(&self) -> NoteDetailsCommitment {
144 self.header.details_commitment()
145 }
146
147 pub fn assets(&self) -> &NoteAssets {
149 self.details.assets()
150 }
151
152 pub fn serial_num(&self) -> Word {
154 self.details.serial_num()
155 }
156
157 pub fn script(&self) -> &NoteScript {
159 self.details.script()
160 }
161
162 pub fn storage(&self) -> &NoteStorage {
164 self.details.storage()
165 }
166
167 pub fn recipient(&self) -> &NoteRecipient {
169 self.details.recipient()
170 }
171
172 pub fn nullifier(&self) -> Nullifier {
176 self.nullifier
177 }
178
179 pub fn attachments(&self) -> &NoteAttachments {
181 &self.attachments
182 }
183
184 pub fn has_attachments(&self) -> bool {
186 !self.attachments.is_empty()
187 }
188
189 pub fn metadata(&self) -> &NoteMetadata {
191 self.header.metadata()
192 }
193
194 pub fn clear_debug_info(&mut self) {
199 self.details.clear_debug_info();
200 }
201
202 pub fn into_parts(self) -> (NoteAssets, NoteMetadata, NoteRecipient, NoteAttachments) {
204 let (assets, recipient) = self.details.into_parts();
205 let metadata = self.header.into_metadata();
206 (assets, metadata, recipient, self.attachments)
207 }
208}
209
210impl AsRef<NoteRecipient> for Note {
214 fn as_ref(&self) -> &NoteRecipient {
215 self.recipient()
216 }
217}
218
219impl From<Note> for NoteHeader {
223 fn from(note: Note) -> Self {
224 note.header
225 }
226}
227
228impl From<&Note> for NoteDetails {
229 fn from(note: &Note) -> Self {
230 note.details.clone()
231 }
232}
233
234impl From<Note> for NoteDetails {
235 fn from(note: Note) -> Self {
236 note.details
237 }
238}
239
240impl From<Note> for PartialNote {
241 fn from(note: Note) -> Self {
242 let (assets, recipient, ..) = note.details.into_parts();
243 PartialNote::new(
244 note.header.into_metadata().into_partial_metadata(),
245 recipient.digest(),
246 assets,
247 note.attachments,
248 )
249 }
250}
251
252impl From<&Note> for NoteHeader {
253 fn from(note: &Note) -> Self {
254 note.header
255 }
256}
257
258impl Serializable for Note {
262 fn write_into<W: ByteWriter>(&self, target: &mut W) {
263 let Self {
264 header,
265 details,
266 attachments,
267
268 nullifier: _,
270 } = self;
271
272 header.metadata().partial_metadata().write_into(target);
275 details.write_into(target);
276 attachments.write_into(target);
277 }
278
279 fn get_size_hint(&self) -> usize {
280 self.header.metadata().partial_metadata().get_size_hint()
281 + self.details.get_size_hint()
282 + self.attachments.get_size_hint()
283 }
284}
285
286impl Deserializable for Note {
287 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
288 let partial_metadata = PartialNoteMetadata::read_from(source)?;
289 let details = NoteDetails::read_from(source)?;
290 let attachments = NoteAttachments::read_from(source)?;
291 let (assets, recipient) = details.into_parts();
292
293 Ok(Self::with_attachments(assets, partial_metadata, recipient, attachments))
294 }
295}