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, Recipient};
7
8#[allow(improper_ctypes)]
9unsafe extern "C" {
10    #[link_name = "miden::input_note::get_assets_info"]
11    pub fn extern_input_note_get_assets_info(note_index: Felt, ptr: *mut (Word, Felt));
12
13    #[link_name = "miden::input_note::get_assets"]
14    pub fn extern_input_note_get_assets(dest_ptr: *mut Felt, note_index: Felt) -> usize;
15
16    #[link_name = "miden::input_note::get_recipient"]
17    pub fn extern_input_note_get_recipient(note_index: Felt, ptr: *mut Recipient);
18
19    #[link_name = "miden::input_note::get_metadata"]
20    pub fn extern_input_note_get_metadata(note_index: Felt, ptr: *mut Word);
21
22    #[link_name = "miden::input_note::get_sender"]
23    pub fn extern_input_note_get_sender(note_index: Felt, ptr: *mut AccountId);
24
25    #[link_name = "miden::input_note::get_inputs_info"]
26    pub fn extern_input_note_get_inputs_info(note_index: Felt, ptr: *mut (Word, Felt));
27
28    #[link_name = "miden::input_note::get_script_root"]
29    pub fn extern_input_note_get_script_root(note_index: Felt, ptr: *mut Word);
30
31    #[link_name = "miden::input_note::get_serial_number"]
32    pub 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 inputs stored in an input note.
42pub struct InputNoteInputsInfo {
43    pub commitment: Word,
44    pub num_inputs: 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: commitment.reverse(),
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        let mut recipient = ret_area.assume_init();
80        recipient.inner = recipient.inner.reverse();
81        recipient
82    }
83}
84
85/// Returns the metadata of the input note at `note_index`.
86pub fn get_metadata(note_index: NoteIdx) -> Word {
87    unsafe {
88        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
89        extern_input_note_get_metadata(note_index.inner, ret_area.as_mut_ptr());
90        ret_area.assume_init().reverse()
91    }
92}
93
94/// Returns the sender of the input note at `note_index`.
95pub fn get_sender(note_index: NoteIdx) -> AccountId {
96    unsafe {
97        let mut ret_area = ::core::mem::MaybeUninit::<AccountId>::uninit();
98        extern_input_note_get_sender(note_index.inner, ret_area.as_mut_ptr());
99        ret_area.assume_init()
100    }
101}
102
103/// Returns the inputs commitment and input count for the input note at `note_index`.
104pub fn get_inputs_info(note_index: NoteIdx) -> InputNoteInputsInfo {
105    unsafe {
106        let mut ret_area = ::core::mem::MaybeUninit::<(Word, Felt)>::uninit();
107        extern_input_note_get_inputs_info(note_index.inner, ret_area.as_mut_ptr());
108        let (commitment, num_inputs) = ret_area.assume_init();
109        InputNoteInputsInfo {
110            commitment: commitment.reverse(),
111            num_inputs,
112        }
113    }
114}
115
116/// Returns the script root of the input note at `note_index`.
117pub fn get_script_root(note_index: NoteIdx) -> Word {
118    unsafe {
119        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
120        extern_input_note_get_script_root(note_index.inner, ret_area.as_mut_ptr());
121        ret_area.assume_init().reverse()
122    }
123}
124
125/// Returns the serial number of the input note at `note_index`.
126pub fn get_serial_number(note_index: NoteIdx) -> Word {
127    unsafe {
128        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
129        extern_input_note_get_serial_number(note_index.inner, ret_area.as_mut_ptr());
130        ret_area.assume_init().reverse()
131    }
132}