miden_standards/note/
network_note.rs1use miden_protocol::account::AccountId;
2use miden_protocol::note::{Note, NoteAttachments, 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.attachments())?;
31
32 Ok(Self { note })
33 }
34
35 pub fn into_note(self) -> Note {
37 self.note
38 }
39
40 pub fn as_note(&self) -> &Note {
42 &self.note
43 }
44
45 pub fn metadata(&self) -> &NoteMetadata {
47 self.note.metadata()
48 }
49
50 pub fn target_account_id(&self) -> AccountId {
52 self.target().target_id()
53 }
54
55 pub fn target(&self) -> NetworkAccountTarget {
57 NetworkAccountTarget::try_from(self.note.attachments())
58 .expect("AccountTargetNetworkNote guarantees valid NetworkAccountTarget attachment")
59 }
60
61 pub fn execution_hint(&self) -> NoteExecutionHint {
63 self.target().execution_hint()
64 }
65
66 pub fn attachments(&self) -> &NoteAttachments {
68 self.note.attachments()
69 }
70
71 pub fn note_type(&self) -> NoteType {
73 self.metadata().note_type()
74 }
75}
76
77pub trait NetworkNoteExt {
79 fn is_network_note(&self) -> bool;
82
83 fn into_account_target_network_note(
86 self,
87 ) -> Result<AccountTargetNetworkNote, NetworkAccountTargetError>;
88}
89
90impl NetworkNoteExt for Note {
91 fn is_network_note(&self) -> bool {
92 self.metadata().note_type() == NoteType::Public
93 && NetworkAccountTarget::try_from(self.attachments()).is_ok()
94 }
95
96 fn into_account_target_network_note(
97 self,
98 ) -> Result<AccountTargetNetworkNote, NetworkAccountTargetError> {
99 AccountTargetNetworkNote::new(self)
100 }
101}
102
103impl TryFrom<Note> for AccountTargetNetworkNote {
104 type Error = NetworkAccountTargetError;
105
106 fn try_from(note: Note) -> Result<Self, Self::Error> {
107 Self::new(note)
108 }
109}