miden_objects/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    metadata: NoteMetadata,
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        Self { metadata, recipient_digest, assets }
35    }
36
37    /// Returns the ID corresponding to this note.
38    pub fn id(&self) -> NoteId {
39        NoteId::new(self.recipient_digest, self.assets.commitment())
40    }
41
42    /// Returns the metadata associated with this note.
43    pub fn metadata(&self) -> &NoteMetadata {
44        &self.metadata
45    }
46
47    /// Returns the digest of the recipient associated with this note.
48    ///
49    /// See [super::NoteRecipient] for more info.
50    pub fn recipient_digest(&self) -> Word {
51        self.recipient_digest
52    }
53
54    /// Returns a list of assets associated with this note.
55    pub fn assets(&self) -> &NoteAssets {
56        &self.assets
57    }
58}
59
60impl From<&PartialNote> for NoteHeader {
61    fn from(note: &PartialNote) -> Self {
62        NoteHeader::new(note.id(), note.metadata)
63    }
64}
65
66impl From<PartialNote> for NoteHeader {
67    fn from(note: PartialNote) -> Self {
68        NoteHeader::new(note.id(), note.metadata)
69    }
70}
71
72// SERIALIZATION
73// ================================================================================================
74
75impl Serializable for PartialNote {
76    fn write_into<W: ByteWriter>(&self, target: &mut W) {
77        self.metadata.write_into(target);
78        self.recipient_digest.write_into(target);
79        self.assets.write_into(target)
80    }
81}
82
83impl Deserializable for PartialNote {
84    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
85        let metadata = NoteMetadata::read_from(source)?;
86        let recipient_digest = Word::read_from(source)?;
87        let assets = NoteAssets::read_from(source)?;
88
89        Ok(Self::new(metadata, recipient_digest, assets))
90    }
91}