miden_standards/note/
network_note.rs1use miden_protocol::account::AccountId;
2use miden_protocol::note::{Note, NoteAttachment, NoteMetadata, NoteType};
3
4use crate::note::{NetworkAccountTarget, NetworkAccountTargetError, NoteExecutionHint};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct AccountTargetNetworkNote {
14 note: Note,
15}
16
17impl AccountTargetNetworkNote {
18 pub fn new(note: Note) -> Result<Self, NetworkAccountTargetError> {
24 if note.metadata().note_type() != NoteType::Public {
26 return Err(NetworkAccountTargetError::NoteNotPublic(note.metadata().note_type()));
27 }
28
29 NetworkAccountTarget::try_from(note.metadata().attachment())?;
31 Ok(Self { note })
32 }
33
34 pub fn into_note(self) -> Note {
36 self.note
37 }
38
39 pub fn as_note(&self) -> &Note {
41 &self.note
42 }
43
44 pub fn metadata(&self) -> &NoteMetadata {
46 self.note.metadata()
47 }
48
49 pub fn target_account_id(&self) -> AccountId {
51 self.target().target_id()
52 }
53
54 pub fn target(&self) -> NetworkAccountTarget {
56 NetworkAccountTarget::try_from(self.note.metadata().attachment())
57 .expect("AccountTargetNetworkNote guarantees valid NetworkAccountTarget attachment")
58 }
59
60 pub fn execution_hint(&self) -> NoteExecutionHint {
62 self.target().execution_hint()
63 }
64
65 pub fn attachment(&self) -> &NoteAttachment {
67 self.metadata().attachment()
68 }
69
70 pub fn note_type(&self) -> NoteType {
72 self.metadata().note_type()
73 }
74}
75
76pub trait NetworkNoteExt {
78 fn is_network_note(&self) -> bool;
81
82 fn into_account_target_network_note(
85 self,
86 ) -> Result<AccountTargetNetworkNote, NetworkAccountTargetError>;
87}
88
89impl NetworkNoteExt for Note {
90 fn is_network_note(&self) -> bool {
91 self.metadata().note_type() == NoteType::Public
92 && NetworkAccountTarget::try_from(self.metadata().attachment()).is_ok()
93 }
94
95 fn into_account_target_network_note(
96 self,
97 ) -> Result<AccountTargetNetworkNote, NetworkAccountTargetError> {
98 AccountTargetNetworkNote::new(self)
99 }
100}
101
102impl TryFrom<Note> for AccountTargetNetworkNote {
103 type Error = NetworkAccountTargetError;
104
105 fn try_from(note: Note) -> Result<Self, Self::Error> {
106 Self::new(note)
107 }
108}