Skip to main content

miden_protocol/note/
details.rs

1use super::{NoteAssets, NoteId, NoteRecipient, NoteScript, NoteStorage, Nullifier};
2use crate::Word;
3use crate::utils::serde::{
4    ByteReader,
5    ByteWriter,
6    Deserializable,
7    DeserializationError,
8    Serializable,
9};
10
11// NOTE DETAILS
12// ================================================================================================
13
14/// Details of a note consisting of assets, script, storage, and a serial number.
15///
16/// See [super::Note] for more details.
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct NoteDetails {
19    assets: NoteAssets,
20    recipient: NoteRecipient,
21}
22
23impl NoteDetails {
24    // CONSTRUCTOR
25    // --------------------------------------------------------------------------------------------
26
27    /// Returns a new note created with the specified parameters.
28    pub fn new(assets: NoteAssets, recipient: NoteRecipient) -> Self {
29        Self { assets, recipient }
30    }
31
32    // PUBLIC ACCESSORS
33    // --------------------------------------------------------------------------------------------
34
35    /// Returns the note's unique identifier.
36    ///
37    /// This value is both an unique identifier and a commitment to the note.
38    pub fn id(&self) -> NoteId {
39        NoteId::from(self)
40    }
41
42    /// Returns the note's assets.
43    pub fn assets(&self) -> &NoteAssets {
44        &self.assets
45    }
46
47    /// Returns the note's recipient serial_num, the secret required to consume the note.
48    pub fn serial_num(&self) -> Word {
49        self.recipient.serial_num()
50    }
51
52    /// Returns the note's recipient script which locks the assets of this note.
53    pub fn script(&self) -> &NoteScript {
54        self.recipient.script()
55    }
56
57    /// Returns the note's recipient storage which customizes the script's behavior.
58    pub fn storage(&self) -> &NoteStorage {
59        self.recipient.storage()
60    }
61
62    /// Returns the note's recipient.
63    pub fn recipient(&self) -> &NoteRecipient {
64        &self.recipient
65    }
66
67    /// Returns the note's nullifier.
68    ///
69    /// This is public data, used to prevent double spend.
70    pub fn nullifier(&self) -> Nullifier {
71        Nullifier::from(self)
72    }
73
74    // MUTATORS
75    // --------------------------------------------------------------------------------------------
76
77    /// Reduces the size of the note script by stripping all debug info from it.
78    pub fn minify_script(&mut self) {
79        self.recipient.minify_script();
80    }
81
82    /// Decomposes note details into underlying assets and recipient.
83    pub fn into_parts(self) -> (NoteAssets, NoteRecipient) {
84        (self.assets, self.recipient)
85    }
86}
87
88// AS REF
89// ================================================================================================
90
91impl AsRef<NoteRecipient> for NoteDetails {
92    fn as_ref(&self) -> &NoteRecipient {
93        self.recipient()
94    }
95}
96
97// SERIALIZATION
98// ================================================================================================
99
100impl Serializable for NoteDetails {
101    fn write_into<W: ByteWriter>(&self, target: &mut W) {
102        let Self { assets, recipient } = self;
103
104        assets.write_into(target);
105        recipient.write_into(target);
106    }
107
108    fn get_size_hint(&self) -> usize {
109        self.assets.get_size_hint() + self.recipient.get_size_hint()
110    }
111}
112
113impl Deserializable for NoteDetails {
114    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
115        let assets = NoteAssets::read_from(source)?;
116        let recipient = NoteRecipient::read_from(source)?;
117        Ok(Self::new(assets, recipient))
118    }
119}