Skip to main content

miden_standards/account/interface/
component.rs

1use alloc::string::{String, ToString};
2use alloc::vec::Vec;
3
4use miden_protocol::account::AccountProcedureRoot;
5
6// ACCOUNT COMPONENT INTERFACE
7// ================================================================================================
8
9/// The enum holding all possible account interfaces which could be loaded to some account.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum AccountComponentInterface {
12    /// Exposes procedures from the [`BasicWallet`][crate::account::wallets::BasicWallet] module.
13    BasicWallet,
14    /// Exposes the `create_note` procedure from the
15    /// [`NoteCreator`][crate::account::note_creator::NoteCreator] component.
16    NoteCreator,
17    /// Exposes procedures from the
18    /// [`FungibleFaucet`][crate::account::faucets::FungibleFaucet] module.
19    FungibleFaucet,
20    /// Exposes procedures from the
21    /// [`CodeInspection`][crate::account::inspection::CodeInspection] component.
22    CodeInspection,
23    /// Exposes procedures from the
24    /// [`Authority`][crate::account::access::Authority] access component.
25    Authority,
26    /// Exposes procedures from the
27    /// [`Ownable2Step`][crate::account::access::Ownable2Step] access component.
28    Ownable2Step,
29    /// Exposes procedures from the
30    /// [`RoleBasedAccessControl`][crate::account::access::RoleBasedAccessControl] access
31    /// component.
32    RoleBasedAccessControl,
33    /// Exposes procedures from the
34    /// [`AuthSingleSig`][crate::account::auth::AuthSingleSig] module.
35    AuthSingleSig,
36    /// Exposes procedures from the
37    /// [`AuthMultisig`][crate::account::auth::AuthMultisig] module.
38    AuthMultisig,
39    /// Exposes procedures from the
40    /// [`AuthMultisigSmart`][crate::account::auth::AuthMultisigSmart] module.
41    AuthMultisigSmart,
42    /// Exposes procedures from the
43    /// [`AuthGuardedMultisig`][crate::account::auth::AuthGuardedMultisig] module.
44    AuthGuardedMultisig,
45    /// Exposes procedures from the [`NoAuth`][crate::account::auth::NoAuth] module.
46    ///
47    /// This authentication scheme provides no cryptographic authentication and only increments
48    /// the nonce if the account state has actually changed during transaction execution.
49    AuthNoAuth,
50    /// Exposes procedures from the
51    /// [`AuthNetworkAccount`][crate::account::auth::AuthNetworkAccount] module.
52    ///
53    /// This authentication scheme is intended for network-owned accounts. It rejects transactions
54    /// that executed a tx script or consumed input notes outside of fixed allowlists, as well as
55    /// transactions that do not consume an input note, create an output note, or change the
56    /// account before fee payment.
57    AuthNetworkAccount,
58    /// Exposes procedures from the
59    /// [`AuthTxFeeCollector`][crate::account::auth::AuthTxFeeCollector] module.
60    ///
61    /// This authentication scheme forwards the single asset of every consumed note into one P2ID
62    /// note, verifies a signature over the transaction summary and rejects any transaction that
63    /// changes the account's state.
64    AuthTxFeeCollector,
65    /// A non-standard authentication component, holding the account's authentication procedure.
66    CustomAuth(AccountProcedureRoot),
67    /// A non-standard, custom interface which exposes the contained procedures.
68    ///
69    /// Custom interface holds all procedures which are not part of some standard interface which is
70    /// used by this account.
71    Custom(Vec<AccountProcedureRoot>),
72}
73
74impl AccountComponentInterface {
75    /// Returns a string line with the name of the [AccountComponentInterface] enum variant.
76    ///
77    /// In case of a [AccountComponentInterface::Custom] along with the name of the enum variant
78    /// the vector of shortened hex representations of the used procedures is returned, e.g.
79    /// `Custom([0x6d93447, 0x0bf23d8])`.
80    pub fn name(&self) -> String {
81        match self {
82            AccountComponentInterface::BasicWallet => "Basic Wallet".to_string(),
83            AccountComponentInterface::NoteCreator => "Note Creator".to_string(),
84            AccountComponentInterface::FungibleFaucet => "Fungible Faucet".to_string(),
85            AccountComponentInterface::CodeInspection => "Code Inspection".to_string(),
86            AccountComponentInterface::Authority => "Authority".to_string(),
87            AccountComponentInterface::Ownable2Step => "Ownable2Step".to_string(),
88            AccountComponentInterface::RoleBasedAccessControl => {
89                "Role Based Access Control".to_string()
90            },
91            AccountComponentInterface::AuthSingleSig => "SingleSig".to_string(),
92            AccountComponentInterface::AuthMultisig => "Multisig".to_string(),
93            AccountComponentInterface::AuthMultisigSmart => "Multisig Smart".to_string(),
94            AccountComponentInterface::AuthGuardedMultisig => "Guarded Multisig".to_string(),
95            AccountComponentInterface::AuthNoAuth => "No Auth".to_string(),
96            AccountComponentInterface::AuthNetworkAccount => "Network Account Auth".to_string(),
97            AccountComponentInterface::AuthTxFeeCollector => "Tx Fee Collector Auth".to_string(),
98            AccountComponentInterface::CustomAuth(proc_root) => {
99                format!("Custom Auth({})", shortened_mast_root(proc_root))
100            },
101            AccountComponentInterface::Custom(proc_root_vec) => {
102                let result =
103                    proc_root_vec.iter().map(shortened_mast_root).collect::<Vec<_>>().join(", ");
104                format!("Custom([{result}])")
105            },
106        }
107    }
108
109    /// Returns true if this component interface is an authentication component.
110    pub fn is_auth_component(&self) -> bool {
111        matches!(
112            self,
113            AccountComponentInterface::AuthSingleSig
114                | AccountComponentInterface::AuthMultisig
115                | AccountComponentInterface::AuthMultisigSmart
116                | AccountComponentInterface::AuthGuardedMultisig
117                | AccountComponentInterface::AuthNoAuth
118                | AccountComponentInterface::AuthNetworkAccount
119                | AccountComponentInterface::AuthTxFeeCollector
120                | AccountComponentInterface::CustomAuth(_)
121        )
122    }
123}
124
125// HELPER FUNCTIONS
126// ================================================================================================
127
128/// Returns a shortened hex representation of the procedure's MAST root: the `0x` prefix followed
129/// by the first seven hex digits, e.g. `0x6d93447`.
130fn shortened_mast_root(proc_root: &AccountProcedureRoot) -> String {
131    proc_root.mast_root().to_hex()[..9].to_string()
132}