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