miden_objects/note/
details.rs

1use vm_processor::DeserializationError;
2
3use super::{NoteAssets, NoteId, NoteInputs, NoteRecipient, NoteScript, Nullifier};
4use crate::{
5    Word,
6    utils::serde::{ByteReader, ByteWriter, Deserializable, Serializable},
7};
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// AS REF
79// ================================================================================================
80
81impl AsRef<NoteRecipient> for NoteDetails {
82    fn as_ref(&self) -> &NoteRecipient {
83        self.recipient()
84    }
85}
86
87// SERIALIZATION
88// ================================================================================================
89
90impl Serializable for NoteDetails {
91    fn write_into<W: ByteWriter>(&self, target: &mut W) {
92        let Self { assets, recipient } = self;
93
94        assets.write_into(target);
95        recipient.write_into(target);
96    }
97}
98
99impl Deserializable for NoteDetails {
100    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
101        let assets = NoteAssets::read_from(source)?;
102        let recipient = NoteRecipient::read_from(source)?;
103        Ok(Self::new(assets, recipient))
104    }
105}