Skip to main content

miden_protocol/note/
partial.rs

1use super::{
2    ByteReader,
3    ByteWriter,
4    Deserializable,
5    DeserializationError,
6    NoteAssets,
7    NoteHeader,
8    NoteId,
9    NoteMetadata,
10    Serializable,
11};
12use crate::Word;
13
14// PARTIAL NOTE
15// ================================================================================================
16
17/// Partial information about a note.
18///
19/// Partial note consists of [NoteMetadata], [NoteAssets], and a recipient digest (see
20/// [super::NoteRecipient]). However, it does not contain detailed recipient info, including
21/// note script, note inputs, and note's serial number. This means that a partial note is
22/// sufficient to compute note ID and note header, but not sufficient to compute note nullifier,
23/// and generally does not have enough info to execute the note.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct PartialNote {
26    header: NoteHeader,
27    recipient_digest: Word,
28    assets: NoteAssets,
29}
30
31impl PartialNote {
32    /// Returns a new [PartialNote] instantiated from the provided parameters.
33    pub fn new(metadata: NoteMetadata, recipient_digest: Word, assets: NoteAssets) -> Self {
34        let note_id = NoteId::new(recipient_digest, assets.commitment());
35        let header = NoteHeader::new(note_id, metadata);
36        Self { header, recipient_digest, assets }
37    }
38
39    /// Returns the ID corresponding to this note.
40    pub fn id(&self) -> NoteId {
41        NoteId::new(self.recipient_digest, self.assets.commitment())
42    }
43
44    /// Returns the metadata associated with this note.
45    pub fn metadata(&self) -> &NoteMetadata {
46        self.header.metadata()
47    }
48
49    /// Returns the digest of the recipient associated with this note.
50    ///
51    /// See [super::NoteRecipient] for more info.
52    pub fn recipient_digest(&self) -> Word {
53        self.recipient_digest
54    }
55
56    /// Returns a list of assets associated with this note.
57    pub fn assets(&self) -> &NoteAssets {
58        &self.assets
59    }
60
61    /// Returns the [`NoteHeader`] of this note.
62    pub fn header(&self) -> &NoteHeader {
63        &self.header
64    }
65}
66
67// SERIALIZATION
68// ================================================================================================
69
70impl Serializable for PartialNote {
71    fn write_into<W: ByteWriter>(&self, target: &mut W) {
72        // Serialize only metadata since the note ID in the header can be recomputed from the
73        // remaining data.
74        self.header().metadata().write_into(target);
75        self.recipient_digest.write_into(target);
76        self.assets.write_into(target)
77    }
78}
79
80impl Deserializable for PartialNote {
81    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
82        let metadata = NoteMetadata::read_from(source)?;
83        let recipient_digest = Word::read_from(source)?;
84        let assets = NoteAssets::read_from(source)?;
85
86        Ok(Self::new(metadata, recipient_digest, assets))
87    }
88}