use miden_protocol::account::AccountId;
use miden_protocol::note::{Note, NoteAttachment, NoteMetadata, NoteType};
use crate::note::{NetworkAccountTarget, NetworkAccountTargetError, NoteExecutionHint};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AccountTargetNetworkNote {
note: Note,
}
impl AccountTargetNetworkNote {
pub fn new(note: Note) -> Result<Self, NetworkAccountTargetError> {
if note.metadata().note_type() != NoteType::Public {
return Err(NetworkAccountTargetError::NoteNotPublic(note.metadata().note_type()));
}
NetworkAccountTarget::try_from(note.metadata().attachment())?;
Ok(Self { note })
}
pub fn into_note(self) -> Note {
self.note
}
pub fn as_note(&self) -> &Note {
&self.note
}
pub fn metadata(&self) -> &NoteMetadata {
self.note.metadata()
}
pub fn target_account_id(&self) -> AccountId {
self.target().target_id()
}
pub fn target(&self) -> NetworkAccountTarget {
NetworkAccountTarget::try_from(self.note.metadata().attachment())
.expect("AccountTargetNetworkNote guarantees valid NetworkAccountTarget attachment")
}
pub fn execution_hint(&self) -> NoteExecutionHint {
self.target().execution_hint()
}
pub fn attachment(&self) -> &NoteAttachment {
self.metadata().attachment()
}
pub fn note_type(&self) -> NoteType {
self.metadata().note_type()
}
}
pub trait NetworkNoteExt {
fn is_network_note(&self) -> bool;
fn into_account_target_network_note(
self,
) -> Result<AccountTargetNetworkNote, NetworkAccountTargetError>;
}
impl NetworkNoteExt for Note {
fn is_network_note(&self) -> bool {
self.metadata().note_type() == NoteType::Public
&& NetworkAccountTarget::try_from(self.metadata().attachment()).is_ok()
}
fn into_account_target_network_note(
self,
) -> Result<AccountTargetNetworkNote, NetworkAccountTargetError> {
AccountTargetNetworkNote::new(self)
}
}
impl TryFrom<Note> for AccountTargetNetworkNote {
type Error = NetworkAccountTargetError;
fn try_from(note: Note) -> Result<Self, Self::Error> {
Self::new(note)
}
}