Skip to content

Commit 937461d

Browse files
committed
refactor(compile): lazy compilation and use pipe for readability
Move miniscript compilation inside match arms to avoid compiling for all script contexts when only one is needed. Use `tap::Pipe` for concise method chaining in sh/wsh/sh-wsh branches.
1 parent 741e672 commit 937461d

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ tracing-subscriber = "0.3.20"
2727
toml = "0.8.23"
2828
serde= {version = "1.0", features = ["derive"]}
2929
shlex = "1.3.0"
30+
tap = "1.0.1"
3031

3132
# Optional dependencies
3233
bdk_bitcoind_rpc = { version = "0.21.0", features = ["std"], optional = true }

src/handlers.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ use bdk_wallet::{
4848
key::{Parity, rand},
4949
secp256k1::{PublicKey, Scalar, SecretKey},
5050
},
51-
descriptor::{Descriptor, Legacy, Miniscript},
51+
descriptor::{Descriptor, Legacy},
5252
miniscript::{Tap, descriptor::TapTree, policy::Concrete},
5353
};
5454

5555
use clap::CommandFactory;
5656
use cli_table::{Cell, CellStruct, Style, Table, format::Justify};
5757
use serde_json::json;
58+
#[cfg(feature = "compiler")]
59+
use tap::Pipe;
5860

5961
#[cfg(feature = "electrum")]
6062
use crate::utils::BlockchainClient::Electrum;
@@ -1021,16 +1023,12 @@ pub(crate) fn handle_compile_subcommand(
10211023
) -> Result<String, Error> {
10221024
let policy = Concrete::<String>::from_str(policy.as_str())?;
10231025

1024-
let legacy_policy: Miniscript<String, Legacy> = policy.compile()?;
1025-
let segwit_policy: Miniscript<String, Segwitv0> = policy.compile()?;
1026-
let taproot_policy: Miniscript<String, Tap> = policy.compile()?;
1027-
10281026
let mut r = None;
10291027

10301028
let descriptor = match script_type.as_str() {
1031-
"sh" => Descriptor::new_sh(legacy_policy),
1032-
"wsh" => Descriptor::new_wsh(segwit_policy),
1033-
"sh-wsh" => Descriptor::new_sh_wsh(segwit_policy),
1029+
"sh" => policy.compile::<Legacy>()?.pipe(Descriptor::new_sh),
1030+
"wsh" => policy.compile::<Segwitv0>()?.pipe(Descriptor::new_wsh),
1031+
"sh-wsh" => policy.compile::<Segwitv0>()?.pipe(Descriptor::new_sh_wsh),
10341032
"tr" => {
10351033
// For tr descriptors, we use a randomized unspendable key (H + rG).
10361034
// This improves privacy by preventing observers from determining if key path spending is disabled.
@@ -1046,7 +1044,7 @@ pub(crate) fn handle_compile_subcommand(
10461044
let internal_key_point = nums_point.add_exp_tweak(&secp, &Scalar::from(r_secret))?;
10471045
let (xonly_internal_key, _) = internal_key_point.x_only_public_key();
10481046

1049-
let tree = TapTree::Leaf(Arc::new(taproot_policy));
1047+
let tree = TapTree::Leaf(Arc::new(policy.compile::<Tap>()?));
10501048

10511049
Descriptor::new_tr(xonly_internal_key.to_string(), Some(tree))
10521050
}

0 commit comments

Comments
 (0)