Skip to main content

miden_base_sys/bindings/
asset.rs

1//! Bindings for the protocol's asset-id accessors.
2//!
3//! These procedures decode the parts of an asset id — the issuing faucet, the asset class and the
4//! composition rule — without touching the account vault.
5
6use miden_stdlib_sys::{Felt, WordAligned};
7
8use super::types::{AccountId, AssetClass, AssetComposition, AssetId, RawAccountId, RawAssetClass};
9
10#[allow(improper_ctypes)]
11unsafe extern "C" {
12    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
13    #[link_name = "miden::protocol::asset::id_into_faucet_id"]
14    fn extern_asset_id_into_faucet_id(
15        asset_id_0: Felt,
16        asset_id_1: Felt,
17        asset_id_2: Felt,
18        asset_id_3: Felt,
19        ptr: *mut RawAccountId,
20    );
21    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
22    #[link_name = "miden::protocol::asset::id_into_asset_class"]
23    fn extern_asset_id_into_asset_class(
24        asset_id_0: Felt,
25        asset_id_1: Felt,
26        asset_id_2: Felt,
27        asset_id_3: Felt,
28        ptr: *mut RawAssetClass,
29    );
30    #[cfg_attr(target_family = "wasm", linkage = "extern_weak")]
31    #[link_name = "miden::protocol::asset::id_into_composition"]
32    fn extern_asset_id_into_composition(
33        asset_id_0: Felt,
34        asset_id_1: Felt,
35        asset_id_2: Felt,
36        asset_id_3: Felt,
37    ) -> Felt;
38}
39
40/// Returns the id of the faucet that issued the asset identified by `asset_id`.
41///
42/// The faucet id is read out of the asset id without being validated.
43pub fn id_into_faucet_id(asset_id: AssetId) -> AccountId {
44    unsafe {
45        let mut ret_area = WordAligned::new(::core::mem::MaybeUninit::<RawAccountId>::uninit());
46        let id = asset_id.inner;
47        extern_asset_id_into_faucet_id(id[0], id[1], id[2], id[3], ret_area.as_mut_ptr());
48        ret_area.into_inner().assume_init().into_account_id()
49    }
50}
51
52/// Returns the asset class of `asset_id`.
53pub fn id_into_asset_class(asset_id: AssetId) -> AssetClass {
54    unsafe {
55        let mut ret_area = WordAligned::new(::core::mem::MaybeUninit::<RawAssetClass>::uninit());
56        let id = asset_id.inner;
57        extern_asset_id_into_asset_class(id[0], id[1], id[2], id[3], ret_area.as_mut_ptr());
58        ret_area.into_inner().assume_init().into_asset_class()
59    }
60}
61
62/// Returns the composition rule encoded in `asset_id`.
63///
64/// # Panics
65///
66/// Panics if the asset id encodes a composition this SDK does not recognize.
67pub fn id_into_composition(asset_id: AssetId) -> AssetComposition {
68    let id = asset_id.inner;
69    let composition = unsafe { extern_asset_id_into_composition(id[0], id[1], id[2], id[3]) };
70    AssetComposition::try_from(composition).expect("unrecognized asset composition")
71}
72
73impl AssetId {
74    /// Returns the id of the faucet that issued this asset.
75    ///
76    /// The faucet id is read out of the asset id without being validated.
77    #[inline]
78    pub fn faucet_id(self) -> AccountId {
79        id_into_faucet_id(self)
80    }
81
82    /// Returns this asset's class, which distinguishes it from the other assets issued by the same
83    /// faucet.
84    #[inline]
85    pub fn asset_class(self) -> AssetClass {
86        id_into_asset_class(self)
87    }
88
89    /// Returns the composition rule encoded in this asset id.
90    ///
91    /// # Panics
92    ///
93    /// Panics if the asset id encodes a composition this SDK does not recognize.
94    #[inline]
95    pub fn composition(self) -> AssetComposition {
96        id_into_composition(self)
97    }
98}