Skip to main content

miden_standards/account/
mod.rs

1pub mod access;
2pub mod auth;
3pub mod components;
4pub mod faucets;
5pub mod fees;
6pub mod inspection;
7pub mod interface;
8pub mod policies;
9pub mod upgrade;
10pub mod wallets;
11
12pub use inspection::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 `.masp` asset
60/// shipped by the `miden-standards` build script.
61///
62/// `$relative_path` is appended to `concat!(env!("OUT_DIR"), "/assets/components/")` and the
63/// resulting bytes are deserialized into a [`Package`], which is then wrapped into an
64/// [`AccountComponentCode`].
65///
66/// This macro is intended for use **inside the `miden-standards` crate only**: it relies on
67/// `env!("OUT_DIR")` resolving against `miden-standards`'s build script, which is where the
68/// `components` assets are written.
69///
70/// [`Package`]: miden_protocol::vm::Package
71/// [`AccountComponentCode`]: miden_protocol::account::component::AccountComponentCode
72///
73/// # Example
74/// ```ignore
75/// account_component_code!(BASIC_WALLET_CODE, "miden-standards-wallets-basic-wallet.masp");
76/// ```
77macro_rules! account_component_code {
78    ($name:ident, $relative_path:expr) => {
79        static $name: miden_protocol::utils::sync::LazyLock<
80            miden_protocol::account::component::AccountComponentCode,
81        > = miden_protocol::utils::sync::LazyLock::new(|| {
82            let bytes =
83                include_bytes!(concat!(env!("OUT_DIR"), "/assets/components/", $relative_path));
84            // These bytes are produced by this crate's build script and embedded in the binary.
85            let package = miden_protocol::vm::Package::read_from_bytes_trusted(bytes)
86                .expect("shipped account-component package failed to deserialize");
87            miden_protocol::account::component::AccountComponentCode::from(package)
88        });
89    };
90}
91
92pub(crate) use account_component_code;