Skip to content

Commit 0759450

Browse files
committed
- Optimized my imports to reduce the lines
- Ensure you can generate different type of descriptor with mnemonic - Changed the Json output format - Added path subcommand for generating descriptors from keys - Set multipath to accept different types for descriptors - Set multipath default value to false and gave it a short of m - Set type default to 84 and gave it a short to t
1 parent 6c8e714 commit 0759450

File tree

3 files changed

+226
-75
lines changed

3 files changed

+226
-75
lines changed

src/commands.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,16 @@ pub enum CliSubCommand {
132132
}
133133
#[derive(Debug, Clone, PartialEq, Args)]
134134
pub struct GenerateDescriptorArgs {
135-
#[clap(long, value_parser = clap::value_parser!(u8).range(44..=86))]
135+
#[clap(long = "type", value_parser = clap::value_parser!(u8).range(44..=86), short = 't', default_value = "84")]
136136
pub r#type: u8, // 44, 49, 84, 86
137137

138-
#[clap(long)]
138+
#[arg(long = "multipath", short = 'm', default_value_t = false)]
139139
pub multipath: bool,
140140

141-
pub key: Option<String>, // Positional argument (tprv/tpub/xprv/xpub)
141+
#[arg(long)]
142+
pub path: Option<String>,
143+
144+
pub key: Option<String>,
142145
}
143146

144147
/// Wallet operation subcommands.

src/handlers.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use bdk_wallet::{KeychainKind, SignOptions, Wallet};
3737
use bdk_wallet::keys::DescriptorKey::Secret;
3838
use bdk_wallet::keys::{DerivableKey, DescriptorKey, ExtendedKey, GeneratableKey, GeneratedKey};
3939
use bdk_wallet::miniscript::miniscript;
40-
use bdk_wallet::serde::ser::Error as SerdeErrorTrait;
41-
use serde_json::{json, Value, Error as SerdeError};
40+
use serde_json::{json, Value};
4241
use std::collections::BTreeMap;
4342
#[cfg(any(feature = "electrum", feature = "esplora"))]
4443
use std::collections::HashSet;
@@ -905,7 +904,7 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
905904
CliSubCommand::Descriptor(args) => {
906905
let network = cli_opts.network;
907906
let descriptor = generate_descriptor_from_args(args.clone(), network)
908-
.map_err(|e| SerdeError::custom(e.to_string()))?;
907+
.map_err(|e| Error::Generic(e.to_string()))?;
909908
let json = serde_json::to_string_pretty(&descriptor)?;
910909
Ok(json)
911910
}
@@ -984,19 +983,42 @@ pub fn generate_descriptor_from_args(
984983
args: GenerateDescriptorArgs,
985984
network: Network,
986985
) -> Result<serde_json::Value, Error> {
986+
let descriptor_type = match args.r#type {
987+
44 => DescriptorType::Bip44,
988+
49 => DescriptorType::Bip49,
989+
84 => DescriptorType::Bip84,
990+
86 => DescriptorType::Bip86,
991+
_ => {
992+
return Err(Error::Generic(format!(
993+
"Unsupported script type {}",
994+
args.r#type
995+
)))
996+
}
997+
};
998+
if args.multipath && args.path.is_some() {
999+
return Err(Error::InvalidArguments(
1000+
"Path can not be called here".to_string(),
1001+
));
1002+
}
1003+
if args.path.is_some() && args.key.is_none() {
1004+
return Err(Error::InvalidArguments(
1005+
"Custom path requires a key".to_string(),
1006+
));
1007+
}
9871008
match (args.multipath, args.key.as_ref()) {
9881009
(true, Some(key)) => generate_multipath_descriptor(&network, args.r#type, key),
989-
(false, Some(key)) => generate_standard_descriptor(&network, args.r#type, key),
990-
(false, None) => {
991-
// New default: generate descriptor from fresh mnemonic (for script_type 84 only)
992-
if args.r#type == 84 {
993-
generate_new_bip84_descriptor_with_mnemonic(network)
1010+
(false, Some(key)) => {
1011+
if let Some(path) = args.path.as_ref() {
1012+
// Use descriptor from custom derivation path
1013+
generate_bip_descriptor_from_key(&network, key, path, descriptor_type)
9941014
} else {
995-
Err(Error::Generic(
996-
"Only script type 84 is supported for mnemonic-based generation".to_string(),
997-
))
1015+
generate_standard_descriptor(&network, args.r#type, key)
9981016
}
9991017
}
1018+
(false, None) => {
1019+
// Generate from fresh mnemonic
1020+
generate_new_descriptor_with_mnemonic(network, descriptor_type)
1021+
}
10001022
_ => Err(Error::InvalidArguments(
10011023
"Invalid arguments: please provide a key or a weak string".to_string(),
10021024
)),

0 commit comments

Comments
 (0)