miden_node_proto/domain/
note.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::collections::{BTreeMap, BTreeSet};

use miden_objects::{
    note::{NoteExecutionHint, NoteId, NoteInclusionProof, NoteMetadata, NoteTag, NoteType},
    Digest, Felt,
};

use crate::{
    convert,
    domain::block::BlockInclusionProof,
    errors::{ConversionError, MissingFieldHelper},
    generated::note as proto,
    try_convert,
};

impl TryFrom<proto::NoteMetadata> for NoteMetadata {
    type Error = ConversionError;

    fn try_from(value: proto::NoteMetadata) -> Result<Self, Self::Error> {
        let sender = value
            .sender
            .ok_or_else(|| proto::NoteMetadata::missing_field(stringify!(sender)))?
            .try_into()?;
        let note_type = NoteType::try_from(u64::from(value.note_type))?;
        let tag = NoteTag::from(value.tag);

        let execution_hint = NoteExecutionHint::try_from(value.execution_hint)?;

        let aux = Felt::try_from(value.aux).map_err(|_| ConversionError::NotAValidFelt)?;

        Ok(NoteMetadata::new(sender, note_type, tag, execution_hint, aux)?)
    }
}

impl From<NoteMetadata> for proto::NoteMetadata {
    fn from(val: NoteMetadata) -> Self {
        let sender = Some(val.sender().into());
        let note_type = val.note_type() as u32;
        let tag = val.tag().into();
        let execution_hint: u64 = val.execution_hint().into();
        let aux = val.aux().into();

        proto::NoteMetadata {
            sender,
            note_type,
            tag,
            execution_hint,
            aux,
        }
    }
}

impl From<(&NoteId, &NoteInclusionProof)> for proto::NoteInclusionInBlockProof {
    fn from((note_id, proof): (&NoteId, &NoteInclusionProof)) -> Self {
        Self {
            note_id: Some(note_id.into()),
            block_num: proof.location().block_num().as_u32(),
            note_index_in_block: proof.location().node_index_in_block().into(),
            merkle_path: Some(Into::into(proof.note_path())),
        }
    }
}

impl TryFrom<&proto::NoteInclusionInBlockProof> for (NoteId, NoteInclusionProof) {
    type Error = ConversionError;

    fn try_from(
        proof: &proto::NoteInclusionInBlockProof,
    ) -> Result<(NoteId, NoteInclusionProof), Self::Error> {
        Ok((
            Digest::try_from(
                proof
                    .note_id
                    .as_ref()
                    .ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(note_id)))?,
            )?
            .into(),
            NoteInclusionProof::new(
                proof.block_num.into(),
                proof.note_index_in_block.try_into()?,
                proof
                    .merkle_path
                    .as_ref()
                    .ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(
                        merkle_path
                    )))?
                    .try_into()?,
            )?,
        ))
    }
}

#[derive(Clone, Default, Debug)]
pub struct NoteAuthenticationInfo {
    pub block_proofs: Vec<BlockInclusionProof>,
    pub note_proofs: BTreeMap<NoteId, NoteInclusionProof>,
}

impl NoteAuthenticationInfo {
    pub fn contains_note(&self, note: &NoteId) -> bool {
        self.note_proofs.contains_key(note)
    }

    pub fn note_ids(&self) -> BTreeSet<NoteId> {
        self.note_proofs.keys().copied().collect()
    }
}

impl From<NoteAuthenticationInfo> for proto::NoteAuthenticationInfo {
    fn from(value: NoteAuthenticationInfo) -> Self {
        Self {
            note_proofs: convert(&value.note_proofs),
            block_proofs: convert(value.block_proofs),
        }
    }
}

impl TryFrom<proto::NoteAuthenticationInfo> for NoteAuthenticationInfo {
    type Error = ConversionError;

    fn try_from(value: proto::NoteAuthenticationInfo) -> Result<Self, Self::Error> {
        let result = Self {
            block_proofs: try_convert(value.block_proofs)?,
            note_proofs: try_convert(&value.note_proofs)?,
        };

        Ok(result)
    }
}