miden_client_cli/lib.rs
1use std::env;
2use std::ffi::OsString;
3use std::ops::{Deref, DerefMut};
4use std::sync::Arc;
5
6use clap::{Parser, Subcommand};
7use comfy_table::{Attribute, Cell, ContentArrangement, Table, presets};
8use errors::CliError;
9use miden_client::account::AccountHeader;
10use miden_client::builder::ClientBuilder;
11use miden_client::keystore::{FilesystemKeyStore, Keystore};
12use miden_client::note_transport::grpc::GrpcNoteTransportClient;
13use miden_client::store::{NoteFilter as ClientNoteFilter, OutputNoteRecord};
14use miden_client_sqlite_store::ClientBuilderSqliteExt;
15
16mod commands;
17use commands::account::AccountCmd;
18use commands::clear_config::ClearConfigCmd;
19use commands::exec::ExecCmd;
20use commands::export::ExportCmd;
21use commands::import::ImportCmd;
22use commands::info::InfoCmd;
23use commands::init::InitCmd;
24use commands::new_account::{NewAccountCmd, NewWalletCmd};
25use commands::new_transactions::{ConsumeNotesCmd, MintCmd, SendCmd, SwapCmd};
26use commands::notes::NotesCmd;
27use commands::sync::SyncCmd;
28use commands::tags::TagsCmd;
29use commands::transactions::TransactionCmd;
30
31use self::utils::config_file_exists;
32use crate::commands::address::AddressCmd;
33
34pub type CliKeyStore = FilesystemKeyStore;
35
36/// A Client configured using the CLI's system user configuration.
37///
38/// This is a wrapper around `Client<CliKeyStore>` that provides convenient
39/// initialization methods while maintaining full compatibility with the
40/// underlying Client API through `Deref`.
41///
42/// # Examples
43///
44/// ```no_run
45/// use miden_client_cli::transaction::TransactionRequestBuilder;
46/// use miden_client_cli::{CliClient, DebugMode};
47///
48/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
49/// // Create a CLI-configured client
50/// let mut client = CliClient::new(DebugMode::Disabled).await?;
51///
52/// // All Client methods work automatically via Deref
53/// client.sync_state().await?;
54///
55/// // Build and submit transactions
56/// let req = TransactionRequestBuilder::new()
57/// // ... configure transaction
58/// .build()?;
59///
60/// // client.submit_new_transaction(req, target_account_id)?;
61/// # Ok(())
62/// # }
63/// ```
64pub struct CliClient(miden_client::Client<CliKeyStore>);
65
66impl CliClient {
67 /// Creates a new `CliClient` instance from an existing `CliConfig`.
68 ///
69 ///
70 /// **⚠️ WARNING: This method bypasses the standard CLI configuration discovery logic and should
71 /// only be used in specific scenarios such as testing or when you have explicit control
72 /// requirements.**
73 ///
74 /// ## When NOT to use this method
75 ///
76 /// - **DO NOT** use this method if you want your application to behave like the CLI tool
77 /// - **DO NOT** use this for general-purpose client initialization
78 /// - **DO NOT** use this if you expect automatic local/global config resolution
79 ///
80 /// ## When to use this method
81 ///
82 /// - **Testing**: When you need to test with a specific configuration
83 /// - **Explicit Control**: When you must load config from a non-standard location
84 /// - **Programmatic Config**: When you're constructing configuration programmatically
85 ///
86 /// ## Recommended Alternative
87 ///
88 /// For standard client initialization that matches CLI behavior, use:
89 /// ```ignore
90 /// CliClient::new(debug_mode).await?
91 /// ```
92 ///
93 /// This method **does not** follow the CLI's configuration priority logic (local → global).
94 /// Instead, it uses exactly the configuration provided, which may not be what you expect.
95 ///
96 /// # Arguments
97 ///
98 /// * `config` - The CLI configuration to use (bypasses standard config discovery)
99 /// * `debug_mode` - The debug mode setting ([`DebugMode::Enabled`] or [`DebugMode::Disabled`])
100 ///
101 /// # Returns
102 ///
103 /// A configured [`CliClient`] instance.
104 ///
105 /// # Errors
106 ///
107 /// Returns a [`CliError`] if:
108 /// - Keystore initialization fails
109 /// - Client builder fails to construct the client
110 /// - Note transport connection fails (if configured)
111 ///
112 /// # Examples
113 ///
114 /// ```no_run
115 /// use std::path::PathBuf;
116 ///
117 /// use miden_client_cli::{CliClient, CliConfig, DebugMode};
118 ///
119 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
120 /// // BEWARE: This bypasses standard config discovery!
121 /// // Only use if you know what you're doing.
122 /// let config = CliConfig::from_dir(&PathBuf::from("/path/to/.miden"))?;
123 /// let client = CliClient::from_config(config, DebugMode::Disabled).await?;
124 ///
125 /// // Prefer this for standard CLI-like behavior:
126 /// let client = CliClient::new(DebugMode::Disabled).await?;
127 /// # Ok(())
128 /// # }
129 /// ```
130 pub async fn from_config(
131 config: CliConfig,
132 debug_mode: miden_client::DebugMode,
133 ) -> Result<Self, CliError> {
134 // Create keystore
135 let keystore =
136 CliKeyStore::new(config.secret_keys_directory.clone()).map_err(CliError::KeyStore)?;
137
138 // Build client with the provided configuration
139 let mut builder = ClientBuilder::new()
140 .sqlite_store(config.store_filepath.clone())
141 .grpc_client(&config.rpc.endpoint.clone().into(), Some(config.rpc.timeout_ms))
142 .authenticator(Arc::new(keystore))
143 .in_debug_mode(debug_mode)
144 .tx_discard_delta(Some(TX_DISCARD_DELTA));
145
146 // Add optional max_block_number_delta
147 if let Some(delta) = config.max_block_number_delta {
148 builder = builder.max_block_number_delta(delta);
149 }
150
151 // Add optional note transport client
152 if let Some(tl_config) = config.note_transport {
153 let note_transport_client =
154 GrpcNoteTransportClient::connect(tl_config.endpoint.clone(), tl_config.timeout_ms)
155 .await
156 .map_err(|e| CliError::from(miden_client::ClientError::from(e)))?;
157 builder = builder.note_transport(Arc::new(note_transport_client));
158 }
159
160 // Build and return the wrapped client
161 let client = builder.build().await.map_err(CliError::from)?;
162 Ok(CliClient(client))
163 }
164
165 /// Creates a new `CliClient` instance configured using the system user configuration.
166 ///
167 /// # ✅ Recommended Constructor
168 ///
169 /// **This is the recommended way to create a `CliClient` instance.**
170 ///
171 /// This method implements the configuration logic used by the CLI tool, allowing external
172 /// projects to create a Client instance with the same configuration. It searches for
173 /// configuration files in the following order:
174 ///
175 /// 1. Local `.miden/miden-client.toml` in the current working directory
176 /// 2. Global `.miden/miden-client.toml` in the home directory
177 ///
178 /// If no configuration file is found, it silently initializes a default configuration.
179 ///
180 /// The client is initialized with:
181 /// - `SQLite` store from the configured path
182 /// - `gRPC` client connection to the configured RPC endpoint
183 /// - Filesystem-based keystore authenticator
184 /// - Optional note transport client (if configured)
185 /// - Transaction graceful blocks delta
186 /// - Optional max block number delta
187 ///
188 /// # Arguments
189 ///
190 /// * `debug_mode` - The debug mode setting ([`DebugMode::Enabled`] or [`DebugMode::Disabled`]).
191 ///
192 /// # Returns
193 ///
194 /// A configured [`CliClient`] instance.
195 ///
196 /// # Errors
197 ///
198 /// Returns a [`CliError`] if:
199 /// - No configuration file is found (local or global)
200 /// - Configuration file parsing fails
201 /// - Keystore initialization fails
202 /// - Client builder fails to construct the client
203 /// - Note transport connection fails (if configured)
204 ///
205 /// # Examples
206 ///
207 /// ```no_run
208 /// use miden_client_cli::transaction::TransactionRequestBuilder;
209 /// use miden_client_cli::{CliClient, DebugMode};
210 ///
211 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
212 /// // Create a client with default settings (debug disabled)
213 /// let mut client = CliClient::new(DebugMode::Disabled).await?;
214 ///
215 /// // Or with debug mode enabled
216 /// let mut client = CliClient::new(DebugMode::Enabled).await?;
217 ///
218 /// // Use it like a regular Client
219 /// client.sync_state().await?;
220 ///
221 /// // Build and submit transactions
222 /// let req = TransactionRequestBuilder::new()
223 /// // ... configure transaction
224 /// .build()?;
225 ///
226 /// // client.submit_new_transaction(req, target_account_id)?;
227 /// # Ok(())
228 /// # }
229 /// ```
230 pub async fn new(debug_mode: miden_client::DebugMode) -> Result<Self, CliError> {
231 // Check if client is not yet initialized => silently initialize the client
232 if !config_file_exists()? {
233 let init_cmd = InitCmd::default();
234 init_cmd.execute()?;
235 }
236
237 // Load configuration from system
238 let config = CliConfig::load()?;
239
240 // Create client using the loaded configuration
241 Self::from_config(config, debug_mode).await
242 }
243
244 /// Unwraps the `CliClient` to get the inner `Client<CliKeyStore>`.
245 ///
246 /// This consumes the `CliClient` and returns the underlying client.
247 ///
248 /// # Examples
249 ///
250 /// ```no_run
251 /// use miden_client_cli::{CliClient, DebugMode};
252 ///
253 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
254 /// let cli_client = CliClient::new(DebugMode::Disabled).await?;
255 /// let inner_client = cli_client.into_inner();
256 /// # Ok(())
257 /// # }
258 /// ```
259 pub fn into_inner(self) -> miden_client::Client<CliKeyStore> {
260 self.0
261 }
262}
263
264/// Allows using `CliClient` like `Client<CliKeyStore>` through deref coercion.
265///
266/// This enables calling all `Client` methods on `CliClient` directly.
267impl Deref for CliClient {
268 type Target = miden_client::Client<CliKeyStore>;
269
270 fn deref(&self) -> &Self::Target {
271 &self.0
272 }
273}
274
275/// Allows mutable access to `Client<CliKeyStore>` methods.
276impl DerefMut for CliClient {
277 fn deref_mut(&mut self) -> &mut Self::Target {
278 &mut self.0
279 }
280}
281
282pub mod config;
283// These modules intentionally shadow the miden_client re-exports - CLI has its own errors/utils
284#[allow(hidden_glob_reexports)]
285mod errors;
286mod faucet_details_map;
287mod info;
288#[allow(hidden_glob_reexports)]
289mod utils;
290
291/// Re-export `MIDEN_DIR` for use in tests
292pub use config::MIDEN_DIR;
293/// Re-export common types for external projects
294pub use config::{CLIENT_CONFIG_FILE_NAME, CliConfig};
295pub use errors::CliError as Error;
296/// Re-export the entire `miden_client` crate so external projects can use a single dependency.
297pub use miden_client::*;
298
299/// Client binary name.
300///
301/// If, for whatever reason, we fail to obtain the client's executable name,
302/// then we simply display the standard "miden-client".
303pub fn client_binary_name() -> OsString {
304 std::env::current_exe()
305 .inspect_err(|e| {
306 eprintln!(
307 "WARNING: Couldn't obtain the path of the current executable because of {e}.\
308 Defaulting to miden-client."
309 );
310 })
311 .and_then(|executable_path| {
312 executable_path.file_name().map(std::ffi::OsStr::to_os_string).ok_or(
313 std::io::Error::other("Couldn't obtain the file name of the current executable"),
314 )
315 })
316 .unwrap_or(OsString::from("miden-client"))
317}
318
319/// Number of blocks that must elapse after a transaction’s reference block before it is marked
320/// stale and discarded.
321const TX_DISCARD_DELTA: u32 = 20;
322
323/// Root CLI struct.
324#[derive(Parser, Debug)]
325#[command(
326 name = "miden-client",
327 about = "The Miden client",
328 version,
329 propagate_version = true,
330 rename_all = "kebab-case"
331)]
332#[command(multicall(true))]
333pub struct MidenClientCli {
334 #[command(subcommand)]
335 behavior: Behavior,
336}
337
338impl From<MidenClientCli> for Cli {
339 fn from(value: MidenClientCli) -> Self {
340 match value.behavior {
341 Behavior::MidenClient { cli } => cli,
342 Behavior::External(args) => Cli::parse_from(args).set_external(),
343 }
344 }
345}
346
347#[derive(Debug, Subcommand)]
348#[command(rename_all = "kebab-case")]
349enum Behavior {
350 /// The Miden Client CLI.
351 MidenClient {
352 #[command(flatten)]
353 cli: Cli,
354 },
355
356 /// Used when the Miden Client CLI is called under a different name, like
357 /// when it is called from [Midenup](https://github.com/0xMiden/midenup).
358 /// Vec<OsString> holds the "raw" arguments passed to the command line,
359 /// analogous to `argv`.
360 #[command(external_subcommand)]
361 External(Vec<OsString>),
362}
363
364#[derive(Parser, Debug)]
365#[command(name = "miden-client")]
366pub struct Cli {
367 /// Activates the executor's debug mode, which enables debug output for scripts
368 /// that were compiled and executed with this mode.
369 #[arg(short, long, default_value_t = false)]
370 debug: bool,
371
372 #[command(subcommand)]
373 action: Command,
374
375 /// Indicates whether the client's CLI is being called directly, or
376 /// externally under an alias (like in the case of
377 /// [Midenup](https://github.com/0xMiden/midenup).
378 #[arg(skip)]
379 #[allow(unused)]
380 external: bool,
381}
382
383/// CLI actions.
384#[derive(Debug, Parser)]
385pub enum Command {
386 Account(AccountCmd),
387 NewAccount(NewAccountCmd),
388 NewWallet(NewWalletCmd),
389 Import(ImportCmd),
390 Export(ExportCmd),
391 Init(InitCmd),
392 ClearConfig(ClearConfigCmd),
393 Notes(NotesCmd),
394 Sync(SyncCmd),
395 /// View a summary of the current client state.
396 Info(InfoCmd),
397 Tags(TagsCmd),
398 Address(AddressCmd),
399 #[command(name = "tx")]
400 Transaction(TransactionCmd),
401 Mint(MintCmd),
402 Send(SendCmd),
403 Swap(SwapCmd),
404 ConsumeNotes(ConsumeNotesCmd),
405 Exec(ExecCmd),
406}
407
408/// CLI entry point.
409impl Cli {
410 pub async fn execute(&self) -> Result<(), CliError> {
411 // Handle commands that don't require client initialization
412 match &self.action {
413 Command::Init(init_cmd) => {
414 init_cmd.execute()?;
415 return Ok(());
416 },
417 Command::ClearConfig(clear_config_cmd) => {
418 clear_config_cmd.execute()?;
419 return Ok(());
420 },
421 _ => {},
422 }
423
424 // Check if Client is not yet initialized => silently initialize the client
425 if !config_file_exists()? {
426 let init_cmd = InitCmd::default();
427 init_cmd.execute()?;
428 }
429
430 // Define whether we want to use the executor's debug mode based on the env var and
431 // the flag override
432 let in_debug_mode = match env::var("MIDEN_DEBUG") {
433 Ok(value) if value.to_lowercase() == "true" => miden_client::DebugMode::Enabled,
434 _ => miden_client::DebugMode::Disabled,
435 };
436
437 // Load configuration
438 let cli_config = CliConfig::load()?;
439
440 // Create keystore for commands that need it
441 let keystore = CliKeyStore::new(cli_config.secret_keys_directory.clone())
442 .map_err(CliError::KeyStore)?;
443
444 // Create the client
445 let cli_client = CliClient::from_config(cli_config, in_debug_mode).await?;
446
447 // Extract the inner client for command execution
448 let client = cli_client.into_inner();
449
450 // Execute CLI command
451 match &self.action {
452 Command::Account(account) => account.execute(client).await,
453 Command::NewWallet(new_wallet) => Box::pin(new_wallet.execute(client, keystore)).await,
454 Command::NewAccount(new_account) => {
455 Box::pin(new_account.execute(client, keystore)).await
456 },
457 Command::Import(import) => import.execute(client, keystore).await,
458 Command::Init(_) | Command::ClearConfig(_) => Ok(()), // Already handled earlier
459 Command::Info(info_cmd) => info::print_client_info(&client, info_cmd.rpc_status).await,
460 Command::Notes(notes) => Box::pin(notes.execute(client)).await,
461 Command::Sync(sync) => sync.execute(client).await,
462 Command::Tags(tags) => tags.execute(client).await,
463 Command::Address(addresses) => addresses.execute(client).await,
464 Command::Transaction(transaction) => transaction.execute(client).await,
465 Command::Exec(execute_program) => Box::pin(execute_program.execute(client)).await,
466 Command::Export(cmd) => cmd.execute(client, keystore).await,
467 Command::Mint(mint) => Box::pin(mint.execute(client)).await,
468 Command::Send(send) => Box::pin(send.execute(client)).await,
469 Command::Swap(swap) => Box::pin(swap.execute(client)).await,
470 Command::ConsumeNotes(consume_notes) => Box::pin(consume_notes.execute(client)).await,
471 }
472 }
473
474 fn set_external(mut self) -> Self {
475 self.external = true;
476 self
477 }
478}
479
480pub fn create_dynamic_table(headers: &[&str]) -> Table {
481 let header_cells = headers
482 .iter()
483 .map(|header| Cell::new(header).add_attribute(Attribute::Bold))
484 .collect::<Vec<_>>();
485
486 let mut table = Table::new();
487 table
488 .load_preset(presets::UTF8_FULL)
489 .set_content_arrangement(ContentArrangement::DynamicFullWidth)
490 .set_header(header_cells);
491
492 table
493}
494
495/// Returns the client output note whose ID starts with `note_id_prefix`.
496///
497/// # Errors
498///
499/// - Returns [`IdPrefixFetchError::NoMatch`](miden_client::IdPrefixFetchError::NoMatch) if we were
500/// unable to find any note where `note_id_prefix` is a prefix of its ID.
501/// - Returns [`IdPrefixFetchError::MultipleMatches`](miden_client::IdPrefixFetchError::MultipleMatches)
502/// if there were more than one note found where `note_id_prefix` is a prefix of its ID.
503pub(crate) async fn get_output_note_with_id_prefix<AUTH: Keystore + Sync>(
504 client: &miden_client::Client<AUTH>,
505 note_id_prefix: &str,
506) -> Result<OutputNoteRecord, miden_client::IdPrefixFetchError> {
507 let mut output_note_records = client
508 .get_output_notes(ClientNoteFilter::All)
509 .await
510 .map_err(|err| {
511 tracing::error!("Error when fetching all notes from the store: {err}");
512 miden_client::IdPrefixFetchError::NoMatch(
513 format!("note ID prefix {note_id_prefix}").to_string(),
514 )
515 })?
516 .into_iter()
517 .filter(|note_record| note_record.id().to_hex().starts_with(note_id_prefix))
518 .collect::<Vec<_>>();
519
520 if output_note_records.is_empty() {
521 return Err(miden_client::IdPrefixFetchError::NoMatch(
522 format!("note ID prefix {note_id_prefix}").to_string(),
523 ));
524 }
525 if output_note_records.len() > 1 {
526 let output_note_record_ids =
527 output_note_records.iter().map(OutputNoteRecord::id).collect::<Vec<_>>();
528 tracing::error!(
529 "Multiple notes found for the prefix {}: {:?}",
530 note_id_prefix,
531 output_note_record_ids
532 );
533 return Err(miden_client::IdPrefixFetchError::MultipleMatches(
534 format!("note ID prefix {note_id_prefix}").to_string(),
535 ));
536 }
537
538 Ok(output_note_records
539 .pop()
540 .expect("input_note_records should always have one element"))
541}
542
543/// Returns the client account whose ID starts with `account_id_prefix`.
544///
545/// # Errors
546///
547/// - Returns [`IdPrefixFetchError::NoMatch`](miden_client::IdPrefixFetchError::NoMatch) if we were
548/// unable to find any account where `account_id_prefix` is a prefix of its ID.
549/// - Returns [`IdPrefixFetchError::MultipleMatches`](miden_client::IdPrefixFetchError::MultipleMatches)
550/// if there were more than one account found where `account_id_prefix` is a prefix of its ID.
551async fn get_account_with_id_prefix<AUTH>(
552 client: &miden_client::Client<AUTH>,
553 account_id_prefix: &str,
554) -> Result<AccountHeader, miden_client::IdPrefixFetchError> {
555 let mut accounts = client
556 .get_account_headers()
557 .await
558 .map_err(|err| {
559 tracing::error!("Error when fetching all accounts from the store: {err}");
560 miden_client::IdPrefixFetchError::NoMatch(
561 format!("account ID prefix {account_id_prefix}").to_string(),
562 )
563 })?
564 .into_iter()
565 .filter(|(account_header, _)| account_header.id().to_hex().starts_with(account_id_prefix))
566 .map(|(acc, _)| acc)
567 .collect::<Vec<_>>();
568
569 if accounts.is_empty() {
570 return Err(miden_client::IdPrefixFetchError::NoMatch(
571 format!("account ID prefix {account_id_prefix}").to_string(),
572 ));
573 }
574 if accounts.len() > 1 {
575 let account_ids = accounts.iter().map(AccountHeader::id).collect::<Vec<_>>();
576 tracing::error!(
577 "Multiple accounts found for the prefix {}: {:?}",
578 account_id_prefix,
579 account_ids
580 );
581 return Err(miden_client::IdPrefixFetchError::MultipleMatches(
582 format!("account ID prefix {account_id_prefix}").to_string(),
583 ));
584 }
585
586 Ok(accounts.pop().expect("account_ids should always have one element"))
587}