diff --git a/crates/miden-lib/build.rs b/crates/miden-lib/build.rs index 5f0b5de38..5d2c3eff9 100644 --- a/crates/miden-lib/build.rs +++ b/crates/miden-lib/build.rs @@ -75,7 +75,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, + )?; generate_kernel_error_constants(&source_dir)?; @@ -233,7 +237,7 @@ fn parse_proc_offsets(filename: impl AsRef) -> Result Result<()> { @@ -283,33 +287,37 @@ 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()?; diff --git a/crates/miden-lib/src/account/auth/mod.rs b/crates/miden-lib/src/account/auth/mod.rs index ce93fafed..8a25fe578 100644 --- a/crates/miden-lib/src/account/auth/mod.rs +++ b/crates/miden-lib/src/account/auth/mod.rs @@ -8,11 +8,16 @@ use crate::account::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, } diff --git a/crates/miden-lib/src/account/faucets/mod.rs b/crates/miden-lib/src/account/faucets/mod.rs index 0238e2ef4..1c0388107 100644 --- a/crates/miden-lib/src/account/faucets/mod.rs +++ b/crates/miden-lib/src/account/faucets/mod.rs @@ -15,7 +15,10 @@ use crate::account::{auth::RpoFalcon512, components::basic_fungible_faucet_libra /// 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. /// @@ -24,6 +27,8 @@ use crate::account::{auth::RpoFalcon512, components::basic_fungible_faucet_libra /// authentication. /// /// This component supports accounts of type [`AccountType::FungibleFaucet`]. +/// +/// [kasm]: crate::transaction::TransactionKernel::assembler pub struct BasicFungibleFaucet { symbol: TokenSymbol, decimals: u8, diff --git a/crates/miden-lib/src/account/wallets/mod.rs b/crates/miden-lib/src/account/wallets/mod.rs index 2d89e2530..9d8e0e33d 100644 --- a/crates/miden-lib/src/account/wallets/mod.rs +++ b/crates/miden-lib/src/account/wallets/mod.rs @@ -15,7 +15,10 @@ use crate::account::{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 @@ -25,6 +28,8 @@ use crate::account::{auth::RpoFalcon512, components::basic_wallet_library}; /// providing authentication. /// /// This component supports all account types. +/// +/// [kasm]: crate::transaction::TransactionKernel::assembler pub struct BasicWallet; impl From for AccountComponent {