miden-standards 0.17.0

Standards of the Miden protocol
Documentation
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use miden_protocol::account::AccountProcedureRoot;

// ACCOUNT COMPONENT INTERFACE
// ================================================================================================

/// The enum holding all possible account interfaces which could be loaded to some account.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccountComponentInterface {
    /// Exposes procedures from the [`BasicWallet`][crate::account::wallets::BasicWallet] module.
    BasicWallet,
    /// Exposes the `create_note` procedure from the
    /// [`NoteCreator`][crate::account::note_creator::NoteCreator] component.
    NoteCreator,
    /// Exposes procedures from the
    /// [`FungibleFaucet`][crate::account::faucets::FungibleFaucet] module.
    FungibleFaucet,
    /// Exposes procedures from the
    /// [`CodeInspection`][crate::account::inspection::CodeInspection] component.
    CodeInspection,
    /// Exposes procedures from the
    /// [`Authority`][crate::account::access::Authority] access component.
    Authority,
    /// Exposes procedures from the
    /// [`Ownable2Step`][crate::account::access::Ownable2Step] access component.
    Ownable2Step,
    /// Exposes procedures from the
    /// [`RoleBasedAccessControl`][crate::account::access::RoleBasedAccessControl] access
    /// component.
    RoleBasedAccessControl,
    /// Exposes procedures from the
    /// [`AuthSingleSig`][crate::account::auth::AuthSingleSig] module.
    AuthSingleSig,
    /// Exposes procedures from the
    /// [`AuthMultisig`][crate::account::auth::AuthMultisig] module.
    AuthMultisig,
    /// Exposes procedures from the
    /// [`AuthMultisigSmart`][crate::account::auth::AuthMultisigSmart] module.
    AuthMultisigSmart,
    /// Exposes procedures from the
    /// [`AuthGuardedMultisig`][crate::account::auth::AuthGuardedMultisig] module.
    AuthGuardedMultisig,
    /// Exposes procedures from the [`NoAuth`][crate::account::auth::NoAuth] module.
    ///
    /// This authentication scheme provides no cryptographic authentication and only increments
    /// the nonce if the account state has actually changed during transaction execution.
    AuthNoAuth,
    /// Exposes procedures from the
    /// [`AuthNetworkAccount`][crate::account::auth::AuthNetworkAccount] module.
    ///
    /// This authentication scheme is intended for network-owned accounts. It rejects transactions
    /// that executed a tx script or consumed input notes outside of fixed allowlists, as well as
    /// transactions that do not consume an input note, create an output note, or change the
    /// account before fee payment.
    AuthNetworkAccount,
    /// Exposes procedures from the
    /// [`AuthTxFeeCollector`][crate::account::auth::AuthTxFeeCollector] module.
    ///
    /// This authentication scheme forwards the single asset of every consumed note into one P2ID
    /// note, verifies a signature over the transaction summary and rejects any transaction that
    /// changes the account's state.
    AuthTxFeeCollector,
    /// A non-standard authentication component, holding the account's authentication procedure.
    CustomAuth(AccountProcedureRoot),
    /// A non-standard, custom interface which exposes the contained procedures.
    ///
    /// Custom interface holds all procedures which are not part of some standard interface which is
    /// used by this account.
    Custom(Vec<AccountProcedureRoot>),
}

impl AccountComponentInterface {
    /// Returns a string line with the name of the [AccountComponentInterface] enum variant.
    ///
    /// In case of a [AccountComponentInterface::Custom] along with the name of the enum variant
    /// the vector of shortened hex representations of the used procedures is returned, e.g.
    /// `Custom([0x6d93447, 0x0bf23d8])`.
    pub fn name(&self) -> String {
        match self {
            AccountComponentInterface::BasicWallet => "Basic Wallet".to_string(),
            AccountComponentInterface::NoteCreator => "Note Creator".to_string(),
            AccountComponentInterface::FungibleFaucet => "Fungible Faucet".to_string(),
            AccountComponentInterface::CodeInspection => "Code Inspection".to_string(),
            AccountComponentInterface::Authority => "Authority".to_string(),
            AccountComponentInterface::Ownable2Step => "Ownable2Step".to_string(),
            AccountComponentInterface::RoleBasedAccessControl => {
                "Role Based Access Control".to_string()
            },
            AccountComponentInterface::AuthSingleSig => "SingleSig".to_string(),
            AccountComponentInterface::AuthMultisig => "Multisig".to_string(),
            AccountComponentInterface::AuthMultisigSmart => "Multisig Smart".to_string(),
            AccountComponentInterface::AuthGuardedMultisig => "Guarded Multisig".to_string(),
            AccountComponentInterface::AuthNoAuth => "No Auth".to_string(),
            AccountComponentInterface::AuthNetworkAccount => "Network Account Auth".to_string(),
            AccountComponentInterface::AuthTxFeeCollector => "Tx Fee Collector Auth".to_string(),
            AccountComponentInterface::CustomAuth(proc_root) => {
                format!("Custom Auth({})", shortened_mast_root(proc_root))
            },
            AccountComponentInterface::Custom(proc_root_vec) => {
                let result =
                    proc_root_vec.iter().map(shortened_mast_root).collect::<Vec<_>>().join(", ");
                format!("Custom([{result}])")
            },
        }
    }

    /// Returns true if this component interface is an authentication component.
    pub fn is_auth_component(&self) -> bool {
        matches!(
            self,
            AccountComponentInterface::AuthSingleSig
                | AccountComponentInterface::AuthMultisig
                | AccountComponentInterface::AuthMultisigSmart
                | AccountComponentInterface::AuthGuardedMultisig
                | AccountComponentInterface::AuthNoAuth
                | AccountComponentInterface::AuthNetworkAccount
                | AccountComponentInterface::AuthTxFeeCollector
                | AccountComponentInterface::CustomAuth(_)
        )
    }
}

// HELPER FUNCTIONS
// ================================================================================================

/// Returns a shortened hex representation of the procedure's MAST root: the `0x` prefix followed
/// by the first seven hex digits, e.g. `0x6d93447`.
fn shortened_mast_root(proc_root: &AccountProcedureRoot) -> String {
    proc_root.mast_root().to_hex()[..9].to_string()
}