Skip to main content

miden_testing/mock_chain/
note.rs

1use miden_processor::serde::DeserializationError;
2use miden_protocol::note::{Note, NoteAttachments, NoteId, NoteInclusionProof, NoteMetadata};
3use miden_protocol::transaction::InputNote;
4use miden_tx::utils::serde::{ByteReader, ByteWriter, Deserializable, Serializable};
5
6// MOCK CHAIN NOTE
7// ================================================================================================
8
9/// Represents a note that is stored in the mock chain.
10#[allow(clippy::large_enum_variant)]
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum MockChainNote {
13    /// Details for a private note only include its [`NoteMetadata`], public [`NoteAttachments`]
14    /// and [`NoteInclusionProof`]. Other details needed to consume the note are expected to be
15    /// stored locally, off-chain.
16    Private(NoteId, NoteMetadata, NoteAttachments, NoteInclusionProof),
17    /// Contains the full [`Note`] object alongside its [`NoteInclusionProof`].
18    Public(Note, NoteInclusionProof),
19}
20
21impl MockChainNote {
22    /// Returns the note's inclusion details.
23    pub fn inclusion_proof(&self) -> &NoteInclusionProof {
24        match self {
25            MockChainNote::Private(_, _, _, inclusion_proof)
26            | MockChainNote::Public(_, inclusion_proof) => inclusion_proof,
27        }
28    }
29
30    /// Returns the note's metadata.
31    pub fn metadata(&self) -> &NoteMetadata {
32        match self {
33            MockChainNote::Private(_, metadata, ..) => metadata,
34            MockChainNote::Public(note, _) => note.metadata(),
35        }
36    }
37
38    /// Returns the note's attachments.
39    pub fn attachments(&self) -> &NoteAttachments {
40        match self {
41            MockChainNote::Private(_, _, attachments, _) => attachments,
42            MockChainNote::Public(note, _) => note.attachments(),
43        }
44    }
45
46    /// Returns the note's ID.
47    pub fn id(&self) -> NoteId {
48        match self {
49            MockChainNote::Private(id, ..) => *id,
50            MockChainNote::Public(note, _) => note.id(),
51        }
52    }
53
54    /// Returns the underlying note if it is public.
55    pub fn note(&self) -> Option<&Note> {
56        match self {
57            MockChainNote::Private(..) => None,
58            MockChainNote::Public(note, _) => Some(note),
59        }
60    }
61}
62
63impl TryFrom<MockChainNote> for InputNote {
64    type Error = anyhow::Error;
65
66    fn try_from(value: MockChainNote) -> Result<Self, Self::Error> {
67        match value {
68            MockChainNote::Private(..) => Err(anyhow::anyhow!(
69                "private notes in the mock chain cannot be converted into input notes due to missing details"
70            )),
71            MockChainNote::Public(note, proof) => Ok(InputNote::Authenticated { note, proof }),
72        }
73    }
74}
75
76// SERIALIZATION
77// ================================================================================================
78
79impl Serializable for MockChainNote {
80    fn write_into<W: ByteWriter>(&self, target: &mut W) {
81        match self {
82            MockChainNote::Private(id, metadata, attachments, proof) => {
83                0u8.write_into(target);
84                id.write_into(target);
85                metadata.write_into(target);
86                attachments.write_into(target);
87                proof.write_into(target);
88            },
89            MockChainNote::Public(note, proof) => {
90                1u8.write_into(target);
91                note.write_into(target);
92                proof.write_into(target);
93            },
94        }
95    }
96}
97
98impl Deserializable for MockChainNote {
99    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
100        let note_type = u8::read_from(source)?;
101        match note_type {
102            0 => {
103                let id = NoteId::read_from(source)?;
104                let metadata = NoteMetadata::read_from(source)?;
105                let attachments = NoteAttachments::read_from(source)?;
106                let proof = NoteInclusionProof::read_from(source)?;
107                Ok(MockChainNote::Private(id, metadata, attachments, proof))
108            },
109            1 => {
110                let note = Note::read_from(source)?;
111                let proof = NoteInclusionProof::read_from(source)?;
112                Ok(MockChainNote::Public(note, proof))
113            },
114            _ => Err(DeserializationError::InvalidValue(format!("Unknown note type: {note_type}"))),
115        }
116    }
117}