Skip to content

Commit 550e816

Browse files
committed
feat(descriptors): add multipath descs and pretty
- add generating multipath descriptors - add pretty formatting for descriptors
1 parent 1f77842 commit 550e816

File tree

2 files changed

+286
-130
lines changed

2 files changed

+286
-130
lines changed

src/handlers.rs

Lines changed: 31 additions & 30 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};
@@ -1270,9 +1270,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
12701270
subcommand: descriptor_subcommand,
12711271
} => {
12721272
let network = cli_opts.network;
1273-
let descriptor = handle_descriptor_subcommand(network, descriptor_subcommand)?;
1274-
let json = serde_json::to_string_pretty(&descriptor)?;
1275-
Ok(json)
1273+
let descriptor = handle_descriptor_subcommand(network, descriptor_subcommand, pretty)?;
1274+
Ok(descriptor)
12761275
}
12771276
};
12781277
result
@@ -1350,43 +1349,45 @@ fn readline() -> Result<String, Error> {
13501349
pub fn handle_descriptor_subcommand(
13511350
network: Network,
13521351
subcommand: DescriptorSubCommand,
1353-
) -> Result<Value, Error> {
1354-
match subcommand {
1352+
pretty: bool,
1353+
) -> Result<String, Error> {
1354+
let result = match subcommand {
13551355
DescriptorSubCommand::Generate {
13561356
r#type,
13571357
multipath,
13581358
key,
13591359
} => {
1360-
let descriptor_type = match r#type {
1361-
44 => DescriptorType::Bip44,
1362-
49 => DescriptorType::Bip49,
1363-
84 => DescriptorType::Bip84,
1364-
86 => DescriptorType::Bip86,
1365-
_ => {
1366-
return Err(Error::Generic(
1367-
"Unsupported script type: {r#type}".to_string(),
1368-
));
1360+
let descriptor_type = DescriptorType::from_bip32_num(r#type)
1361+
.ok_or_else(|| Error::Generic(format!("Unsupported script type: {type}")))?;
1362+
1363+
match (multipath, key) {
1364+
// generate multipath descriptors with a key
1365+
(true, Some(key)) => {
1366+
if is_mnemonic(&key) {
1367+
return Err(Error::Generic(
1368+
"Mnemonic not supported for multipath descriptors".to_string(),
1369+
));
1370+
}
1371+
generate_descriptors(descriptor_type, &key, true)
13691372
}
1370-
};
1371-
1372-
match (multipath, key.as_ref()) {
1373-
// generate multipath descriptors
1374-
(true, Some(k)) => generate_descriptors(&network, descriptor_type, k, true),
1375-
(false, Some(k)) => {
1376-
if is_mnemonic(k) {
1377-
// generate descriptors from given mnemonic string
1378-
generate_descriptor_from_mnemonic_string(k, network, descriptor_type)
1373+
// generate descriptors with a key or mnemonic
1374+
(false, Some(key)) => {
1375+
if is_mnemonic(&key) {
1376+
generate_descriptor_from_mnemonic_string(&key, network, descriptor_type)
13791377
} else {
1380-
// generate descriptors from key
1381-
generate_descriptors(&network, descriptor_type, k, false)
1378+
generate_descriptors(descriptor_type, &key, false)
13821379
}
13831380
}
1384-
// generate mnemonic and descriptors
1381+
// Generate new mnemonic and descriptors
13851382
(false, None) => generate_new_descriptor_with_mnemonic(network, descriptor_type),
1386-
_ => Err(Error::Generic("Provide a key or string".to_string())),
1383+
// Invalid case
1384+
(true, None) => Err(Error::Generic(
1385+
"A key is required for multipath descriptors".to_string(),
1386+
)),
13871387
}
13881388
}
1389-
}
1389+
}?;
1390+
format_descriptor_output(&result, pretty)
13901391
}
13911392

13921393
#[cfg(any(

0 commit comments

Comments
 (0)