miden_standards/account/interface/mod.rs
1use alloc::vec::Vec;
2
3use miden_protocol::account::AccountId;
4
5#[cfg(test)]
6mod test;
7
8mod component;
9pub use component::AccountComponentInterface;
10
11mod extension;
12pub use extension::{AccountComponentInterfaceExt, AccountInterfaceExt};
13
14// ACCOUNT INTERFACE
15// ================================================================================================
16
17/// An [`AccountInterface`] describes the exported, callable procedures of an account.
18pub struct AccountInterface {
19 account_id: AccountId,
20 components: Vec<AccountComponentInterface>,
21}
22
23// ------------------------------------------------------------------------------------------------
24/// Constructors and public accessors
25impl AccountInterface {
26 // CONSTRUCTORS
27 // --------------------------------------------------------------------------------------------
28
29 /// Creates a new [`AccountInterface`] instance from the provided account ID and account
30 /// component interfaces.
31 ///
32 /// # Panics
33 ///
34 /// Panics if `components` does not contain exactly one auth component. Every account installs
35 /// a single auth component. Zero or multiple auth components is a malformed account.
36 pub fn new(account_id: AccountId, components: Vec<AccountComponentInterface>) -> Self {
37 let auth_count = components.iter().filter(|c| c.is_auth_component()).count();
38 assert_eq!(
39 auth_count, 1,
40 "account interface must contain exactly one auth component, found {auth_count}"
41 );
42 Self { account_id, components }
43 }
44
45 // PUBLIC ACCESSORS
46 // --------------------------------------------------------------------------------------------
47
48 /// Returns a reference to the account ID.
49 pub fn id(&self) -> &AccountId {
50 &self.account_id
51 }
52
53 /// Returns `true` if the reference account is a private account, `false` otherwise.
54 pub fn is_private(&self) -> bool {
55 self.account_id.is_private()
56 }
57
58 /// Returns true if the reference account is a public account, `false` otherwise.
59 pub fn is_public(&self) -> bool {
60 self.account_id.is_public()
61 }
62
63 /// Returns a reference to the set of used component interfaces.
64 pub fn components(&self) -> &Vec<AccountComponentInterface> {
65 &self.components
66 }
67
68 /// Returns a reference to the single auth component installed on this account.
69 ///
70 /// Every account installs exactly one auth component (validated in [`Self::new`]).
71 pub fn auth_component(&self) -> &AccountComponentInterface {
72 self.components
73 .iter()
74 .find(|c| c.is_auth_component())
75 .expect("AccountInterface invariant: exactly one auth component present")
76 }
77}