miden_base_sys/bindings/
active_account.rs

1use miden_stdlib_sys::{Felt, Word};
2
3use super::types::{AccountId, Asset};
4
5#[allow(improper_ctypes)]
6extern "C" {
7    #[link_name = "miden::active_account::get_id"]
8    fn extern_active_account_get_id(ptr: *mut AccountId);
9    #[link_name = "miden::active_account::get_nonce"]
10    fn extern_active_account_get_nonce() -> Felt;
11    #[link_name = "miden::active_account::get_initial_commitment"]
12    fn extern_active_account_get_initial_commitment(ptr: *mut Word);
13    #[link_name = "miden::active_account::compute_commitment"]
14    fn extern_active_account_compute_commitment(ptr: *mut Word);
15    #[link_name = "miden::active_account::get_code_commitment"]
16    fn extern_active_account_get_code_commitment(ptr: *mut Word);
17    #[link_name = "miden::active_account::get_initial_storage_commitment"]
18    fn extern_active_account_get_initial_storage_commitment(ptr: *mut Word);
19    #[link_name = "miden::active_account::compute_storage_commitment"]
20    fn extern_active_account_compute_storage_commitment(ptr: *mut Word);
21    #[link_name = "miden::active_account::get_balance"]
22    fn extern_active_account_get_balance(faucet_id_prefix: Felt, faucet_id_suffix: Felt) -> Felt;
23    #[link_name = "miden::active_account::get_initial_balance"]
24    fn extern_active_account_get_initial_balance(
25        faucet_id_prefix: Felt,
26        faucet_id_suffix: Felt,
27    ) -> Felt;
28    #[link_name = "miden::active_account::has_non_fungible_asset"]
29    fn extern_active_account_has_non_fungible_asset(
30        asset_3: Felt,
31        asset_2: Felt,
32        asset_1: Felt,
33        asset_0: Felt,
34    ) -> Felt;
35    #[link_name = "miden::active_account::get_initial_vault_root"]
36    fn extern_active_account_get_initial_vault_root(ptr: *mut Word);
37    #[link_name = "miden::active_account::get_vault_root"]
38    fn extern_active_account_get_vault_root(ptr: *mut Word);
39    #[link_name = "miden::active_account::get_num_procedures"]
40    fn extern_active_account_get_num_procedures() -> Felt;
41    #[link_name = "miden::active_account::get_procedure_root"]
42    fn extern_active_account_get_procedure_root(index: Felt, ptr: *mut Word);
43    #[link_name = "miden::active_account::has_procedure"]
44    fn extern_active_account_has_procedure(
45        proc_root_3: Felt,
46        proc_root_2: Felt,
47        proc_root_1: Felt,
48        proc_root_0: Felt,
49    ) -> Felt;
50}
51
52/// Returns the account ID of the active account.
53pub fn get_id() -> AccountId {
54    unsafe {
55        let mut ret_area = ::core::mem::MaybeUninit::<AccountId>::uninit();
56        extern_active_account_get_id(ret_area.as_mut_ptr());
57        ret_area.assume_init()
58    }
59}
60
61/// Returns the nonce of the active account.
62#[inline]
63pub fn get_nonce() -> Felt {
64    unsafe { extern_active_account_get_nonce() }
65}
66
67/// Returns the active account commitment at the beginning of the transaction.
68#[inline]
69pub fn get_initial_commitment() -> Word {
70    unsafe {
71        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
72        extern_active_account_get_initial_commitment(ret_area.as_mut_ptr());
73        ret_area.assume_init().reverse()
74    }
75}
76
77/// Computes and returns the commitment of the current account data.
78#[inline]
79pub fn compute_commitment() -> Word {
80    unsafe {
81        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
82        extern_active_account_compute_commitment(ret_area.as_mut_ptr());
83        ret_area.assume_init().reverse()
84    }
85}
86
87/// Returns the code commitment of the active account.
88#[inline]
89pub fn get_code_commitment() -> Word {
90    unsafe {
91        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
92        extern_active_account_get_code_commitment(ret_area.as_mut_ptr());
93        ret_area.assume_init().reverse()
94    }
95}
96
97/// Returns the initial storage commitment of the active account.
98#[inline]
99pub fn get_initial_storage_commitment() -> Word {
100    unsafe {
101        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
102        extern_active_account_get_initial_storage_commitment(ret_area.as_mut_ptr());
103        ret_area.assume_init().reverse()
104    }
105}
106
107/// Computes the latest storage commitment of the active account.
108#[inline]
109pub fn compute_storage_commitment() -> Word {
110    unsafe {
111        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
112        extern_active_account_compute_storage_commitment(ret_area.as_mut_ptr());
113        ret_area.assume_init().reverse()
114    }
115}
116
117/// Returns the balance of the fungible asset identified by `faucet_id`.
118///
119/// # Panics
120///
121/// Propagates kernel errors if the referenced asset is non-fungible or the
122/// account vault invariants are violated.
123pub fn get_balance(faucet_id: AccountId) -> Felt {
124    unsafe { extern_active_account_get_balance(faucet_id.prefix, faucet_id.suffix) }
125}
126
127/// Returns the initial balance of the fungible asset identified by `faucet_id`.
128#[inline]
129pub fn get_initial_balance(faucet_id: AccountId) -> Felt {
130    unsafe { extern_active_account_get_initial_balance(faucet_id.prefix, faucet_id.suffix) }
131}
132
133/// Returns `true` if the active account vault currently contains the specified non-fungible asset.
134#[inline]
135pub fn has_non_fungible_asset(asset: Asset) -> bool {
136    unsafe {
137        extern_active_account_has_non_fungible_asset(
138            asset.inner[3],
139            asset.inner[2],
140            asset.inner[1],
141            asset.inner[0],
142        ) != Felt::from_u32(0)
143    }
144}
145
146/// Returns the vault root of the active account at the beginning of the transaction.
147#[inline]
148pub fn get_initial_vault_root() -> Word {
149    unsafe {
150        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
151        extern_active_account_get_initial_vault_root(ret_area.as_mut_ptr());
152        ret_area.assume_init().reverse()
153    }
154}
155
156/// Returns the current vault root of the active account.
157#[inline]
158pub fn get_vault_root() -> Word {
159    unsafe {
160        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
161        extern_active_account_get_vault_root(ret_area.as_mut_ptr());
162        ret_area.assume_init().reverse()
163    }
164}
165
166/// Returns the number of procedures exported by the active account.
167#[inline]
168pub fn get_num_procedures() -> Felt {
169    unsafe { extern_active_account_get_num_procedures() }
170}
171
172/// Returns the procedure root for the procedure at `index`.
173#[inline]
174pub fn get_procedure_root(index: u8) -> Word {
175    unsafe {
176        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
177        extern_active_account_get_procedure_root(index.into(), ret_area.as_mut_ptr());
178        ret_area.assume_init().reverse()
179    }
180}
181
182/// Returns `true` if the procedure identified by `proc_root` exists on the active account.
183#[inline]
184pub fn has_procedure(proc_root: Word) -> bool {
185    unsafe {
186        extern_active_account_has_procedure(proc_root[3], proc_root[2], proc_root[1], proc_root[0])
187            != Felt::from_u32(0)
188    }
189}