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