Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
//! All subcommands are defined in the below enums.

#![allow(clippy::large_enum_variant)]

use bdk_wallet::bitcoin::{
Address, Network, OutPoint, ScriptBuf,
bip32::{DerivationPath, Xpriv},
};
use clap::{Args, Parser, Subcommand, ValueEnum, value_parser};
use clap::{
Args, Parser, Subcommand, ValueEnum,
builder::{PossibleValuesParser, TypedValueParser},
value_parser,
};

#[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))]
use crate::utils::parse_proxy_auth;
Expand Down Expand Up @@ -107,8 +110,15 @@ pub enum CliSubCommand {
#[command(flatten)]
wallet_opts: WalletOpts,
},
/// Output Descriptors operations.
///
/// 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,
},
}

/// Wallet operation subcommands.
#[derive(Debug, Subcommand, Clone, PartialEq)]
pub enum WalletSubCommand {
Expand Down Expand Up @@ -477,3 +487,24 @@ pub enum ReplSubCommand {
/// Exit REPL loop.
Exit,
}
/// Subcommands for Key operations.
#[derive(Debug, Subcommand, Clone, PartialEq, Eq)]
pub enum DescriptorSubCommand {
/// Generate a descriptor
Generate {
/// Descriptor type (script type).
#[arg(
long = "type",
short = 't',
value_parser = PossibleValuesParser::new(["44", "49", "84", "86"])
.map(|s| s.parse::<u8>().unwrap()),
default_value = "84"
)]
r#type: u8,
/// Enable multipath descriptors
#[arg(long = "multipath", short = 'm', default_value_t = false)]
multipath: bool,
/// Optional key input
key: Option<String>,
},
}
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use thiserror::Error;

#[derive(Debug, Error)]
pub enum BDKCliError {
#[error("BIP39 error: {0}")]
BIP39Error(#[from] bdk_wallet::bip39::Error),
#[error("BIP39 error: {0:?}")]
BIP39Error(#[from] Option<bdk_wallet::bip39::Error>),

#[error("BIP32 error: {0}")]
BIP32Error(#[from] bdk_wallet::bitcoin::bip32::Error),
Expand Down
81 changes: 69 additions & 12 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use crate::commands::*;
use crate::error::BDKCliError as Error;
#[cfg(any(feature = "sqlite", feature = "redb"))]
use crate::persister::Persister;
#[cfg(feature = "cbf")]
use crate::utils::BlockchainClient::KyotoClient;
use crate::utils::*;
#[cfg(feature = "redb")]
use bdk_redb::Store as RedbStore;
use bdk_wallet::bip39::{Language, Mnemonic};
use bdk_wallet::bitcoin::base64::Engine;
use bdk_wallet::bitcoin::base64::prelude::BASE64_STANDARD;
use bdk_wallet::bitcoin::{
Address, Amount, FeeRate, Network, Psbt, Sequence, Txid,
bip32::{DerivationPath, KeySource},
Expand All @@ -40,11 +40,21 @@ use bdk_wallet::rusqlite::Connection;
use bdk_wallet::{KeychainKind, SignOptions, Wallet};
#[cfg(feature = "compiler")]
use bdk_wallet::{
bitcoin::XOnlyPublicKey,
descriptor::{Descriptor, Legacy, Miniscript},
miniscript::{Tap, descriptor::TapTree, policy::Concrete},
};
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
use serde_json::json;
#[cfg(feature = "cbf")]
use {
crate::utils::BlockchainClient::KyotoClient,
bdk_kyoto::{Info, LightClient},
tokio::select,
};

#[cfg(feature = "electrum")]
use crate::utils::BlockchainClient::Electrum;
use std::collections::BTreeMap;
#[cfg(any(feature = "electrum", feature = "esplora"))]
use std::collections::HashSet;
Expand All @@ -54,16 +64,6 @@ use std::io::Write;
use std::str::FromStr;
#[cfg(any(feature = "redb", feature = "compiler"))]
use std::sync::Arc;

#[cfg(feature = "electrum")]
use crate::utils::BlockchainClient::Electrum;
#[cfg(feature = "cbf")]
use bdk_kyoto::{Info, LightClient};
#[cfg(feature = "compiler")]
use bdk_wallet::bitcoin::XOnlyPublicKey;
use bdk_wallet::bitcoin::base64::prelude::*;
#[cfg(feature = "cbf")]
use tokio::select;
#[cfg(any(
feature = "electrum",
feature = "esplora",
Expand Down Expand Up @@ -1280,6 +1280,13 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
}
Ok("".to_string())
}
CliSubCommand::Descriptor {
subcommand: descriptor_subcommand,
} => {
let network = cli_opts.network;
let descriptor = handle_descriptor_subcommand(network, descriptor_subcommand, pretty)?;
Ok(descriptor)
}
};
result
}
Expand Down Expand Up @@ -1353,6 +1360,56 @@ fn readline() -> Result<String, Error> {
Ok(buffer)
}

pub fn handle_descriptor_subcommand(
network: Network,
subcommand: DescriptorSubCommand,
pretty: bool,
) -> Result<String, Error> {
let result = match subcommand {
DescriptorSubCommand::Generate {
r#type,
multipath,
key,
} => {
let descriptor_type = DescriptorType::from_bip32_num(r#type)
.ok_or_else(|| Error::Generic(format!("Unsupported script type: {type}")))?;

match (multipath, key) {
// generate multipath descriptors with a key
(true, Some(key)) => {
if is_mnemonic(&key) {
return Err(Error::Generic(
"Mnemonic not supported for multipath descriptors".to_string(),
));
}
generate_descriptors(descriptor_type, &key, true)
}
// generate descriptors with a key or mnemonic
(false, Some(key)) => {
if is_mnemonic(&key) {
generate_descriptor_from_mnemonic_string(&key, network, descriptor_type)
} else {
generate_descriptors(descriptor_type, &key, false)
}
}
// Generate new mnemonic and descriptors
(false, None) => generate_new_descriptor_with_mnemonic(network, descriptor_type),
// Invalid case
(true, None) => Err(Error::Generic(
"A key is required for multipath descriptors".to_string(),
)),
}
}
}?;
format_descriptor_output(&result, pretty)
}

#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "cbf",
feature = "rpc"
))]
#[cfg(test)]
mod test {
#[cfg(any(
Expand Down
Loading
Loading