extern crate alloc;
use alloc::vec::Vec;
use miden_stdlib_sys::{Felt, Word};
use super::types::{AccountId, Asset, NoteIdx, NoteMetadata, RawAccountId, Recipient};
#[allow(improper_ctypes)]
unsafe extern "C" {
#[link_name = "miden::protocol::input_note::get_assets_info"]
fn extern_input_note_get_assets_info(note_index: Felt, ptr: *mut (Word, Felt));
#[link_name = "miden::protocol::input_note::get_assets"]
fn extern_input_note_get_assets(dest_ptr: *mut Felt, note_index: Felt) -> usize;
#[link_name = "miden::protocol::input_note::get_recipient"]
fn extern_input_note_get_recipient(note_index: Felt, ptr: *mut Recipient);
#[link_name = "miden::protocol::input_note::get_metadata"]
fn extern_input_note_get_metadata(note_index: Felt, ptr: *mut NoteMetadata);
#[link_name = "miden::protocol::input_note::get_sender"]
fn extern_input_note_get_sender(note_index: Felt, ptr: *mut RawAccountId);
#[link_name = "miden::protocol::input_note::get_storage_info"]
fn extern_input_note_get_storage_info(note_index: Felt, ptr: *mut (Word, Felt));
#[link_name = "miden::protocol::input_note::get_script_root"]
fn extern_input_note_get_script_root(note_index: Felt, ptr: *mut Word);
#[link_name = "miden::protocol::input_note::get_serial_number"]
fn extern_input_note_get_serial_number(note_index: Felt, ptr: *mut Word);
}
pub struct InputNoteAssetsInfo {
pub commitment: Word,
pub num_assets: Felt,
}
pub struct InputNoteStorageInfo {
pub commitment: Word,
pub num_storage_items: Felt,
}
pub fn get_assets_info(note_index: NoteIdx) -> InputNoteAssetsInfo {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<(Word, Felt)>::uninit();
extern_input_note_get_assets_info(note_index.inner, ret_area.as_mut_ptr());
let (commitment, num_assets) = ret_area.assume_init();
InputNoteAssetsInfo {
commitment,
num_assets,
}
}
}
pub fn get_assets(note_index: NoteIdx) -> Vec<Asset> {
const MAX_ASSETS: usize = 256;
let mut assets: Vec<Asset> = Vec::with_capacity(MAX_ASSETS);
let num_assets = unsafe {
let ptr = (assets.as_mut_ptr() as usize) / 4;
extern_input_note_get_assets(ptr as *mut Felt, note_index.inner)
};
unsafe {
assets.set_len(num_assets);
}
assets
}
pub fn get_recipient(note_index: NoteIdx) -> Recipient {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<Recipient>::uninit();
extern_input_note_get_recipient(note_index.inner, ret_area.as_mut_ptr());
ret_area.assume_init()
}
}
pub fn get_metadata(note_index: NoteIdx) -> NoteMetadata {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<NoteMetadata>::uninit();
extern_input_note_get_metadata(note_index.inner, ret_area.as_mut_ptr());
ret_area.assume_init()
}
}
pub fn get_sender(note_index: NoteIdx) -> AccountId {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<RawAccountId>::uninit();
extern_input_note_get_sender(note_index.inner, ret_area.as_mut_ptr());
ret_area.assume_init().into_account_id()
}
}
pub fn get_storage_info(note_index: NoteIdx) -> InputNoteStorageInfo {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<(Word, Felt)>::uninit();
extern_input_note_get_storage_info(note_index.inner, ret_area.as_mut_ptr());
let (commitment, num_storage_items) = ret_area.assume_init();
InputNoteStorageInfo {
commitment,
num_storage_items,
}
}
}
pub fn get_script_root(note_index: NoteIdx) -> Word {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
extern_input_note_get_script_root(note_index.inner, ret_area.as_mut_ptr());
ret_area.assume_init()
}
}
pub fn get_serial_number(note_index: NoteIdx) -> Word {
unsafe {
let mut ret_area = ::core::mem::MaybeUninit::<Word>::uninit();
extern_input_note_get_serial_number(note_index.inner, ret_area.as_mut_ptr());
ret_area.assume_init()
}
}