-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from 0xPolygonMiden/greenhat/i339-expose-modu…
…les-miden-sdk [2/x] Rename `miden-sdk` crate to `miden` and expose modules in the public API
- Loading branch information
Showing
39 changed files
with
403 additions
and
425 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use miden_stdlib_sys::Felt; | ||
|
||
use super::types::{AccountId, CoreAsset}; | ||
|
||
#[allow(improper_ctypes)] | ||
#[link(wasm_import_module = "miden:core-import/[email protected]")] | ||
extern "C" { | ||
#[link_name = "get-id"] | ||
pub fn extern_account_get_id() -> AccountId; | ||
#[link_name = "add-asset"] | ||
pub fn extern_account_add_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); | ||
#[link_name = "remove-asset"] | ||
pub fn extern_account_remove_asset(_: Felt, _: Felt, _: Felt, _: Felt, ptr: *mut CoreAsset); | ||
} | ||
|
||
/// Get the account ID of the currently executing note account. | ||
pub fn get_id() -> AccountId { | ||
unsafe { extern_account_get_id() } | ||
} | ||
|
||
/// Add the specified asset to the vault. | ||
/// Returns the final asset in the account vault defined as follows: If asset is | ||
/// a non-fungible asset, then returns the same as asset. If asset is a | ||
/// fungible asset, then returns the total fungible asset in the account | ||
/// vault after asset was added to it. | ||
/// | ||
/// Panics: | ||
/// - If the asset is not valid. | ||
/// - If the total value of two fungible assets is greater than or equal to 2^63. | ||
/// - If the vault already contains the same non-fungible asset. | ||
pub fn add_asset(asset: CoreAsset) -> CoreAsset { | ||
unsafe { | ||
let mut ret_area = ::core::mem::MaybeUninit::<CoreAsset>::uninit(); | ||
extern_account_add_asset( | ||
asset.inner[0], | ||
asset.inner[1], | ||
asset.inner[2], | ||
asset.inner[3], | ||
ret_area.as_mut_ptr(), | ||
); | ||
ret_area.assume_init() | ||
} | ||
} | ||
|
||
/// Remove the specified asset from the vault. | ||
/// | ||
/// Panics: | ||
/// - The fungible asset is not found in the vault. | ||
/// - The amount of the fungible asset in the vault is less than the amount to be removed. | ||
/// - The non-fungible asset is not found in the vault. | ||
pub fn remove_asset(asset: CoreAsset) -> CoreAsset { | ||
unsafe { | ||
let mut ret_area = ::core::mem::MaybeUninit::<CoreAsset>::uninit(); | ||
extern_account_remove_asset( | ||
asset.inner[0], | ||
asset.inner[1], | ||
asset.inner[2], | ||
asset.inner[3], | ||
ret_area.as_mut_ptr(), | ||
); | ||
ret_area.assume_init() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
pub mod account; | ||
pub mod note; | ||
pub mod tx; | ||
mod types; | ||
|
||
pub use types::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
extern crate alloc; | ||
use alloc::vec::Vec; | ||
|
||
use miden_stdlib_sys::Felt; | ||
|
||
#[link(wasm_import_module = "miden:core-import/[email protected]")] | ||
extern "C" { | ||
#[link_name = "get-inputs"] | ||
pub fn extern_note_get_inputs(ptr: *mut Felt) -> usize; | ||
} | ||
|
||
/// Get the inputs of the currently executing note. | ||
pub fn get_inputs() -> Vec<Felt> { | ||
const MAX_INPUTS: usize = 256; | ||
let mut inputs: Vec<Felt> = Vec::with_capacity(MAX_INPUTS); | ||
let num_inputs = unsafe { | ||
// Ensure the pointer is a valid Miden pointer | ||
// | ||
// NOTE: This relies on the fact that BumpAlloc makes all allocations | ||
// minimally word-aligned. Each word consists of 4 elements of 4 bytes, | ||
// so to get a Miden address from a Rust address, we divide by 16 to get | ||
// the address in words (dividing by 4 gets us an address in elements, | ||
// and by 4 again we get the word address). | ||
let ptr = (inputs.as_mut_ptr() as usize) / 16; | ||
// The MASM for this function is here: | ||
// https://github.com/0xPolygonMiden/miden-base/blob/3cbe8d59dcf4ccc9c380b7c8417ac6178fc6b86a/miden-lib/asm/miden/note.masm#L69-L102 | ||
// #! Writes the inputs of the currently execute note into memory starting at the specified | ||
// address. #! | ||
// #! Inputs: [dest_ptr] | ||
// #! Outputs: [num_inputs, dest_ptr] | ||
// #! | ||
// #! - dest_ptr is the memory address to write the inputs. | ||
// Compiler generated adapter code at call site will drop the returned dest_ptr | ||
// and return the number of inputs | ||
extern_note_get_inputs(ptr as *mut Felt) | ||
}; | ||
unsafe { | ||
inputs.set_len(num_inputs); | ||
} | ||
inputs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use miden_stdlib_sys::Felt; | ||
|
||
use super::types::{CoreAsset, NoteId, NoteType, Recipient, Tag}; | ||
|
||
#[link(wasm_import_module = "miden:core-import/[email protected]")] | ||
extern "C" { | ||
#[link_name = "create-note"] | ||
pub fn extern_tx_create_note( | ||
asset_f0: Felt, | ||
asset_f1: Felt, | ||
asset_f2: Felt, | ||
asset_f3: Felt, | ||
tag: Tag, | ||
note_type: NoteType, | ||
recipient_f0: Felt, | ||
recipient_f1: Felt, | ||
recipient_f2: Felt, | ||
recipient_f3: Felt, | ||
) -> NoteId; | ||
} | ||
|
||
/// Creates a new note. asset is the asset to be included in the note. tag is | ||
/// the tag to be included in the note. recipient is the recipient of the note. | ||
/// Returns the id of the created note. | ||
pub fn create_note( | ||
asset: CoreAsset, | ||
tag: Tag, | ||
note_type: NoteType, | ||
recipient: Recipient, | ||
) -> NoteId { | ||
unsafe { | ||
extern_tx_create_note( | ||
asset.inner[0], | ||
asset.inner[1], | ||
asset.inner[2], | ||
asset.inner[3], | ||
tag, | ||
note_type, | ||
recipient.inner[0], | ||
recipient.inner[1], | ||
recipient.inner[2], | ||
recipient.inner[3], | ||
) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
|
||
#[cfg(feature = "bindings")] | ||
pub mod bindings; | ||
|
||
#[cfg(feature = "masl-lib")] | ||
pub mod masl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#![no_std] | ||
|
||
pub use miden_base_sys::bindings::tx::*; | ||
pub use miden_base_sys::bindings::*; | ||
pub use miden_sdk_alloc::BumpAlloc; | ||
pub use miden_stdlib_sys::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.