miden_protocol/note/header.rs
1use super::{
2 ByteReader,
3 ByteWriter,
4 Deserializable,
5 DeserializationError,
6 NoteId,
7 NoteMetadata,
8 Serializable,
9 Word,
10};
11use crate::Hasher;
12
13// NOTE HEADER
14// ================================================================================================
15
16/// Holds the strictly required, public information of a note.
17///
18/// See [NoteId] and [NoteMetadata] for additional details.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct NoteHeader {
21 note_id: NoteId,
22 note_metadata: NoteMetadata,
23}
24
25impl NoteHeader {
26 /// Returns a new [NoteHeader] instantiated from the specified note ID and metadata.
27 pub fn new(note_id: NoteId, note_metadata: NoteMetadata) -> Self {
28 Self { note_id, note_metadata }
29 }
30
31 /// Returns the note's identifier.
32 ///
33 /// The [NoteId] value is both an unique identifier and a commitment to the note.
34 pub fn id(&self) -> NoteId {
35 self.note_id
36 }
37
38 /// Returns the note's metadata.
39 pub fn metadata(&self) -> &NoteMetadata {
40 &self.note_metadata
41 }
42
43 /// Consumes self and returns the note header's metadata.
44 pub fn into_metadata(self) -> NoteMetadata {
45 self.note_metadata
46 }
47
48 /// Returns a commitment to the note and its metadata.
49 ///
50 /// > hash(NOTE_ID || NOTE_METADATA_COMMITMENT)
51 ///
52 /// This value is used primarily for authenticating notes consumed when they are consumed
53 /// in a transaction.
54 pub fn commitment(&self) -> Word {
55 compute_note_commitment(self.id(), self.metadata())
56 }
57}
58
59// UTILITIES
60// ================================================================================================
61
62/// Returns a commitment to the note and its metadata.
63///
64/// > hash(NOTE_ID || NOTE_METADATA_COMMITMENT)
65///
66/// This value is used primarily for authenticating notes consumed when they are consumed
67/// in a transaction.
68pub fn compute_note_commitment(id: NoteId, metadata: &NoteMetadata) -> Word {
69 Hasher::merge(&[id.as_word(), metadata.to_commitment()])
70}
71
72// SERIALIZATION
73// ================================================================================================
74
75impl Serializable for NoteHeader {
76 fn write_into<W: ByteWriter>(&self, target: &mut W) {
77 self.note_id.write_into(target);
78 self.note_metadata.write_into(target);
79 }
80}
81
82impl Deserializable for NoteHeader {
83 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
84 let note_id = NoteId::read_from(source)?;
85 let note_metadata = NoteMetadata::read_from(source)?;
86
87 Ok(Self { note_id, note_metadata })
88 }
89}