Skip to content

Commit 1452eaa

Browse files
committed
feat(descriptors): add multipath descs and pretty
- add generating multipath descriptors - add pretty formatting for descriptors
1 parent 618bf39 commit 1452eaa

File tree

2 files changed

+286
-134
lines changed

2 files changed

+286
-134
lines changed

src/handlers.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::utils::*;
1919
#[cfg(feature = "redb")]
2020
use bdk_redb::Store as RedbStore;
2121
use bdk_wallet::bip39::{Language, Mnemonic};
22+
use bdk_wallet::bitcoin::base64::Engine;
23+
use bdk_wallet::bitcoin::base64::prelude::BASE64_STANDARD;
2224
use bdk_wallet::bitcoin::{
2325
Address, Amount, FeeRate, Network, Psbt, Sequence, Txid,
2426
bip32::{DerivationPath, KeySource},
@@ -40,8 +42,6 @@ use bdk_wallet::{KeychainKind, SignOptions, Wallet};
4042
use bdk_wallet::{
4143
bitcoin::XOnlyPublicKey,
4244
descriptor::{Descriptor, Legacy, Miniscript},
43-
descriptor::{Legacy, Miniscript},
44-
miniscript::policy::Concrete,
4545
miniscript::{Tap, descriptor::TapTree, policy::Concrete},
4646
};
4747
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
@@ -55,10 +55,6 @@ use {
5555

5656
#[cfg(feature = "electrum")]
5757
use crate::utils::BlockchainClient::Electrum;
58-
#[cfg(feature = "electrum")]
59-
use crate::utils::BlockchainClient::Electrum;
60-
use bdk_wallet::bitcoin::base64::prelude::*;
61-
use serde_json::Value;
6258
use std::collections::BTreeMap;
6359
#[cfg(any(feature = "electrum", feature = "esplora"))]
6460
use std::collections::HashSet;
@@ -1288,9 +1284,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
12881284
subcommand: descriptor_subcommand,
12891285
} => {
12901286
let network = cli_opts.network;
1291-
let descriptor = handle_descriptor_subcommand(network, descriptor_subcommand)?;
1292-
let json = serde_json::to_string_pretty(&descriptor)?;
1293-
Ok(json)
1287+
let descriptor = handle_descriptor_subcommand(network, descriptor_subcommand, pretty)?;
1288+
Ok(descriptor)
12941289
}
12951290
};
12961291
result
@@ -1368,43 +1363,45 @@ fn readline() -> Result<String, Error> {
13681363
pub fn handle_descriptor_subcommand(
13691364
network: Network,
13701365
subcommand: DescriptorSubCommand,
1371-
) -> Result<Value, Error> {
1372-
match subcommand {
1366+
pretty: bool,
1367+
) -> Result<String, Error> {
1368+
let result = match subcommand {
13731369
DescriptorSubCommand::Generate {
13741370
r#type,
13751371
multipath,
13761372
key,
13771373
} => {
1378-
let descriptor_type = match r#type {
1379-
44 => DescriptorType::Bip44,
1380-
49 => DescriptorType::Bip49,
1381-
84 => DescriptorType::Bip84,
1382-
86 => DescriptorType::Bip86,
1383-
_ => {
1384-
return Err(Error::Generic(
1385-
"Unsupported script type: {r#type}".to_string(),
1386-
));
1374+
let descriptor_type = DescriptorType::from_bip32_num(r#type)
1375+
.ok_or_else(|| Error::Generic(format!("Unsupported script type: {type}")))?;
1376+
1377+
match (multipath, key) {
1378+
// generate multipath descriptors with a key
1379+
(true, Some(key)) => {
1380+
if is_mnemonic(&key) {
1381+
return Err(Error::Generic(
1382+
"Mnemonic not supported for multipath descriptors".to_string(),
1383+
));
1384+
}
1385+
generate_descriptors(descriptor_type, &key, true)
13871386
}
1388-
};
1389-
1390-
match (multipath, key.as_ref()) {
1391-
// generate multipath descriptors
1392-
(true, Some(k)) => generate_descriptors(&network, descriptor_type, k, true),
1393-
(false, Some(k)) => {
1394-
if is_mnemonic(k) {
1395-
// generate descriptors from given mnemonic string
1396-
generate_descriptor_from_mnemonic_string(k, network, descriptor_type)
1387+
// generate descriptors with a key or mnemonic
1388+
(false, Some(key)) => {
1389+
if is_mnemonic(&key) {
1390+
generate_descriptor_from_mnemonic_string(&key, network, descriptor_type)
13971391
} else {
1398-
// generate descriptors from key
1399-
generate_descriptors(&network, descriptor_type, k, false)
1392+
generate_descriptors(descriptor_type, &key, false)
14001393
}
14011394
}
1402-
// generate mnemonic and descriptors
1395+
// Generate new mnemonic and descriptors
14031396
(false, None) => generate_new_descriptor_with_mnemonic(network, descriptor_type),
1404-
_ => Err(Error::Generic("Provide a key or string".to_string())),
1397+
// Invalid case
1398+
(true, None) => Err(Error::Generic(
1399+
"A key is required for multipath descriptors".to_string(),
1400+
)),
14051401
}
14061402
}
1407-
}
1403+
}?;
1404+
format_descriptor_output(&result, pretty)
14081405
}
14091406

14101407
#[cfg(any(

0 commit comments

Comments
 (0)