miden_base_sys/bindings/
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::account::get_id"]
8    pub fn extern_account_get_id(ptr: *mut AccountId);
9    #[link_name = "miden::account::remove_asset"]
10    pub fn extern_account_remove_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut Asset);
11    #[link_name = "miden::account::get_nonce"]
12    pub fn extern_account_get_nonce() -> Felt;
13    #[link_name = "miden::account::incr_nonce"]
14    pub fn extern_account_incr_nonce() -> Felt;
15    #[link_name = "miden::account::get_initial_commitment"]
16    pub fn extern_account_get_initial_commitment(ptr: *mut Word);
17    #[link_name = "miden::account::compute_current_commitment"]
18    pub fn extern_account_compute_current_commitment(ptr: *mut Word);
19    #[link_name = "miden::account::compute_delta_commitment"]
20    pub fn extern_account_compute_delta_commitment(ptr: *mut Word);
21    // Resolved via stub rlib at core Wasm link time
22    #[link_name = "miden::account::add_asset"]
23    pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut Asset);
24    #[link_name = "miden::account::get_balance"]
25    pub fn extern_account_get_balance(faucet_id_prefix: Felt, faucet_id_suffix: Felt) -> Felt;
26}
27
28/// Get the account ID of the currently executing note account.
29pub fn get_id() -> AccountId {
30    unsafe {
31        let mut ret_area = ::core::mem::MaybeUninit::<AccountId>::uninit();
32
33        // The MASM procedure returns the account ID on the stack.
34        // Inputs:  []
35        // Outputs: [acct_id_prefix, acct_id_suffix]
36        extern_account_get_id(ret_area.as_mut_ptr());
37        ret_area.assume_init()
38    }
39}
40
41/// Add the specified asset to the vault.
42/// Returns the final asset in the account vault defined as follows: If asset is
43/// a non-fungible asset, then returns the same as asset. If asset is a
44/// fungible asset, then returns the total fungible asset in the account
45/// vault after asset was added to it.
46///
47/// Panics:
48/// - If the asset is not valid.
49/// - If the total value of two fungible assets is greater than or equal to 2^63.
50/// - If the vault already contains the same non-fungible asset.
51pub fn add_asset(asset: Asset) -> Asset {
52    unsafe {
53        let mut ret_area = ::core::mem::MaybeUninit::<Asset>::uninit();
54        extern_account_add_asset(
55            asset.inner[3],
56            asset.inner[2],
57            asset.inner[1],
58            asset.inner[0],
59            ret_area.as_mut_ptr(),
60        );
61        ret_area.assume_init()
62    }
63}
64
65/// Remove the specified asset from the vault.
66///
67/// Panics:
68/// - The fungible asset is not found in the vault.
69/// - The amount of the fungible asset in the vault is less than the amount to be removed.
70/// - The non-fungible asset is not found in the vault.
71pub fn remove_asset(asset: Asset) -> Asset {
72    unsafe {
73        let mut ret_area = ::core::mem::MaybeUninit::<Asset>::uninit();
74        extern_account_remove_asset(
75            asset.inner[3],
76            asset.inner[2],
77            asset.inner[1],
78            asset.inner[0],
79            ret_area.as_mut_ptr(),
80        );
81        ret_area.assume_init().reverse()
82    }
83}
84
85/// Returns the current account nonce.
86#[inline]
87pub fn get_nonce() -> Felt {
88    unsafe { extern_account_get_nonce() }
89}
90
91/// Increments the account nonce by one and return the new nonce
92#[inline]
93pub fn incr_nonce() -> Felt {
94    unsafe { extern_account_incr_nonce() }
95}
96
97/// Returns the native account commitment at the beginning of the transaction.
98#[inline]
99pub fn get_initial_commitment() -> Word {
100    unsafe {
101        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
102        extern_account_get_initial_commitment(ret_area.as_mut_ptr());
103        ret_area.assume_init().reverse()
104    }
105}
106
107/// Computes and returns the commitment of the current account data.
108#[inline]
109pub fn compute_current_commitment() -> Word {
110    unsafe {
111        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
112        extern_account_compute_current_commitment(ret_area.as_mut_ptr());
113        ret_area.assume_init().reverse()
114    }
115}
116
117/// Computes and returns the commitment to the native account's delta for this transaction.
118#[inline]
119pub fn compute_delta_commitment() -> Word {
120    unsafe {
121        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
122        extern_account_compute_delta_commitment(ret_area.as_mut_ptr());
123        ret_area.assume_init().reverse()
124    }
125}
126
127/// Returns the balance of the fungible asset identified by `faucet_id`.
128///
129/// # Panics
130///
131/// Propagates kernel errors if the referenced asset is non-fungible or the
132/// account vault invariants are violated.
133pub fn get_balance(faucet_id: AccountId) -> Felt {
134    unsafe { extern_account_get_balance(faucet_id.prefix, faucet_id.suffix) }
135}