Skip to main content

miden_standards/account/
mod.rs

1use super::auth_method::AuthMethod;
2
3pub mod access;
4pub mod auth;
5pub mod components;
6pub mod faucets;
7pub mod interface;
8pub mod metadata;
9pub mod policies;
10pub mod wallets;
11
12pub use metadata::AccountBuilderSchemaCommitmentExt;
13
14/// Macro to simplify the creation of static procedure root constants.
15///
16/// This macro generates a `LazyLock<AccountProcedureRoot>` static variable that lazily initializes
17/// the procedure root of a procedure from an [`AccountComponentCode`].
18///
19/// The full procedure path is constructed by concatenating `$component_name` and `$proc_name`
20/// with `::` as separator (i.e. `"{component_name}::{proc_name}"`).
21///
22/// Note: This macro references exported types from `miden_protocol`, so your crate must
23/// include `miden_protocol` as a dependency. The expanded code uses `::alloc::format!`, so
24/// downstream callers must also have `extern crate alloc;` (or otherwise expose `alloc` at the
25/// crate root) - this is automatic in `std`-linked binaries.
26///
27/// # Arguments
28/// * `$name` - The name of the static variable to create
29/// * `$component_name` - The name of the component (e.g. `BasicWallet::NAME`)
30/// * `$proc_name` - The short name of the procedure (e.g. `"receive_asset"`)
31/// * `$component_code` - An expression evaluating to `&AccountComponentCode` (or any type coercible
32///   via `Deref` such as `&LazyLock<AccountComponentCode>`).
33///
34/// [`AccountComponentCode`]: miden_protocol::account::AccountComponentCode
35///
36/// # Example
37/// ```ignore
38/// procedure_root!(
39///     BASIC_WALLET_RECEIVE_ASSET,
40///     BasicWallet::NAME,
41///     BasicWallet::RECEIVE_ASSET_PROC_NAME,
42///     BasicWallet::code()
43/// );
44/// ```
45#[macro_export]
46macro_rules! procedure_root {
47    ($name:ident, $component_name:expr, $proc_name:expr, $component_code:expr) => {
48        static $name: miden_protocol::utils::sync::LazyLock<
49            miden_protocol::account::AccountProcedureRoot,
50        > = miden_protocol::utils::sync::LazyLock::new(|| {
51            let full_path = ::alloc::format!("{}::{}", $component_name, $proc_name);
52            let code: &miden_protocol::account::AccountComponentCode = $component_code;
53            code.get_procedure_root_by_path(full_path.as_str())
54                .unwrap_or_else(|| panic!("component should contain procedure '{}'", full_path))
55        });
56    };
57}
58
59/// Macro to declare a static `LazyLock<AccountComponentCode>` initialized from a `.masl` asset
60/// shipped by the `miden-standards` build script.
61///
62/// `$relative_path` is appended to `concat!(env!("OUT_DIR"), "/assets/account_components/")` and
63/// the resulting bytes are deserialized via [`Library::read_from_bytes`].
64///
65/// This macro is intended for use **inside the `miden-standards` crate only**: it relies on
66/// `env!("OUT_DIR")` resolving against `miden-standards`'s build script, which is where the
67/// `account_components` assets are written.
68///
69/// [`Library::read_from_bytes`]: miden_protocol::assembly::Library::read_from_bytes
70///
71/// # Example
72/// ```ignore
73/// account_component_code!(BASIC_WALLET_CODE, "wallets/basic_wallet.masl");
74/// ```
75macro_rules! account_component_code {
76    ($name:ident, $relative_path:expr) => {
77        static $name: miden_protocol::utils::sync::LazyLock<
78            miden_protocol::account::component::AccountComponentCode,
79        > = miden_protocol::utils::sync::LazyLock::new(|| {
80            let bytes = include_bytes!(concat!(
81                env!("OUT_DIR"),
82                "/assets/account_components/",
83                $relative_path
84            ));
85            let library = <miden_protocol::assembly::Library as miden_protocol::utils::serde::Deserializable>::read_from_bytes(bytes)
86                .expect("Shipped library failed to deserialize: {err}");
87            miden_protocol::account::component::AccountComponentCode::from(library)
88        });
89    };
90}
91
92pub(crate) use account_component_code;