Skip to content

Commit 329cad2

Browse files
committed
- Updated error calls on functions in handlers and commands, and also deleted redundant errors that were created.
- Updated the comment on the descriptor command, also added comments to the inputs for the subcommands. - Added possiblevalues for the descriptor types in the subcommand - Made the descriptor type format consistent - Fixed duplicated on invalidArguments call
1 parent 10ae1bd commit 329cad2

4 files changed

Lines changed: 34 additions & 35 deletions

File tree

src/commands.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bdk_wallet::bitcoin::{
1717
Address, Network, OutPoint, ScriptBuf,
1818
bip32::{DerivationPath, Xpriv},
1919
};
20-
use clap::{Args, Parser, Subcommand, ValueEnum, value_parser};
20+
use clap::{Args, Parser, Subcommand, ValueEnum, value_parser, builder::TypedValueParser};
2121

2222
#[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))]
2323
use crate::utils::parse_proxy_auth;
@@ -106,10 +106,10 @@ pub enum CliSubCommand {
106106
#[command(flatten)]
107107
wallet_opts: WalletOpts,
108108
},
109-
/// Descriptor generation operations.
110-
///
111-
/// Allows users to generate Bitcoin wallet descriptors from either an extended key (e.g., Xprv/Xpub) or by generating a new random mnemonic phrase.
112-
/// This feature is intended for development and testing. Exercise caution when using descriptors created this way in production environments.
109+
/// Output Descriptors operations.
110+
///
111+
/// Generate output descriptors from either extended key (Xprv/Xpub) or mnemonic phrase.
112+
/// This feature is intended for development and testing purposes only.
113113
Descriptor {
114114
#[clap(subcommand)]
115115
subcommand: DescriptorSubCommand,
@@ -488,12 +488,19 @@ pub enum ReplSubCommand {
488488
pub enum DescriptorSubCommand {
489489
/// Generate a descriptor
490490
Generate {
491-
#[clap(long = "type", value_parser = clap::value_parser!(u8).range(44..=86), short = 't', default_value = "84")]
492-
r#type: u8, // 44, 49, 84, 86
493-
491+
/// Descriptor type (script type).
492+
#[arg(
493+
long = "type",
494+
short = 't',
495+
value_parser = clap::builder::PossibleValuesParser::new(["44", "49", "84", "86"])
496+
.map(|s| s.parse::<u8>().unwrap()),
497+
default_value = "84"
498+
)]
499+
r#type: u8,
500+
/// Enable multipath descriptors
494501
#[arg(long = "multipath", short = 'm', default_value_t = false)]
495502
multipath: bool,
496-
503+
/// Optional key input
497504
key: Option<String>,
498505
},
499506

src/error.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,8 @@ pub enum BDKCliError {
113113
#[error("Descriptor parsing failed: {0}")]
114114
DescriptorParsingError(String),
115115

116-
#[error("Invalid extended public key (xpub): {0}")]
117-
InvalidXpub(String),
118-
119-
#[error("Invalid extended private key (xprv): {0}")]
120-
InvalidXprv(String),
116+
#[error("Invalid extended key (xpub): {0}")]
117+
InvalidKey(String),
121118

122119
#[error("Invalid derivation path: {0}")]
123120
InvalidDerivationPath(String),

src/handlers.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ use bdk_wallet::{
4141
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
4242

4343
use bdk_wallet::keys::{
44-
DerivableKey, DescriptorKey, DescriptorKey::Secret, ExtendedKey, GeneratableKey, GeneratedKey,
45-
bip39::WordCount, DescriptorPublicKey,
44+
DerivableKey, DescriptorKey, DescriptorKey::Secret, DescriptorPublicKey, ExtendedKey,
45+
GeneratableKey, GeneratedKey, bip39::WordCount,
4646
};
4747
use bdk_wallet::miniscript::miniscript;
48-
use serde_json::{json, Value};
48+
use serde_json::{Value, json};
4949
use std::collections::BTreeMap;
5050
#[cfg(any(feature = "electrum", feature = "esplora"))]
5151
use std::collections::HashSet;
@@ -1352,16 +1352,11 @@ pub fn handle_descriptor_subcommand(
13521352
key,
13531353
} => {
13541354
let (descriptor_type, derivation_path_str) = match r#type {
1355-
44 => (DescriptorType::Bip44, "m/44'/1'/0'"),
1356-
49 => (DescriptorType::Bip49, "m/49'/1'/0'"),
1357-
84 => (DescriptorType::Bip84, "m/84'/1'/0'"),
1358-
86 => (DescriptorType::Bip86, "m/86'/1'/0'"),
1359-
_ => {
1360-
return Err(Error::Generic(format!(
1361-
"Unsupported script type {}",
1362-
r#type
1363-
)))
1364-
}
1355+
44 => (DescriptorType::Bip44, "m/44h/1h/0h"),
1356+
49 => (DescriptorType::Bip49, "m/49h/1h/0h"),
1357+
84 => (DescriptorType::Bip84, "m/84h/1h/0h"),
1358+
86 => (DescriptorType::Bip86, "m/86h/1h/0h"),
1359+
_ => return Err(Error::UnsupportedScriptType(r#type)),
13651360
};
13661361

13671362
match (multipath, key.as_ref()) {
@@ -1380,7 +1375,7 @@ pub fn handle_descriptor_subcommand(
13801375
}
13811376
(false, None) => generate_new_descriptor_with_mnemonic(network, descriptor_type),
13821377
_ => Err(Error::InvalidArguments(
1383-
"Invalid arguments: provide a key or weak string".to_string(),
1378+
"Provide a key or weak string".to_string(),
13841379
)),
13851380
}
13861381
}

src/utils.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ use bdk_wallet::descriptor::{
4747
Segwitv0, {Descriptor, DescriptorPublicKey},
4848
};
4949
use bdk_wallet::keys::{
50-
bip39::WordCount,
5150
DerivableKey, ExtendedKey,
51+
bip39::WordCount,
5252
{DescriptorSecretKey, GeneratableKey, GeneratedKey, IntoDescriptorKey},
5353
};
5454
use bdk_wallet::miniscript::{
55-
descriptor::{DescriptorXKey, Wildcard},
5655
Tap,
56+
descriptor::{DescriptorXKey, Wildcard},
5757
};
58-
use serde_json::{json, Value};
58+
use serde_json::{Value, json};
5959

6060
/// Parse the recipient (Address,Amount) argument from cli input.
6161
pub(crate) fn parse_recipient(s: &str) -> Result<(ScriptBuf, u64), String> {
@@ -537,7 +537,7 @@ pub fn generate_multipath_descriptor(
537537
let (fingerprint, make_desc): (_, DescriptorBuilderFn) = if is_private {
538538
let xprv: Xpriv = key
539539
.parse()
540-
.map_err(|e| Error::InvalidXprv(format!("Invalid xprv: {e}")))?;
540+
.map_err(|e| Error::InvalidKey(format!("Invalid xprv: {e}")))?;
541541
let fingerprint = xprv.fingerprint(&secp);
542542

543543
let closure = move |change: u32| -> Result<(String, Option<String>), Error> {
@@ -578,7 +578,7 @@ pub fn generate_multipath_descriptor(
578578
} else {
579579
let xpub: Xpub = key
580580
.parse()
581-
.map_err(|e| Error::InvalidXpub(format!("Invalid xpub: {e}")))?;
581+
.map_err(|e| Error::InvalidKey(format!("Invalid xpub: {e}")))?;
582582
let fingerprint = xpub.fingerprint();
583583

584584
let closure = move |change: u32| -> Result<(String, Option<String>), Error> {
@@ -638,7 +638,7 @@ pub fn generate_bip_descriptor_from_key(
638638

639639
let xprv: Xpriv = key
640640
.parse()
641-
.map_err(|e| Error::InvalidXprv(format!("Invalid xprv: {e}")))?;
641+
.map_err(|e| Error::InvalidKey(format!("Invalid xprv: {e}")))?;
642642

643643
let fingerprint = xprv.fingerprint(&secp);
644644

@@ -722,7 +722,7 @@ pub fn generate_descriptor_from_mnemonic_string(
722722

723723
let xprv = xprv
724724
.derive_priv(&secp, &derivation_path)
725-
.map_err(|e| Error::InvalidXprv(format!("Failed to derive xprv: {e}")))?;
725+
.map_err(|e| Error::InvalidKey(format!("Failed to derive xprv: {e}")))?;
726726

727727
generate_bip_descriptor_from_key(
728728
&network,

0 commit comments

Comments
 (0)