Skip to main content

miden_protocol/note/
details.rs

1use super::{NoteAssets, NoteDetailsCommitment, NoteRecipient, NoteScript, NoteStorage};
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 commitment of this [`NoteDetails`].
36    ///
37    /// This value is used as part of the note's public identifier.
38    pub fn commitment(&self) -> NoteDetailsCommitment {
39        NoteDetailsCommitment::new(self.recipient(), self.assets())
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    // MUTATORS
68    // --------------------------------------------------------------------------------------------
69
70    /// Reduces the size of the note script by stripping all debug info from it.
71    pub fn minify_script(&mut self) {
72        self.recipient.minify_script();
73    }
74
75    /// Decomposes note details into underlying assets and recipient.
76    pub fn into_parts(self) -> (NoteAssets, NoteRecipient) {
77        (self.assets, self.recipient)
78    }
79}
80
81// AS REF
82// ================================================================================================
83
84impl AsRef<NoteRecipient> for NoteDetails {
85    fn as_ref(&self) -> &NoteRecipient {
86        self.recipient()
87    }
88}
89
90// SERIALIZATION
91// ================================================================================================
92
93impl Serializable for NoteDetails {
94    fn write_into<W: ByteWriter>(&self, target: &mut W) {
95        let Self { assets, recipient } = self;
96
97        assets.write_into(target);
98        recipient.write_into(target);
99    }
100
101    fn get_size_hint(&self) -> usize {
102        self.assets.get_size_hint() + self.recipient.get_size_hint()
103    }
104}
105
106impl Deserializable for NoteDetails {
107    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
108        let assets = NoteAssets::read_from(source)?;
109        let recipient = NoteRecipient::read_from(source)?;
110        Ok(Self::new(assets, recipient))
111    }
112}