use miden_crypto::Word;
use crate::account::AccountId;
use crate::errors::NoteError;
use crate::utils::serde::{
ByteReader,
ByteWriter,
Deserializable,
DeserializationError,
Serializable,
};
use crate::{Felt, Hasher, ZERO};
mod assets;
pub use assets::NoteAssets;
mod details;
pub use details::NoteDetails;
mod header;
pub use header::NoteHeader;
mod storage;
pub use storage::NoteStorage;
mod metadata;
pub use metadata::{NoteMetadata, PartialNoteMetadata};
mod attachment;
pub use attachment::{
NoteAttachment,
NoteAttachmentContent,
NoteAttachmentHeader,
NoteAttachmentScheme,
NoteAttachments,
};
mod note_id;
pub use note_id::NoteId;
mod note_details_commitment;
pub use note_details_commitment::NoteDetailsCommitment;
mod note_tag;
pub use note_tag::NoteTag;
mod note_type;
pub use note_type::NoteType;
mod nullifier;
pub use nullifier::Nullifier;
mod location;
pub use location::{NoteInclusionProof, NoteLocation};
mod partial;
pub use partial::PartialNote;
mod recipient;
pub use recipient::NoteRecipient;
mod script;
pub use script::{NoteScript, NoteScriptRoot};
mod file;
pub use file::NoteFile;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Note {
header: NoteHeader,
details: NoteDetails,
attachments: NoteAttachments,
nullifier: Nullifier,
}
impl Note {
pub fn new(
assets: NoteAssets,
partial_metadata: PartialNoteMetadata,
recipient: NoteRecipient,
) -> Self {
Self::with_attachments(assets, partial_metadata, recipient, NoteAttachments::default())
}
pub fn with_attachments(
assets: NoteAssets,
partial_metadata: PartialNoteMetadata,
recipient: NoteRecipient,
attachments: NoteAttachments,
) -> Self {
let details = NoteDetails::new(assets, recipient);
let metadata = NoteMetadata::new(partial_metadata, &attachments);
let header = NoteHeader::new(details.commitment(), metadata);
let nullifier = Nullifier::from_details_and_metadata(&details, &metadata);
Self { header, details, attachments, nullifier }
}
pub fn header(&self) -> &NoteHeader {
&self.header
}
pub fn id(&self) -> NoteId {
self.header.id()
}
pub fn details_commitment(&self) -> NoteDetailsCommitment {
self.header.details_commitment()
}
pub fn assets(&self) -> &NoteAssets {
self.details.assets()
}
pub fn serial_num(&self) -> Word {
self.details.serial_num()
}
pub fn script(&self) -> &NoteScript {
self.details.script()
}
pub fn storage(&self) -> &NoteStorage {
self.details.storage()
}
pub fn recipient(&self) -> &NoteRecipient {
self.details.recipient()
}
pub fn nullifier(&self) -> Nullifier {
self.nullifier
}
pub fn attachments(&self) -> &NoteAttachments {
&self.attachments
}
pub fn metadata(&self) -> &NoteMetadata {
self.header.metadata()
}
pub fn minify_script(&mut self) {
self.details.minify_script();
}
pub fn into_parts(self) -> (NoteAssets, NoteMetadata, NoteRecipient, NoteAttachments) {
let (assets, recipient) = self.details.into_parts();
let metadata = self.header.into_metadata();
(assets, metadata, recipient, self.attachments)
}
}
impl AsRef<NoteRecipient> for Note {
fn as_ref(&self) -> &NoteRecipient {
self.recipient()
}
}
impl From<Note> for NoteHeader {
fn from(note: Note) -> Self {
note.header
}
}
impl From<&Note> for NoteDetails {
fn from(note: &Note) -> Self {
note.details.clone()
}
}
impl From<Note> for NoteDetails {
fn from(note: Note) -> Self {
note.details
}
}
impl From<Note> for PartialNote {
fn from(note: Note) -> Self {
let (assets, recipient, ..) = note.details.into_parts();
PartialNote::new(
note.header.into_metadata().into_partial_metadata(),
recipient.digest(),
assets,
note.attachments,
)
}
}
impl From<&Note> for NoteHeader {
fn from(note: &Note) -> Self {
note.header
}
}
impl Serializable for Note {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let Self {
header,
details,
attachments,
nullifier: _,
} = self;
header.metadata().partial_metadata().write_into(target);
details.write_into(target);
attachments.write_into(target);
}
fn get_size_hint(&self) -> usize {
self.header.metadata().partial_metadata().get_size_hint()
+ self.details.get_size_hint()
+ self.attachments.get_size_hint()
}
}
impl Deserializable for Note {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let partial_metadata = PartialNoteMetadata::read_from(source)?;
let details = NoteDetails::read_from(source)?;
let attachments = NoteAttachments::read_from(source)?;
let (assets, recipient) = details.into_parts();
Ok(Self::with_attachments(assets, partial_metadata, recipient, attachments))
}
}