Skip to content

Commit

Permalink
Merge pull request #344 from 0xPolygonMiden/greenhat/i339-expose-modu…
Browse files Browse the repository at this point in the history
…les-miden-sdk

[2/x] Rename `miden-sdk` crate to `miden` and expose modules in the public API
  • Loading branch information
bitwalker authored Oct 28, 2024
2 parents c201bf6 + 6f3d32a commit ab3947b
Show file tree
Hide file tree
Showing 39 changed files with 403 additions and 425 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions sdk/base-sys/src/bindings/account.rs
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/account@1.0.0")]
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()
}
}
5 changes: 5 additions & 0 deletions sdk/base-sys/src/bindings/mod.rs
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::*;
41 changes: 41 additions & 0 deletions sdk/base-sys/src/bindings/note.rs
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/note@1.0.0")]
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
}
45 changes: 45 additions & 0 deletions sdk/base-sys/src/bindings/tx.rs
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/tx@1.0.0")]
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],
)
}
}
37 changes: 0 additions & 37 deletions sdk/base-sys/src/bindings/tx/externs.rs

This file was deleted.

116 changes: 0 additions & 116 deletions sdk/base-sys/src/bindings/tx/mod.rs

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions sdk/base-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

#[cfg(feature = "bindings")]
pub mod bindings;

#[cfg(feature = "masl-lib")]
pub mod masl;
2 changes: 1 addition & 1 deletion sdk/sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "miden-sdk"
name = "miden"
description = "Miden SDK"
version.workspace = true
rust-version.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion sdk/sdk/src/lib.rs
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::*;
7 changes: 5 additions & 2 deletions sdk/stdlib-sys/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use core::ops::{Deref, DerefMut};

pub(crate) mod felt;
pub(crate) mod word;
mod felt;
mod word;

pub use felt::*;
pub use word::*;

#[repr(C, align(32))]
pub struct WordAligned<T>(T);
Expand Down
2 changes: 1 addition & 1 deletion sdk/stdlib-sys/src/intrinsics/word.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ops::{Index, IndexMut};

use crate::Felt;
use super::felt::Felt;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(C, align(32))]
Expand Down
2 changes: 1 addition & 1 deletion sdk/stdlib-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
mod intrinsics;
mod stdlib;

pub use intrinsics::{felt::*, word::*, WordAligned};
pub use intrinsics::*;
pub use stdlib::*;
Loading

0 comments on commit ab3947b

Please sign in to comment.