miden_node_proto/domain/
note.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use miden_objects::{
4    note::{NoteExecutionHint, NoteId, NoteInclusionProof, NoteMetadata, NoteTag, NoteType},
5    Digest, Felt,
6};
7
8use crate::{
9    convert,
10    domain::block::BlockInclusionProof,
11    errors::{ConversionError, MissingFieldHelper},
12    generated::note as proto,
13    try_convert,
14};
15
16impl TryFrom<proto::NoteMetadata> for NoteMetadata {
17    type Error = ConversionError;
18
19    fn try_from(value: proto::NoteMetadata) -> Result<Self, Self::Error> {
20        let sender = value
21            .sender
22            .ok_or_else(|| proto::NoteMetadata::missing_field(stringify!(sender)))?
23            .try_into()?;
24        let note_type = NoteType::try_from(u64::from(value.note_type))?;
25        let tag = NoteTag::from(value.tag);
26
27        let execution_hint = NoteExecutionHint::try_from(value.execution_hint)?;
28
29        let aux = Felt::try_from(value.aux).map_err(|_| ConversionError::NotAValidFelt)?;
30
31        Ok(NoteMetadata::new(sender, note_type, tag, execution_hint, aux)?)
32    }
33}
34
35impl From<NoteMetadata> for proto::NoteMetadata {
36    fn from(val: NoteMetadata) -> Self {
37        let sender = Some(val.sender().into());
38        let note_type = val.note_type() as u32;
39        let tag = val.tag().into();
40        let execution_hint: u64 = val.execution_hint().into();
41        let aux = val.aux().into();
42
43        proto::NoteMetadata {
44            sender,
45            note_type,
46            tag,
47            execution_hint,
48            aux,
49        }
50    }
51}
52
53impl From<(&NoteId, &NoteInclusionProof)> for proto::NoteInclusionInBlockProof {
54    fn from((note_id, proof): (&NoteId, &NoteInclusionProof)) -> Self {
55        Self {
56            note_id: Some(note_id.into()),
57            block_num: proof.location().block_num().as_u32(),
58            note_index_in_block: proof.location().node_index_in_block().into(),
59            merkle_path: Some(Into::into(proof.note_path())),
60        }
61    }
62}
63
64impl TryFrom<&proto::NoteInclusionInBlockProof> for (NoteId, NoteInclusionProof) {
65    type Error = ConversionError;
66
67    fn try_from(
68        proof: &proto::NoteInclusionInBlockProof,
69    ) -> Result<(NoteId, NoteInclusionProof), Self::Error> {
70        Ok((
71            Digest::try_from(
72                proof
73                    .note_id
74                    .as_ref()
75                    .ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(note_id)))?,
76            )?
77            .into(),
78            NoteInclusionProof::new(
79                proof.block_num.into(),
80                proof.note_index_in_block.try_into()?,
81                proof
82                    .merkle_path
83                    .as_ref()
84                    .ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(
85                        merkle_path
86                    )))?
87                    .try_into()?,
88            )?,
89        ))
90    }
91}
92
93#[derive(Clone, Default, Debug)]
94pub struct NoteAuthenticationInfo {
95    pub block_proofs: Vec<BlockInclusionProof>,
96    pub note_proofs: BTreeMap<NoteId, NoteInclusionProof>,
97}
98
99impl NoteAuthenticationInfo {
100    pub fn contains_note(&self, note: &NoteId) -> bool {
101        self.note_proofs.contains_key(note)
102    }
103
104    pub fn note_ids(&self) -> BTreeSet<NoteId> {
105        self.note_proofs.keys().copied().collect()
106    }
107}
108
109impl From<NoteAuthenticationInfo> for proto::NoteAuthenticationInfo {
110    fn from(value: NoteAuthenticationInfo) -> Self {
111        Self {
112            note_proofs: convert(&value.note_proofs),
113            block_proofs: convert(value.block_proofs),
114        }
115    }
116}
117
118impl TryFrom<proto::NoteAuthenticationInfo> for NoteAuthenticationInfo {
119    type Error = ConversionError;
120
121    fn try_from(value: proto::NoteAuthenticationInfo) -> Result<Self, Self::Error> {
122        let result = Self {
123            block_proofs: try_convert(value.block_proofs)?,
124            note_proofs: try_convert(&value.note_proofs)?,
125        };
126
127        Ok(result)
128    }
129}