From 7d3720e382410af5ebe18aa48c5c819bf21a1ec3 Mon Sep 17 00:00:00 2001 From: Vihiga Tyonum Date: Wed, 5 Nov 2025 18:41:04 +0100 Subject: [PATCH] feat(desc): remove generate subcommand - remove generate subcommand - add descriptor to the repl --- src/commands.rs | 26 +++++++++++++++----------- src/handlers.rs | 43 ++++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 2b82c4b..d3f2d98 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -111,8 +111,16 @@ pub enum CliSubCommand { /// Generate output descriptors from either extended key (Xprv/Xpub) or mnemonic phrase. /// This feature is intended for development and testing purposes only. Descriptor { - #[clap(subcommand)] - subcommand: DescriptorSubCommand, + /// Descriptor type (script type) + #[arg( + long = "type", + short = 't', + value_parser = ["pkh", "wpkh", "sh", "wsh", "tr"], + default_value = "wsh" + )] + desc_type: String, + /// Optional key: xprv, xpub, or mnemonic phrase + key: Option, }, } /// Wallet operation subcommands. @@ -476,14 +484,8 @@ pub enum ReplSubCommand { #[command(subcommand)] subcommand: KeySubCommand, }, - /// Exit REPL loop. - Exit, -} -/// Subcommands for Key operations. -#[derive(Debug, Subcommand, Clone, PartialEq, Eq)] -pub enum DescriptorSubCommand { - /// Generate a descriptor - Generate { + /// Generate descriptors + Descriptor { /// Descriptor type (script type). #[arg( long = "type", @@ -492,7 +494,9 @@ pub enum DescriptorSubCommand { default_value = "wsh" )] desc_type: String, - /// Optional key input + /// Optional key: xprv, xpub, or mnemonic phrase key: Option, }, + /// Exit REPL loop. + Exit, } diff --git a/src/handlers.rs b/src/handlers.rs index b39712e..6c58a83 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1256,11 +1256,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { } Ok("".to_string()) } - CliSubCommand::Descriptor { - subcommand: descriptor_subcommand, - } => { - let network = cli_opts.network; - let descriptor = handle_descriptor_subcommand(network, descriptor_subcommand, pretty)?; + CliSubCommand::Descriptor { desc_type, key } => { + let descriptor = handle_descriptor_command(cli_opts.network, desc_type, key, pretty)?; Ok(descriptor) } }; @@ -1310,6 +1307,11 @@ async fn respond( .map_err(|e| e.to_string())?; Some(value) } + ReplSubCommand::Descriptor { desc_type, key } => { + let value = handle_descriptor_command(network, desc_type, key, cli_opts.pretty) + .map_err(|e| e.to_string())?; + Some(value) + } ReplSubCommand::Exit => None, }; if let Some(value) = response { @@ -1336,30 +1338,29 @@ fn readline() -> Result { Ok(buffer) } -pub fn handle_descriptor_subcommand( +/// Handle the descriptor command +pub fn handle_descriptor_command( network: Network, - subcommand: DescriptorSubCommand, + desc_type: String, + key: Option, pretty: bool, ) -> Result { - let result = match subcommand { - DescriptorSubCommand::Generate { desc_type, key } => { - match key { - Some(key) => { - if is_mnemonic(&key) { - // User provided mnemonic - generate_descriptor_from_mnemonic(&key, network, &desc_type) - } else { - // User provided xprv/xpub - generate_descriptors(&desc_type, &key, network) - } - } - // Generate new mnemonic and descriptors - None => generate_descriptor_with_mnemonic(network, &desc_type), + let result = match key { + Some(key) => { + if is_mnemonic(&key) { + // User provided mnemonic + generate_descriptor_from_mnemonic(&key, network, &desc_type) + } else { + // User provided xprv/xpub + generate_descriptors(&desc_type, &key, network) } } + // Generate new mnemonic and descriptors + None => generate_descriptor_with_mnemonic(network, &desc_type), }?; format_descriptor_output(&result, pretty) } + #[cfg(any( feature = "electrum", feature = "esplora", -- 2.49.0