miden_objects/note/
details.rs

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