use super::{
ByteReader,
ByteWriter,
Deserializable,
DeserializationError,
NoteId,
NoteMetadata,
Serializable,
Word,
};
use crate::Hasher;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NoteHeader {
note_id: NoteId,
note_metadata: NoteMetadata,
}
impl NoteHeader {
pub fn new(note_id: NoteId, note_metadata: NoteMetadata) -> Self {
Self { note_id, note_metadata }
}
pub fn id(&self) -> NoteId {
self.note_id
}
pub fn metadata(&self) -> &NoteMetadata {
&self.note_metadata
}
pub fn into_metadata(self) -> NoteMetadata {
self.note_metadata
}
pub fn to_commitment(&self) -> Word {
compute_note_commitment(self.id(), self.metadata())
}
}
pub fn compute_note_commitment(id: NoteId, metadata: &NoteMetadata) -> Word {
Hasher::merge(&[id.as_word(), metadata.to_commitment()])
}
impl Serializable for NoteHeader {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.note_id.write_into(target);
self.note_metadata.write_into(target);
}
fn get_size_hint(&self) -> usize {
self.note_id.get_size_hint() + self.note_metadata.get_size_hint()
}
}
impl Deserializable for NoteHeader {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let note_id = NoteId::read_from(source)?;
let note_metadata = NoteMetadata::read_from(source)?;
Ok(Self { note_id, note_metadata })
}
}