miden_objects/note/
partial.rs

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