diff --git a/miden-lib/asm/account_components/basic_fungible_faucet.masm b/miden-lib/asm/account_components/basic_fungible_faucet.masm new file mode 100644 index 000000000..238c6ea2a --- /dev/null +++ b/miden-lib/asm/account_components/basic_fungible_faucet.masm @@ -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 diff --git a/miden-lib/asm/account_components/basic_wallet.masm b/miden-lib/asm/account_components/basic_wallet.masm new file mode 100644 index 000000000..9c70246e9 --- /dev/null +++ b/miden-lib/asm/account_components/basic_wallet.masm @@ -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 diff --git a/miden-lib/asm/account_components/rpo_falcon_512.masm b/miden-lib/asm/account_components/rpo_falcon_512.masm new file mode 100644 index 000000000..86bb1eed5 --- /dev/null +++ b/miden-lib/asm/account_components/rpo_falcon_512.masm @@ -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 diff --git a/miden-lib/build.rs b/miden-lib/build.rs index a7f1470ff..ca940f9b9 100644 --- a/miden-lib/build.rs +++ b/miden-lib/build.rs @@ -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(()) } @@ -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. @@ -257,7 +261,7 @@ fn parse_proc_offsets(filename: impl AsRef) -> Result Result<()> { @@ -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()?; } diff --git a/miden-lib/src/accounts/auth/mod.rs b/miden-lib/src/accounts/auth/mod.rs index 8c3aca405..5e81ace52 100644 --- a/miden-lib/src/accounts/auth/mod.rs +++ b/miden-lib/src/accounts/auth/mod.rs @@ -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, } diff --git a/miden-lib/src/accounts/faucets/mod.rs b/miden-lib/src/accounts/faucets/mod.rs index 8ebd4afb8..a57dd20a9 100644 --- a/miden-lib/src/accounts/faucets/mod.rs +++ b/miden-lib/src/accounts/faucets/mod.rs @@ -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. /// @@ -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, diff --git a/miden-lib/src/accounts/wallets/mod.rs b/miden-lib/src/accounts/wallets/mod.rs index c0f50d6e4..3be86c2bd 100644 --- a/miden-lib/src/accounts/wallets/mod.rs +++ b/miden-lib/src/accounts/wallets/mod.rs @@ -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 @@ -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 for AccountComponent {