Skip to main content

miden_protocol/note/
header.rs

1use super::{
2    ByteReader,
3    ByteWriter,
4    Deserializable,
5    DeserializationError,
6    NoteDetailsCommitment,
7    NoteId,
8    NoteMetadata,
9    Serializable,
10};
11
12// NOTE HEADER
13// ================================================================================================
14
15/// Holds the strictly required, public information of a note.
16///
17/// See [NoteDetailsCommitment] and [NoteMetadata] for additional details.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct NoteHeader {
20    details_commitment: NoteDetailsCommitment,
21    metadata: NoteMetadata,
22}
23
24impl NoteHeader {
25    /// Returns a new [NoteHeader] instantiated from the specified note details commitment and
26    /// metadata.
27    pub fn new(details_commitment: NoteDetailsCommitment, metadata: NoteMetadata) -> Self {
28        Self { details_commitment, metadata }
29    }
30
31    /// Returns the note's identifier.
32    ///
33    /// The [NoteId] commits to both the note details and the note metadata.
34    pub fn id(&self) -> NoteId {
35        NoteId::new(self.details_commitment(), self.metadata())
36    }
37
38    /// Returns the commitment to the note's details, excluding metadata.
39    pub fn details_commitment(&self) -> NoteDetailsCommitment {
40        self.details_commitment
41    }
42
43    /// Returns a reference to the note's metadata.
44    pub fn metadata(&self) -> &NoteMetadata {
45        &self.metadata
46    }
47
48    /// Consumes self and returns the note header's metadata.
49    pub fn into_metadata(self) -> NoteMetadata {
50        self.metadata
51    }
52}
53
54// SERIALIZATION
55// ================================================================================================
56
57impl Serializable for NoteHeader {
58    fn write_into<W: ByteWriter>(&self, target: &mut W) {
59        self.details_commitment.write_into(target);
60        self.metadata.write_into(target);
61    }
62
63    fn get_size_hint(&self) -> usize {
64        self.details_commitment.get_size_hint() + self.metadata.get_size_hint()
65    }
66}
67
68impl Deserializable for NoteHeader {
69    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
70        let details_commitment = NoteDetailsCommitment::read_from(source)?;
71        let metadata = NoteMetadata::read_from(source)?;
72
73        Ok(Self::new(details_commitment, metadata))
74    }
75}