miden_node_proto/domain/
note.rs

1use miden_objects::{
2    Digest, Felt,
3    note::{NoteExecutionHint, NoteId, NoteInclusionProof, NoteMetadata, NoteTag, NoteType},
4};
5
6use crate::{
7    errors::{ConversionError, MissingFieldHelper},
8    generated::note as proto,
9};
10
11impl TryFrom<proto::NoteMetadata> for NoteMetadata {
12    type Error = ConversionError;
13
14    fn try_from(value: proto::NoteMetadata) -> Result<Self, Self::Error> {
15        let sender = value
16            .sender
17            .ok_or_else(|| proto::NoteMetadata::missing_field(stringify!(sender)))?
18            .try_into()?;
19        let note_type = NoteType::try_from(u64::from(value.note_type))?;
20        let tag = NoteTag::from(value.tag);
21
22        let execution_hint = NoteExecutionHint::try_from(value.execution_hint)?;
23
24        let aux = Felt::try_from(value.aux).map_err(|_| ConversionError::NotAValidFelt)?;
25
26        Ok(NoteMetadata::new(sender, note_type, tag, execution_hint, aux)?)
27    }
28}
29
30impl From<NoteMetadata> for proto::NoteMetadata {
31    fn from(val: NoteMetadata) -> Self {
32        let sender = Some(val.sender().into());
33        let note_type = val.note_type() as u32;
34        let tag = val.tag().into();
35        let execution_hint: u64 = val.execution_hint().into();
36        let aux = val.aux().into();
37
38        proto::NoteMetadata {
39            sender,
40            note_type,
41            tag,
42            execution_hint,
43            aux,
44        }
45    }
46}
47
48impl From<(&NoteId, &NoteInclusionProof)> for proto::NoteInclusionInBlockProof {
49    fn from((note_id, proof): (&NoteId, &NoteInclusionProof)) -> Self {
50        Self {
51            note_id: Some(note_id.into()),
52            block_num: proof.location().block_num().as_u32(),
53            note_index_in_block: proof.location().node_index_in_block().into(),
54            merkle_path: Some(Into::into(proof.note_path())),
55        }
56    }
57}
58
59impl TryFrom<&proto::NoteInclusionInBlockProof> for (NoteId, NoteInclusionProof) {
60    type Error = ConversionError;
61
62    fn try_from(
63        proof: &proto::NoteInclusionInBlockProof,
64    ) -> Result<(NoteId, NoteInclusionProof), Self::Error> {
65        Ok((
66            Digest::try_from(
67                proof
68                    .note_id
69                    .as_ref()
70                    .ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(note_id)))?,
71            )?
72            .into(),
73            NoteInclusionProof::new(
74                proof.block_num.into(),
75                proof.note_index_in_block.try_into()?,
76                proof
77                    .merkle_path
78                    .as_ref()
79                    .ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(
80                        merkle_path
81                    )))?
82                    .try_into()?,
83            )?,
84        ))
85    }
86}