Skip to content
Merged
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
31 changes: 29 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//! All subcommands are defined in the below enums.

#![allow(clippy::large_enum_variant)]

use bdk_wallet::bitcoin::{
Address, Network, OutPoint, ScriptBuf,
bip32::{DerivationPath, Xpriv},
Expand Down Expand Up @@ -107,8 +106,23 @@ 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 {
/// 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<String>,
},
}

/// Wallet operation subcommands.
#[derive(Debug, Subcommand, Clone, PartialEq)]
pub enum WalletSubCommand {
Expand Down Expand Up @@ -470,6 +484,19 @@ pub enum ReplSubCommand {
#[command(subcommand)]
subcommand: KeySubCommand,
},
/// Generate descriptors
Descriptor {
/// 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<String>,
},
/// Exit REPL loop.
Exit,
}
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
58 changes: 46 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,17 @@ 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::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 +60,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::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 @@ -1260,6 +1256,10 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
}
Ok("".to_string())
}
CliSubCommand::Descriptor { desc_type, key } => {
let descriptor = handle_descriptor_command(cli_opts.network, desc_type, key, pretty)?;
Ok(descriptor)
}
};
result
}
Expand Down Expand Up @@ -1307,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 {
Expand All @@ -1333,6 +1338,35 @@ fn readline() -> Result<String, Error> {
Ok(buffer)
}

/// Handle the descriptor command
pub fn handle_descriptor_command(
network: Network,
desc_type: String,
key: Option<String>,
pretty: bool,
) -> Result<String, Error> {
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",
feature = "cbf",
feature = "rpc"
))]
#[cfg(test)]
mod test {
#[cfg(any(
Expand Down
Loading
Loading