Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move pre-defined account components' source code into masm files #960

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions miden-lib/asm/account_components/basic_fungible_faucet.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The MASM code of the Basic Fungible Faucet Account Component.
#
# See the `BasicFungibleFaucet` Rust type's documentation for more details.

export.::miden::contracts::faucets::basic_fungible::distribute
export.::miden::contracts::faucets::basic_fungible::burn
7 changes: 7 additions & 0 deletions miden-lib/asm/account_components/basic_wallet.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The MASM code of the Basic Wallet Account Component.
#
# See the `BasicWallet` Rust type's documentation for more details.

export.::miden::contracts::wallets::basic::receive_asset
export.::miden::contracts::wallets::basic::create_note
export.::miden::contracts::wallets::basic::move_asset_to_note
5 changes: 5 additions & 0 deletions miden-lib/asm/account_components/rpo_falcon_512.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The MASM code of the RPO Falcon 512 authentication Account Component.
#
# See the `RpoFalcon512` Rust type's documentation for more details.

export.::miden::contracts::auth::basic::auth_tx_rpo_falcon512
70 changes: 40 additions & 30 deletions miden-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ fn main() -> Result<()> {
)?;

// compile account components
compile_account_components(&target_dir.join(ASM_ACCOUNT_COMPONENTS_DIR), assembler)?;
compile_account_components(
&source_dir.join(ASM_ACCOUNT_COMPONENTS_DIR),
&target_dir.join(ASM_ACCOUNT_COMPONENTS_DIR),
assembler,
)?;

Ok(())
}
Expand All @@ -83,7 +87,7 @@ fn main() -> Result<()> {
/// - {source_dir}/main.masm -> defines the executable program of the transaction kernel.
/// - {source_dir}/lib -> contains common modules used by both api.masm and main.masm.
///
/// The complied files are written as follows:
/// The compiled files are written as follows:
///
/// - {target_dir}/tx_kernel.masl -> contains kernel library compiled from api.masm.
/// - {target_dir}/tx_kernel.masb -> contains the executable compiled from main.masm.
Expand Down Expand Up @@ -257,7 +261,7 @@ fn parse_proc_offsets(filename: impl AsRef<Path>) -> Result<BTreeMap<String, usi
// ================================================================================================

/// Reads the MASM files from "{source_dir}/miden" directory, compiles them into a Miden assembly
/// library, saves the library into "{target_dir}/miden.masl", and returns the complied library.
/// library, saves the library into "{target_dir}/miden.masl", and returns the compiled library.
fn compile_miden_lib(
source_dir: &Path,
target_dir: &Path,
Expand All @@ -277,8 +281,8 @@ fn compile_miden_lib(
// COMPILE EXECUTABLE MODULES
// ================================================================================================

/// Reads all MASM files from the "{source_dir}", complies each file individually into a MASB
/// file, and stores the complied files into the "{target_dir}".
/// Reads all MASM files from the "{source_dir}", compiles each file individually into a MASB
/// file, and stores the compiled files into the "{target_dir}".
///
/// The source files are expected to contain executable programs.
fn compile_note_scripts(source_dir: &Path, target_dir: &Path, assembler: Assembler) -> Result<()> {
Expand All @@ -303,35 +307,41 @@ fn compile_note_scripts(source_dir: &Path, target_dir: &Path, assembler: Assembl
Ok(())
}

// COMPILE DEFAULT ACCOUNT COMPONENTS
// COMPILE ACCOUNT COMPONENTS
// ================================================================================================

const BASIC_WALLET_CODE: &str = "
export.::miden::contracts::wallets::basic::receive_asset
export.::miden::contracts::wallets::basic::create_note
export.::miden::contracts::wallets::basic::move_asset_to_note
";

const RPO_FALCON_AUTH_CODE: &str = "
export.::miden::contracts::auth::basic::auth_tx_rpo_falcon512
";

const BASIC_FUNGIBLE_FAUCET_CODE: &str = "
export.::miden::contracts::faucets::basic_fungible::distribute
export.::miden::contracts::faucets::basic_fungible::burn
";

/// Compiles the default account components into a MASL library and stores the complied files in
/// `target_dir`.
fn compile_account_components(target_dir: &Path, assembler: Assembler) -> Result<()> {
for (component_name, component_code) in [
("basic_wallet", BASIC_WALLET_CODE),
("rpo_falcon_512", RPO_FALCON_AUTH_CODE),
("basic_fungible_faucet", BASIC_FUNGIBLE_FAUCET_CODE),
] {
let component_library = assembler.clone().assemble_library([component_code])?;
/// Compiles the account components in `source_dir` into MASL libraries and stores the compiled
/// files in `target_dir`.
fn compile_account_components(
source_dir: &Path,
target_dir: &Path,
assembler: Assembler,
) -> Result<()> {
if !target_dir.exists() {
fs::create_dir_all(target_dir).unwrap();
}

for masm_file_path in get_masm_files(source_dir).unwrap() {
let component_name = masm_file_path
.file_stem()
.expect("masm file should have a file stem")
.to_str()
.expect("file stem should be valid UTF-8")
.to_owned();

// Read the source code to string instead of passing it to assemble_library directly since
// that would attempt to interpret the path as a LibraryPath which would fail.
let component_source_code = fs::read_to_string(masm_file_path)
.expect("reading the component's MASM source code should succeed");

let component_library = assembler
.clone()
.assemble_library([component_source_code])
.expect("library assembly should succeed");

let component_file_path =
target_dir.join(component_name).with_extension(Library::LIBRARY_EXTENSION);

component_library.write_to_file(component_file_path).into_diagnostic()?;
}

Expand Down
7 changes: 6 additions & 1 deletion miden-lib/src/accounts/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use crate::accounts::components::rpo_falcon_512_library;
/// An [`AccountComponent`] implementing the RpoFalcon512 signature scheme for authentication of
/// transactions.
///
/// Its exported procedures are:
/// It reexports the procedures from `miden::contracts::auth::basic`. When linking against this
/// component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be available to the
/// assembler which is the case when using [`TransactionKernel::assembler()`][kasm]. The procedures
/// of this component are:
/// - `auth_tx_rpo_falcon512`, which can be used to verify a signature provided via the advice stack
/// to authenticate a transaction.
///
/// This component supports all account types.
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct RpoFalcon512 {
public_key: PublicKey,
}
Expand Down
7 changes: 6 additions & 1 deletion miden-lib/src/accounts/faucets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_fungible_faucet_libr

/// An [`AccountComponent`] implementing a basic fungible faucet.
///
/// Its exported procedures are:
/// It reexports the procedures from `miden::contracts::faucets::basic_fungible`. When linking
/// against this component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be
/// available to the assembler which is the case when using
/// [`TransactionKernel::assembler()`][kasm]. The procedures of this component are:
/// - `distribute`, which mints an assets and create a note for the provided recipient.
/// - `burn`, which burns the provided asset.
///
Expand All @@ -25,6 +28,8 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_fungible_faucet_libr
/// authentication.
///
/// This component supports accounts of type [`AccountType::FungibleFaucet`].
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct BasicFungibleFaucet {
symbol: TokenSymbol,
decimals: u8,
Expand Down
7 changes: 6 additions & 1 deletion miden-lib/src/accounts/wallets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_wallet_library};

/// An [`AccountComponent`] implementing a basic wallet.
///
/// Its exported procedures are:
/// It reexports the procedures from `miden::contracts::wallets::basic`. When linking against this
/// component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be available to the
/// assembler which is the case when using [`TransactionKernel::assembler()`][kasm]. The procedures
/// of this component are:
/// - `receive_asset`, which can be used to add an asset to the account.
/// - `create_note`, which can be used to create a new note without any assets attached to it.
/// - `move_asset_to_note`, which can be used to remove the specified asset from the account and add
Expand All @@ -23,6 +26,8 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_wallet_library};
/// providing authentication.
///
/// This component supports all account types.
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct BasicWallet;

impl From<BasicWallet> for AccountComponent {
Expand Down
Loading