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 storage, 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    /// Consumes self and returns the non-Copy parts of this note.
67    pub fn into_parts(self) -> (NoteAssets, NoteHeader) {
68        (self.assets, self.header)
69    }
70}
71
72// SERIALIZATION
73// ================================================================================================
74
75impl Serializable for PartialNote {
76    fn write_into<W: ByteWriter>(&self, target: &mut W) {
77        // Serialize only metadata since the note ID in the header can be recomputed from the
78        // remaining data.
79        self.header().metadata().write_into(target);
80        self.recipient_digest.write_into(target);
81        self.assets.write_into(target)
82    }
83
84    fn get_size_hint(&self) -> usize {
85        self.metadata().get_size_hint() + Word::SERIALIZED_SIZE + self.assets.get_size_hint()
86    }
87}
88
89impl Deserializable for PartialNote {
90    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
91        let metadata = NoteMetadata::read_from(source)?;
92        let recipient_digest = Word::read_from(source)?;
93        let assets = NoteAssets::read_from(source)?;
94
95        Ok(Self::new(metadata, recipient_digest, assets))
96    }
97}