From: Vadim Anufriev Date: Tue, 24 Mar 2026 20:12:30 +0000 (+0400) Subject: refactor(compile): lazy compilation and use pipe for readability X-Git-Url: http://internal-gitweb-vhost/blockdata/script/encode/-script/display/struct.ScriptHash.html?a=commitdiff_plain;h=29ef2e2912a6503086682c33b7ffda21f4c0eed7;p=bdk-cli 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. --- diff --git a/Cargo.lock b/Cargo.lock index f40db91..d984ac4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -216,6 +216,7 @@ dependencies = [ "serde", "serde_json", "shlex", + "tap", "thiserror 2.0.18", "tokio", "toml 1.1.0+spec-1.1.0", @@ -2635,6 +2636,12 @@ dependencies = [ "syn", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tempfile" version = "3.24.0" diff --git a/Cargo.toml b/Cargo.toml index 3b4ffc6..4603662 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ tracing-subscriber = "0.3.20" toml = "1.1.0" serde= {version = "1.0", features = ["derive"]} shlex = "1.3.0" +tap = "1.0.1" # Optional dependencies bdk_bitcoind_rpc = { version = "0.21.0", features = ["std"], optional = true } diff --git a/src/handlers.rs b/src/handlers.rs index 6c5647b..8d893b7 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -48,13 +48,15 @@ use bdk_wallet::{ key::{Parity, rand}, secp256k1::{Scalar, SecretKey}, }, - descriptor::{Descriptor, Legacy, Miniscript}, + descriptor::{Descriptor, Legacy}, miniscript::{Tap, descriptor::TapTree, policy::Concrete}, }; use clap::CommandFactory; use cli_table::{Cell, CellStruct, Style, Table, format::Justify}; use serde_json::json; +#[cfg(feature = "compiler")] +use tap::Pipe; #[cfg(feature = "silent-payments")] use { bdk_sp::{ @@ -1286,16 +1288,12 @@ pub(crate) fn handle_compile_subcommand( ) -> Result { let policy = Concrete::::from_str(policy.as_str())?; - let legacy_policy: Miniscript = policy.compile()?; - let segwit_policy: Miniscript = policy.compile()?; - let taproot_policy: Miniscript = policy.compile()?; - let mut r = None; let descriptor = match script_type.as_str() { - "sh" => Descriptor::new_sh(legacy_policy), - "wsh" => Descriptor::new_wsh(segwit_policy), - "sh-wsh" => Descriptor::new_sh_wsh(segwit_policy), + "sh" => policy.compile::()?.pipe(Descriptor::new_sh), + "wsh" => policy.compile::()?.pipe(Descriptor::new_wsh), + "sh-wsh" => policy.compile::()?.pipe(Descriptor::new_sh_wsh), "tr" => { // For tr descriptors, we use a randomized unspendable key (H + rG). // This improves privacy by preventing observers from determining if key path spending is disabled. @@ -1314,7 +1312,7 @@ pub(crate) fn handle_compile_subcommand( let internal_key_point = nums_point.add_exp_tweak(&secp, &Scalar::from(r_secret))?; let (xonly_internal_key, _) = internal_key_point.x_only_public_key(); - let tree = TapTree::Leaf(Arc::new(taproot_policy)); + let tree = TapTree::Leaf(Arc::new(policy.compile::()?)); Descriptor::new_tr(xonly_internal_key.to_string(), Some(tree)) }