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        Self::from(&note)
136    }
137}
138
139impl From<&Note> for proto::note::Note {
140    fn from(note: &Note) -> Self {
141        Self {
142            metadata: Some((*note.metadata().partial_metadata()).into()),
143            note_details: Some(note.details().into()),
144            note_attachments: Some(note.attachments().into()),
145        }
146    }
147}
148
149// PARTIAL NOTE
150// ================================================================================================
151
152impl From<&PartialNote> for proto::note::PartialNote {
153    fn from(note: &PartialNote) -> Self {
154        Self {
155            metadata: Some((*note.partial_metadata()).into()),
156            recipient_digest: Some(note.recipient_digest().into()),
157            assets: note.assets().iter().map(Into::into).collect(),
158            attachments: Some(note.attachments().into()),
159        }
160    }
161}
162
163// NOTE ID
164// ================================================================================================
165
166impl From<Word> for proto::note::NoteId {
167    fn from(digest: Word) -> Self {
168        Self { id: Some(digest.into()) }
169    }
170}
171
172impl From<&NoteId> for proto::note::NoteId {
173    fn from(note_id: &NoteId) -> Self {
174        Self { id: Some(note_id.as_word().into()) }
175    }
176}
177
178impl From<(&NoteId, &NoteInclusionProof)> for proto::note::NoteInclusionProof {
179    fn from((note_id, proof): (&NoteId, &NoteInclusionProof)) -> Self {
180        Self {
181            note_id: Some(note_id.into()),
182            block_num: Some(proof.location().block_num().into()),
183            note_index_in_block: proof.location().block_note_tree_index().into(),
184            inclusion_path: Some(proof.note_path().clone().into()),
185        }
186    }
187}
188
189// NOTE HEADER
190// ================================================================================================
191
192impl From<NoteHeader> for proto::note::NoteHeader {
193    fn from(header: NoteHeader) -> Self {
194        Self {
195            details_commitment: Some(header.details_commitment().as_word().into()),
196            metadata: Some(header.into_metadata().into()),
197        }
198    }
199}
200
201// NOTE SCRIPT
202// ================================================================================================
203
204impl From<NoteScript> for proto::note::NoteScript {
205    fn from(script: NoteScript) -> Self {
206        Self::from(&script)
207    }
208}
209
210impl From<&NoteScript> for proto::note::NoteScript {
211    fn from(script: &NoteScript) -> Self {
212        Self {
213            entrypoint: script.entrypoint().into(),
214            mast: Some(script.mast().as_ref().into()),
215        }
216    }
217}
218
219impl From<PartialNoteMetadata> for proto::note::PartialNoteMetadata {
220    fn from(metadata: PartialNoteMetadata) -> Self {
221        Self {
222            version: proto::note::NoteVersion::V1 as i32,
223            sender: Some(metadata.sender().into()),
224            note_type: proto::note::NoteType::from(metadata.note_type()) as i32,
225            tag: metadata.tag().as_u32(),
226        }
227    }
228}