Skip to main content

miden_base_sys/bindings/
input_note.rs

1extern crate alloc;
2use alloc::vec::Vec;
3
4use miden_stdlib_sys::{Felt, Word};
5
6use super::types::{AccountId, Asset, NoteIdx, NoteMetadata, RawAccountId, Recipient};
7
8#[allow(improper_ctypes)]
9unsafe extern "C" {
10    #[link_name = "miden::protocol::input_note::get_assets_info"]
11    fn extern_input_note_get_assets_info(note_index: Felt, ptr: *mut (Word, Felt));
12
13    #[link_name = "miden::protocol::input_note::get_assets"]
14    fn extern_input_note_get_assets(dest_ptr: *mut Felt, note_index: Felt) -> usize;
15
16    #[link_name = "miden::protocol::input_note::get_recipient"]
17    fn extern_input_note_get_recipient(note_index: Felt, ptr: *mut Recipient);
18
19    #[link_name = "miden::protocol::input_note::get_metadata"]
20    fn extern_input_note_get_metadata(note_index: Felt, ptr: *mut NoteMetadata);
21
22    #[link_name = "miden::protocol::input_note::get_sender"]
23    fn extern_input_note_get_sender(note_index: Felt, ptr: *mut RawAccountId);
24
25    #[link_name = "miden::protocol::input_note::get_storage_info"]
26    fn extern_input_note_get_storage_info(note_index: Felt, ptr: *mut (Word, Felt));
27
28    #[link_name = "miden::protocol::input_note::get_script_root"]
29    fn extern_input_note_get_script_root(note_index: Felt, ptr: *mut Word);
30
31    #[link_name = "miden::protocol::input_note::get_serial_number"]
32    fn extern_input_note_get_serial_number(note_index: Felt, ptr: *mut Word);
33}
34
35/// Contains summary information about the assets stored in an input note.
36pub struct InputNoteAssetsInfo {
37    pub commitment: Word,
38    pub num_assets: Felt,
39}
40
41/// Contains summary information about the storage stored in an input note.
42pub struct InputNoteStorageInfo {
43    pub commitment: Word,
44    pub num_storage_items: Felt,
45}
46
47/// Returns the assets commitment and asset count for the input note at `note_index`.
48pub fn get_assets_info(note_index: NoteIdx) -> InputNoteAssetsInfo {
49    unsafe {
50        let mut ret_area = ::core::mem::MaybeUninit::<(Word, Felt)>::uninit();
51        extern_input_note_get_assets_info(note_index.inner, ret_area.as_mut_ptr());
52        let (commitment, num_assets) = ret_area.assume_init();
53        InputNoteAssetsInfo {
54            commitment,
55            num_assets,
56        }
57    }
58}
59
60/// Returns the assets contained in the input note at `note_index`.
61pub fn get_assets(note_index: NoteIdx) -> Vec<Asset> {
62    const MAX_ASSETS: usize = 256;
63    let mut assets: Vec<Asset> = Vec::with_capacity(MAX_ASSETS);
64    let num_assets = unsafe {
65        let ptr = (assets.as_mut_ptr() as usize) / 4;
66        extern_input_note_get_assets(ptr as *mut Felt, note_index.inner)
67    };
68    unsafe {
69        assets.set_len(num_assets);
70    }
71    assets
72}
73
74/// Returns the recipient of the input note at `note_index`.
75pub fn get_recipient(note_index: NoteIdx) -> Recipient {
76    unsafe {
77        let mut ret_area = ::core::mem::MaybeUninit::<Recipient>::uninit();
78        extern_input_note_get_recipient(note_index.inner, ret_area.as_mut_ptr());
79        ret_area.assume_init()
80    }
81}
82
83/// Returns the attachment and metadata header of the input note at `note_index`.
84pub fn get_metadata(note_index: NoteIdx) -> NoteMetadata {
85    unsafe {
86        let mut ret_area = ::core::mem::MaybeUninit::<NoteMetadata>::uninit();
87        extern_input_note_get_metadata(note_index.inner, ret_area.as_mut_ptr());
88        ret_area.assume_init()
89    }
90}
91
92/// Returns the sender of the input note at `note_index`.
93pub fn get_sender(note_index: NoteIdx) -> AccountId {
94    unsafe {
95        let mut ret_area = ::core::mem::MaybeUninit::<RawAccountId>::uninit();
96        extern_input_note_get_sender(note_index.inner, ret_area.as_mut_ptr());
97        ret_area.assume_init().into_account_id()
98    }
99}
100
101/// Returns the storage commitment and storage item count for the input note at `note_index`.
102pub fn get_storage_info(note_index: NoteIdx) -> InputNoteStorageInfo {
103    unsafe {
104        let mut ret_area = ::core::mem::MaybeUninit::<(Word, Felt)>::uninit();
105        extern_input_note_get_storage_info(note_index.inner, ret_area.as_mut_ptr());
106        let (commitment, num_storage_items) = ret_area.assume_init();
107        InputNoteStorageInfo {
108            commitment,
109            num_storage_items,
110        }
111    }
112}
113
114/// Returns the script root of the input note at `note_index`.
115pub fn get_script_root(note_index: NoteIdx) -> Word {
116    unsafe {
117        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
118        extern_input_note_get_script_root(note_index.inner, ret_area.as_mut_ptr());
119        ret_area.assume_init()
120    }
121}
122
123/// Returns the serial number of the input note at `note_index`.
124pub fn get_serial_number(note_index: NoteIdx) -> Word {
125    unsafe {
126        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
127        extern_input_note_get_serial_number(note_index.inner, ret_area.as_mut_ptr());
128        ret_area.assume_init()
129    }
130}