miden_base_sys/bindings/
output_note.rs

1extern crate alloc;
2use alloc::vec::Vec;
3
4use miden_stdlib_sys::{Felt, Word};
5
6use super::types::{Asset, NoteIdx, NoteType, Recipient, Tag};
7
8#[allow(improper_ctypes)]
9unsafe extern "C" {
10    #[link_name = "miden::output_note::create"]
11    pub fn extern_output_note_create(
12        tag: Tag,
13        aux: Felt,
14        note_type: NoteType,
15        execution_hint: Felt,
16        recipient_f0: Felt,
17        recipient_f1: Felt,
18        recipient_f2: Felt,
19        recipient_f3: Felt,
20    ) -> NoteIdx;
21
22    #[link_name = "miden::output_note::add_asset"]
23    pub fn extern_output_note_add_asset(
24        asset_f0: Felt,
25        asset_f1: Felt,
26        asset_f2: Felt,
27        asset_f3: Felt,
28        note_idx: NoteIdx,
29    );
30
31    #[link_name = "miden::output_note::get_assets_info"]
32    pub fn extern_output_note_get_assets_info(note_index: Felt, ptr: *mut (Word, Felt));
33
34    #[link_name = "miden::output_note::get_assets"]
35    pub fn extern_output_note_get_assets(dest_ptr: *mut Felt, note_index: Felt) -> usize;
36
37    #[link_name = "miden::output_note::get_recipient"]
38    pub fn extern_output_note_get_recipient(note_index: Felt, ptr: *mut Recipient);
39
40    #[link_name = "miden::output_note::get_metadata"]
41    pub fn extern_output_note_get_metadata(note_index: Felt, ptr: *mut Word);
42}
43
44/// Creates a new output note and returns its index.
45pub fn create(
46    tag: Tag,
47    aux: Felt,
48    note_type: NoteType,
49    execution_hint: Felt,
50    recipient: Recipient,
51) -> NoteIdx {
52    unsafe {
53        extern_output_note_create(
54            tag,
55            aux,
56            note_type,
57            execution_hint,
58            recipient.inner[3],
59            recipient.inner[2],
60            recipient.inner[1],
61            recipient.inner[0],
62        )
63    }
64}
65
66/// Adds the asset to the output note specified by `note_idx`.
67pub fn add_asset(asset: Asset, note_idx: NoteIdx) {
68    unsafe {
69        extern_output_note_add_asset(
70            asset.inner[3],
71            asset.inner[2],
72            asset.inner[1],
73            asset.inner[0],
74            note_idx,
75        );
76    }
77}
78
79/// Contains summary information about the assets of an output note.
80pub struct OutputNoteAssetsInfo {
81    pub commitment: Word,
82    pub num_assets: Felt,
83}
84
85/// Retrieves the assets commitment and asset count for the output note at `note_index`.
86pub fn get_assets_info(note_index: NoteIdx) -> OutputNoteAssetsInfo {
87    unsafe {
88        let mut ret_area = ::core::mem::MaybeUninit::<(Word, Felt)>::uninit();
89        extern_output_note_get_assets_info(note_index.inner, ret_area.as_mut_ptr());
90        let (commitment, num_assets) = ret_area.assume_init();
91        OutputNoteAssetsInfo {
92            commitment: commitment.reverse(),
93            num_assets,
94        }
95    }
96}
97
98/// Returns the assets contained in the output note at `note_index`.
99pub fn get_assets(note_index: NoteIdx) -> Vec<Asset> {
100    const MAX_ASSETS: usize = 256;
101    let mut assets: Vec<Asset> = Vec::with_capacity(MAX_ASSETS);
102    let num_assets = unsafe {
103        let ptr = (assets.as_mut_ptr() as usize) / 4;
104        extern_output_note_get_assets(ptr as *mut Felt, note_index.inner)
105    };
106    unsafe {
107        assets.set_len(num_assets);
108    }
109    assets
110}
111
112/// Returns the recipient of the output note at `note_index`.
113pub fn get_recipient(note_index: NoteIdx) -> Recipient {
114    unsafe {
115        let mut ret_area = ::core::mem::MaybeUninit::<Recipient>::uninit();
116        extern_output_note_get_recipient(note_index.inner, ret_area.as_mut_ptr());
117        let recipient = ret_area.assume_init();
118        Recipient {
119            inner: recipient.inner.reverse(),
120        }
121    }
122}
123
124/// Returns the metadata of the output note at `note_index`.
125pub fn get_metadata(note_index: NoteIdx) -> Word {
126    unsafe {
127        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
128        extern_output_note_get_metadata(note_index.inner, ret_area.as_mut_ptr());
129        ret_area.assume_init().reverse()
130    }
131}