Skip to content

Commit da76708

Browse files
Update compiler example.
Change the compiler clap app into a specific example. Add comment docs and example description. Remove clap from dependency.
1 parent dfeb08f commit da76708

File tree

2 files changed

+31
-61
lines changed

2 files changed

+31
-61
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ test-hardware-signer = ["hardware-signer"]
100100
[dev-dependencies]
101101
lazy_static = "1.4"
102102
env_logger = "0.7"
103-
clap = "2.33"
104103
electrsd = "0.20"
105104

106105
[[example]]

examples/compiler.rs

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
extern crate bdk;
1313
extern crate bitcoin;
14-
extern crate clap;
1514
extern crate log;
1615
extern crate miniscript;
1716
extern crate serde_json;
@@ -21,8 +20,6 @@ use std::str::FromStr;
2120

2221
use log::info;
2322

24-
use clap::{App, Arg};
25-
2623
use bitcoin::Network;
2724
use miniscript::policy::Concrete;
2825
use miniscript::Descriptor;
@@ -31,75 +28,49 @@ use bdk::database::memory::MemoryDatabase;
3128
use bdk::wallet::AddressIndex::New;
3229
use bdk::{KeychainKind, Wallet};
3330

31+
/// Miniscript policy is a high level abstraction of spending conditions. Defined in the
32+
/// rust-miscript library here https://docs.rs/miniscript/7.0.0/miniscript/policy/index.html
33+
/// rust-miniscript provides a `compile()` function that can be used to compile any miniscript policy
34+
/// into a descriptor. This descriptor then in turn can be used in bdk a fully functioning wallet
35+
/// can be derived from the policy.
36+
///
37+
/// This example demonstrates the interaction between a bdk wallet and miniscript policy.
38+
3439
fn main() -> Result<(), Box<dyn Error>> {
3540
env_logger::init_from_env(
3641
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
3742
);
3843

39-
let matches = App::new("Miniscript Compiler")
40-
.arg(
41-
Arg::with_name("POLICY")
42-
.help("Sets the spending policy to compile")
43-
.required(true)
44-
.index(1),
45-
)
46-
.arg(
47-
Arg::with_name("TYPE")
48-
.help("Sets the script type used to embed the compiled policy")
49-
.required(true)
50-
.index(2)
51-
.possible_values(&["sh", "wsh", "sh-wsh"]),
52-
)
53-
.arg(
54-
Arg::with_name("parsed_policy")
55-
.long("parsed_policy")
56-
.short("p")
57-
.help("Also return the parsed spending policy in JSON format"),
58-
)
59-
.arg(
60-
Arg::with_name("network")
61-
.short("n")
62-
.long("network")
63-
.help("Sets the network")
64-
.takes_value(true)
65-
.default_value("testnet")
66-
.possible_values(&["testnet", "regtest", "bitcoin", "signet"]),
67-
)
68-
.get_matches();
69-
70-
let policy_str = matches.value_of("POLICY").unwrap();
71-
info!("Compiling policy: {}", policy_str);
44+
// We start with a generic miniscript policy string
45+
let policy_str = "or(10@thresh(4,pk(029ffbe722b147f3035c87cb1c60b9a5947dd49c774cc31e94773478711a929ac0),pk(025f05815e3a1a8a83bfbb03ce016c9a2ee31066b98f567f6227df1d76ec4bd143),pk(025625f41e4a065efc06d5019cbbd56fe8c07595af1231e7cbc03fafb87ebb71ec),pk(02a27c8b850a00f67da3499b60562673dcf5fdfb82b7e17652a7ac54416812aefd),pk(03e618ec5f384d6e19ca9ebdb8e2119e5bef978285076828ce054e55c4daf473e2)),1@and(older(4209713),thresh(2,pk(03deae92101c790b12653231439f27b8897264125ecb2f46f48278603102573165),pk(033841045a531e1adf9910a6ec279589a90b3b8a904ee64ffd692bd08a8996c1aa),pk(02aebf2d10b040eb936a6f02f44ee82f8b34f5c1ccb20ff3949c2b28206b7c1068))))";
46+
info!("Compiling policy: \n{}", policy_str);
7247

48+
// Parse the string as a [`Concrete`] type miniscript policy.
7349
let policy = Concrete::<String>::from_str(policy_str)?;
7450

75-
let descriptor = match matches.value_of("TYPE").unwrap() {
76-
"sh" => Descriptor::new_sh(policy.compile()?)?,
77-
"wsh" => Descriptor::new_wsh(policy.compile()?)?,
78-
"sh-wsh" => Descriptor::new_sh_wsh(policy.compile()?)?,
79-
_ => panic!("Invalid type"),
80-
};
51+
// Create a `wsh` type descriptor from the policy.
52+
// `policy.compile()` returns the resulting miniscript from the policy.
53+
let descriptor = Descriptor::new_wsh(policy.compile()?)?;
8154

82-
info!("... Descriptor: {}", descriptor);
55+
info!("Compiled into following Descriptor: \n{}", descriptor);
8356

8457
let database = MemoryDatabase::new();
8558

86-
let network = matches
87-
.value_of("network")
88-
.map(Network::from_str)
89-
.transpose()
90-
.unwrap()
91-
.unwrap_or(Network::Testnet);
92-
let wallet = Wallet::new(&format!("{}", descriptor), None, network, database)?;
93-
94-
info!("... First address: {}", wallet.get_address(New)?);
95-
96-
if matches.is_present("parsed_policy") {
97-
let spending_policy = wallet.policies(KeychainKind::External)?;
98-
info!(
99-
"... Spending policy:\n{}",
100-
serde_json::to_string_pretty(&spending_policy)?
101-
);
102-
}
59+
// Create a new wallet from this descriptor
60+
let wallet = Wallet::new(&format!("{}", descriptor), None, Network::Regtest, database)?;
61+
62+
info!(
63+
"First derived address from the descriptor: \n{}",
64+
wallet.get_address(New)?
65+
);
66+
67+
// BDK also has it's own `Policy` structure to represent the spending condition in a more
68+
// human readable json format.
69+
let spending_policy = wallet.policies(KeychainKind::External)?;
70+
info!(
71+
"The BDK spending policy: \n{}",
72+
serde_json::to_string_pretty(&spending_policy)?
73+
);
10374

10475
Ok(())
10576
}

0 commit comments

Comments
 (0)