Skip to main content

miden_objects/conversion/
note.rs

1use miden_protocol::Word;
2use miden_protocol::note::{
3    Note,
4    NoteAttachment,
5    NoteAttachments,
6    NoteDetails,
7    NoteHeader,
8    NoteId,
9    NoteInclusionProof,
10    NoteMetadata,
11    NoteRecipient,
12    NoteScript,
13    NoteStorage,
14    NoteType,
15    PartialNote,
16    PartialNoteMetadata,
17};
18
19use crate::proto;
20
21#[cfg(test)]
22mod tests;
23
24// NOTE TYPE
25// ================================================================================================
26
27impl From<NoteType> for proto::note::NoteType {
28    fn from(note_type: NoteType) -> Self {
29        match note_type {
30            NoteType::Private => proto::note::NoteType::Private,
31            NoteType::Public => proto::note::NoteType::Public,
32        }
33    }
34}
35
36// NOTE METADATA
37// ================================================================================================
38
39impl From<NoteMetadata> for proto::note::NoteMetadata {
40    fn from(metadata: NoteMetadata) -> Self {
41        Self {
42            version: proto::note::NoteVersion::V1 as i32,
43            sender: Some(metadata.sender().into()),
44            note_type: proto::note::NoteType::from(metadata.note_type()) as i32,
45            tag: metadata.tag().as_u32(),
46            attachment_schemes: metadata
47                .attachment_headers()
48                .iter()
49                .map(|header| u32::from(header.scheme().map_or(0, |scheme| scheme.as_u16())))
50                .collect(),
51            attachments_commitment: Some(metadata.attachments_commitment().into()),
52        }
53    }
54}
55
56// NOTE ATTACHMENTS
57// ================================================================================================
58
59impl From<&NoteAttachment> for proto::note::NoteAttachment {
60    fn from(attachment: &NoteAttachment) -> Self {
61        Self {
62            scheme: u32::from(attachment.attachment_scheme().as_u16()),
63            words: attachment.content().as_words().iter().map(Into::into).collect(),
64        }
65    }
66}
67
68impl From<NoteAttachments> for proto::note::NoteAttachments {
69    fn from(attachments: NoteAttachments) -> Self {
70        Self::from(&attachments)
71    }
72}
73
74impl From<&NoteAttachments> for proto::note::NoteAttachments {
75    fn from(attachments: &NoteAttachments) -> Self {
76        Self {
77            attachments: attachments.iter().map(Into::into).collect(),
78        }
79    }
80}
81
82// NOTE DETAILS
83// ================================================================================================
84
85impl From<NoteStorage> for proto::note::NoteStorage {
86    fn from(storage: NoteStorage) -> Self {
87        Self::from(&storage)
88    }
89}
90
91impl From<&NoteStorage> for proto::note::NoteStorage {
92    fn from(storage: &NoteStorage) -> Self {
93        Self {
94            items: storage.items().iter().map(Into::into).collect(),
95        }
96    }
97}
98
99impl From<NoteRecipient> for proto::note::NoteRecipient {
100    fn from(recipient: NoteRecipient) -> Self {
101        Self::from(&recipient)
102    }
103}
104
105impl From<&NoteRecipient> for proto::note::NoteRecipient {
106    fn from(recipient: &NoteRecipient) -> Self {
107        Self {
108            serial_num: Some(recipient.serial_num().into()),
109            script: Some(recipient.script().into()),
110            storage: Some(recipient.storage().into()),
111        }
112    }
113}
114
115impl From<NoteDetails> for proto::note::NoteDetails {
116    fn from(details: NoteDetails) -> Self {
117        Self::from(&details)
118    }
119}
120
121impl From<&NoteDetails> for proto::note::NoteDetails {
122    fn from(details: &NoteDetails) -> Self {
123        Self {
124            assets: details.assets().iter().copied().map(Into::into).collect(),
125            recipient: Some(details.recipient().into()),
126        }
127    }
128}
129
130// NOTE
131// ================================================================================================
132
133impl From<Note> for proto::note::Note {
134    fn from(note: Note) -> Self {
135        let (assets, metadata, recipient, attachments) = note.into_parts();
136        Self {
137            metadata: Some(metadata.into_partial_metadata().into()),
138            note_details: Some(NoteDetails::new(assets, recipient).into()),
139            note_attachments: Some(attachments.into()),
140        }
141    }
142}
143
144// PARTIAL NOTE
145// ================================================================================================
146
147impl From<&PartialNote> for proto::note::PartialNote {
148    fn from(note: &PartialNote) -> Self {
149        Self {
150            metadata: Some((*note.partial_metadata()).into()),
151            recipient_digest: Some(note.recipient_digest().into()),
152            assets: note.assets().iter().map(Into::into).collect(),
153            attachments: Some(note.attachments().into()),
154        }
155    }
156}
157
158// NOTE ID
159// ================================================================================================
160
161impl From<Word> for proto::note::NoteId {
162    fn from(digest: Word) -> Self {
163        Self { id: Some(digest.into()) }
164    }
165}
166
167impl From<&NoteId> for proto::note::NoteId {
168    fn from(note_id: &NoteId) -> Self {
169        Self { id: Some(note_id.as_word().into()) }
170    }
171}
172
173impl From<(&NoteId, &NoteInclusionProof)> for proto::note::NoteInclusionProof {
174    fn from((note_id, proof): (&NoteId, &NoteInclusionProof)) -> Self {
175        Self {
176            note_id: Some(note_id.into()),
177            block_num: Some(proof.location().block_num().into()),
178            note_index_in_block: proof.location().block_note_tree_index().into(),
179            inclusion_path: Some(proof.note_path().clone().into()),
180        }
181    }
182}
183
184// NOTE HEADER
185// ================================================================================================
186
187impl From<NoteHeader> for proto::note::NoteHeader {
188    fn from(header: NoteHeader) -> Self {
189        Self {
190            details_commitment: Some(header.details_commitment().as_word().into()),
191            metadata: Some(header.into_metadata().into()),
192        }
193    }
194}
195
196// NOTE SCRIPT
197// ================================================================================================
198
199impl From<NoteScript> for proto::note::NoteScript {
200    fn from(script: NoteScript) -> Self {
201        Self::from(&script)
202    }
203}
204
205impl From<&NoteScript> for proto::note::NoteScript {
206    fn from(script: &NoteScript) -> Self {
207        Self {
208            entrypoint: script.entrypoint().into(),
209            mast: Some(script.mast().as_ref().into()),
210        }
211    }
212}
213
214impl From<PartialNoteMetadata> for proto::note::PartialNoteMetadata {
215    fn from(metadata: PartialNoteMetadata) -> Self {
216        Self {
217            version: proto::note::NoteVersion::V1 as i32,
218            sender: Some(metadata.sender().into()),
219            note_type: proto::note::NoteType::from(metadata.note_type()) as i32,
220            tag: metadata.tag().as_u32(),
221        }
222    }
223}