miden_base_sys/bindings/
tx.rs

1use miden_stdlib_sys::{Felt, Word};
2
3use super::types::{Asset, NoteIdx, NoteType, Recipient, Tag};
4
5#[allow(improper_ctypes)]
6extern "C" {
7    #[link_name = "miden::tx::create_note"]
8    pub fn extern_tx_create_note(
9        tag: Tag,
10        aux: Felt,
11        note_type: NoteType,
12        execution_hint: Felt,
13        recipient_f0: Felt,
14        recipient_f1: Felt,
15        recipient_f2: Felt,
16        recipient_f3: Felt,
17    ) -> NoteIdx;
18
19    #[link_name = "miden::tx::add_asset_to_note"]
20    pub fn extern_tx_add_asset_to_note(
21        asset_f0: Felt,
22        asset_f1: Felt,
23        asset_f2: Felt,
24        asset_f3: Felt,
25        note_idx: NoteIdx,
26        result: *mut (Asset, NoteIdx),
27    );
28
29    #[link_name = "miden::tx::get_block_number"]
30    pub fn extern_tx_get_block_number() -> Felt;
31
32    #[link_name = "miden::tx::get_input_notes_commitment"]
33    pub fn extern_tx_get_input_notes_commitment(ptr: *mut Word);
34
35    #[link_name = "miden::tx::get_output_notes_commitment"]
36    pub fn extern_tx_get_output_notes_commitment(ptr: *mut Word);
37}
38
39/// Creates a new note.  asset is the asset to be included in the note.  tag is
40/// the tag to be included in the note.  recipient is the recipient of the note.
41/// Returns the id of the created note.
42pub fn create_note(
43    tag: Tag,
44    aux: Felt,
45    note_type: NoteType,
46    execution_hint: Felt,
47    recipient: Recipient,
48) -> NoteIdx {
49    unsafe {
50        extern_tx_create_note(
51            tag,
52            aux,
53            note_type,
54            execution_hint,
55            recipient.inner[3],
56            recipient.inner[2],
57            recipient.inner[1],
58            recipient.inner[0],
59        )
60    }
61}
62
63/// Adds the asset to the note specified by the index.
64///
65/// # Arguments
66/// * `asset` - The asset to be added to the note
67/// * `note_idx` - The index of the note to which the asset will be added
68///
69/// # Returns
70/// A tuple containing the same asset and note_idx
71pub fn add_asset_to_note(asset: Asset, note_idx: NoteIdx) -> (Asset, NoteIdx) {
72    unsafe {
73        let mut ret_area = ::core::mem::MaybeUninit::<(Asset, NoteIdx)>::uninit();
74        extern_tx_add_asset_to_note(
75            asset.inner[3],
76            asset.inner[2],
77            asset.inner[1],
78            asset.inner[0],
79            note_idx,
80            ret_area.as_mut_ptr(),
81        );
82
83        let (asset, note_idx) = ret_area.assume_init();
84        (asset.reverse(), note_idx)
85    }
86}
87
88/// Returns the current block number.
89pub fn get_block_number() -> Felt {
90    unsafe { extern_tx_get_block_number() }
91}
92
93/// Returns the input notes commitment digest.
94pub fn get_input_notes_commitment() -> Word {
95    unsafe {
96        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
97        extern_tx_get_input_notes_commitment(ret_area.as_mut_ptr());
98        ret_area.assume_init().reverse()
99    }
100}
101
102/// Returns the output notes commitment digest.
103pub fn get_output_notes_commitment() -> Word {
104    unsafe {
105        let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
106        extern_tx_get_output_notes_commitment(ret_area.as_mut_ptr());
107        ret_area.assume_init().reverse()
108    }
109}