miden_objects/note/
details.rs

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