miden-base-sys 0.12.0

Miden rollup Rust bingings and MASM library
Documentation
extern crate alloc;
use alloc::vec::Vec;

use miden_stdlib_sys::{Felt, Word};

use super::types::{Asset, NoteIdx, NoteMetadata, NoteType, Recipient, Tag};

#[allow(improper_ctypes)]
unsafe extern "C" {
    #[link_name = "miden::protocol::output_note::create"]
    pub fn extern_output_note_create(
        tag: Tag,
        note_type: NoteType,
        recipient_f0: Felt,
        recipient_f1: Felt,
        recipient_f2: Felt,
        recipient_f3: Felt,
    ) -> NoteIdx;

    #[link_name = "miden::protocol::output_note::add_asset"]
    pub fn extern_output_note_add_asset(
        asset_key_f0: Felt,
        asset_key_f1: Felt,
        asset_key_f2: Felt,
        asset_key_f3: Felt,
        asset_value_f0: Felt,
        asset_value_f1: Felt,
        asset_value_f2: Felt,
        asset_value_f3: Felt,
        note_idx: NoteIdx,
    );

    #[link_name = "miden::protocol::output_note::get_assets_info"]
    pub fn extern_output_note_get_assets_info(note_index: Felt, ptr: *mut (Word, Felt));

    #[link_name = "miden::protocol::output_note::get_assets"]
    pub fn extern_output_note_get_assets(dest_ptr: *mut Felt, note_index: Felt) -> usize;

    #[link_name = "miden::protocol::output_note::get_recipient"]
    pub fn extern_output_note_get_recipient(note_index: Felt, ptr: *mut Recipient);

    #[link_name = "miden::protocol::output_note::get_metadata"]
    pub fn extern_output_note_get_metadata(note_index: Felt, ptr: *mut NoteMetadata);

    #[link_name = "miden::protocol::output_note::set_attachment"]
    pub fn extern_output_note_set_attachment(
        note_idx: NoteIdx,
        attachment_scheme: Felt,
        attachment_kind: Felt,
        attachment_f0: Felt,
        attachment_f1: Felt,
        attachment_f2: Felt,
        attachment_f3: Felt,
    );

    #[link_name = "miden::protocol::output_note::set_word_attachment"]
    pub fn extern_output_note_set_word_attachment(
        note_idx: NoteIdx,
        attachment_scheme: Felt,
        attachment_f0: Felt,
        attachment_f1: Felt,
        attachment_f2: Felt,
        attachment_f3: Felt,
    );

    #[link_name = "miden::protocol::output_note::set_array_attachment"]
    pub fn extern_output_note_set_array_attachment(
        note_idx: NoteIdx,
        attachment_scheme: Felt,
        attachment_f0: Felt,
        attachment_f1: Felt,
        attachment_f2: Felt,
        attachment_f3: Felt,
    );
}

/// Creates a new output note and returns its index.
///
/// # Examples
///
/// Create a note and add a single asset to it:
///
/// ```rust,ignore
/// // before using `Vec`/`vec!`.
/// extern crate alloc;
///
/// use miden::{felt, note, output_note, Asset, NoteType, Tag, Word};
///
/// // Values used to derive the note recipient.
/// let serial_num = Word::from_u64_unchecked(1, 2, 3, 4);
/// let note_script_root = Word::from_u64_unchecked(0, 0, 0, 0);
///
/// let storage = alloc::vec![felt!(0); 2];
/// let recipient = note::build_recipient(serial_num, note_script_root, storage);
///
/// let tag = Tag::from(felt!(0));
/// let note_type = NoteType::from(felt!(1)); // public note type (0b01)
///
/// let note_idx = output_note::create(tag, note_type, recipient);
/// output_note::add_asset(
///     Asset::new(
///         [felt!(0), felt!(0), felt!(0), felt!(1)],
///         [felt!(1), felt!(0), felt!(0), felt!(0)],
///     ),
///     note_idx,
/// );
/// ```
pub fn create(tag: Tag, note_type: NoteType, recipient: Recipient) -> NoteIdx {
    unsafe {
        extern_output_note_create(
            tag,
            note_type,
            recipient.inner[0],
            recipient.inner[1],
            recipient.inner[2],
            recipient.inner[3],
        )
    }
}

/// Sets the attachment of the output note specified by `note_idx`.
pub fn set_attachment(
    note_idx: NoteIdx,
    attachment_scheme: Felt,
    attachment_kind: Felt,
    attachment: Word,
) {
    unsafe {
        extern_output_note_set_attachment(
            note_idx,
            attachment_scheme,
            attachment_kind,
            attachment[0],
            attachment[1],
            attachment[2],
            attachment[3],
        );
    }
}

/// Sets the attachment of the output note specified by `note_idx` to the provided word.
pub fn set_word_attachment(note_idx: NoteIdx, attachment_scheme: Felt, attachment: Word) {
    unsafe {
        extern_output_note_set_word_attachment(
            note_idx,
            attachment_scheme,
            attachment[0],
            attachment[1],
            attachment[2],
            attachment[3],
        );
    }
}

/// Sets the attachment of the output note specified by `note_idx` to the provided commitment.
///
/// The advice map must contain an entry for the attachment elements committed to by `attachment`.
pub fn set_array_attachment(note_idx: NoteIdx, attachment_scheme: Felt, attachment: Word) {
    unsafe {
        extern_output_note_set_array_attachment(
            note_idx,
            attachment_scheme,
            attachment[0],
            attachment[1],
            attachment[2],
            attachment[3],
        );
    }
}

/// Adds the asset to the output note specified by `note_idx`.
///
/// # Examples
///
/// ```rust,ignore
/// use miden::{felt, output_note, Asset, NoteIdx};
///
/// // `note_idx` is returned by `output_note::create(...)`.
/// let note_idx: NoteIdx = /* ... */
///
/// let asset = Asset::new(
///     [felt!(0), felt!(0), felt!(0), felt!(1)],
///     [felt!(1), felt!(0), felt!(0), felt!(0)],
/// );
/// output_note::add_asset(asset, note_idx);
/// ```
pub fn add_asset(asset: Asset, note_idx: NoteIdx) {
    unsafe {
        extern_output_note_add_asset(
            asset.key[0],
            asset.key[1],
            asset.key[2],
            asset.key[3],
            asset.value[0],
            asset.value[1],
            asset.value[2],
            asset.value[3],
            note_idx,
        );
    }
}

/// Contains summary information about the assets of an output note.
pub struct OutputNoteAssetsInfo {
    pub commitment: Word,
    pub num_assets: Felt,
}

/// Retrieves the assets commitment and asset count for the output note at `note_index`.
pub fn get_assets_info(note_index: NoteIdx) -> OutputNoteAssetsInfo {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<(Word, Felt)>::uninit();
        extern_output_note_get_assets_info(note_index.inner, ret_area.as_mut_ptr());
        let (commitment, num_assets) = ret_area.assume_init();
        OutputNoteAssetsInfo {
            commitment,
            num_assets,
        }
    }
}

/// Returns the assets contained in the output note at `note_index`.
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_output_note_get_assets(ptr as *mut Felt, note_index.inner)
    };
    unsafe {
        assets.set_len(num_assets);
    }
    assets
}

/// Returns the recipient of the output note at `note_index`.
pub fn get_recipient(note_index: NoteIdx) -> Recipient {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<Recipient>::uninit();
        extern_output_note_get_recipient(note_index.inner, ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}

/// Returns the attachment and metadata header of the output note at `note_index`.
pub fn get_metadata(note_index: NoteIdx) -> NoteMetadata {
    unsafe {
        let mut ret_area = ::core::mem::MaybeUninit::<NoteMetadata>::uninit();
        extern_output_note_get_metadata(note_index.inner, ret_area.as_mut_ptr());
        ret_area.assume_init()
    }
}