Skip to content

Commit f0501da

Browse files
committed
examples: Refactor htlc
In an effort to make the example more clear; refactor the `htlc` example. Refactor only, no logic changes.
1 parent c79235a commit f0501da

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

examples/htlc.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use miniscript::DescriptorTrait;
2424
use std::str::FromStr;
2525

2626
fn main() {
27-
//HTLC policy with 10:1 odds for happy(co-operative) case compared to uncooperative case
27+
// HTLC policy with 10:1 odds for happy (co-operative) case compared to uncooperative case.
2828
let htlc_policy = Concrete::<bitcoin::PublicKey>::from_str(&format!("or(10@and(sha256({secret_hash}),pk({redeem_identity})),1@and(older({expiry}),pk({refund_identity})))",
2929
secret_hash = "1111111111111111111111111111111111111111111111111111111111111111",
3030
redeem_identity = "022222222222222222222222222222222222222222222222222222222222222222",
@@ -39,31 +39,34 @@ fn main() {
3939
)
4040
.expect("Resource limits");
4141

42-
// Check whether the descriptor is safe
43-
// This checks whether all spend paths are accessible in bitcoin network.
44-
// It maybe possible that some of the spend require more than 100 elements in Wsh scripts
45-
// Or they contain a combination of timelock and heightlock.
42+
// Check whether the descriptor is safe. This checks whether all spend paths are accessible in
43+
// the Bitcoin network. It may be possible that some of the spend paths require more than 100
44+
// elements in Wsh scripts or they contain a combination of timelock and heightlock.
4645
assert!(htlc_descriptor.sanity_check().is_ok());
4746
assert_eq!(
4847
format!("{}", htlc_descriptor),
4948
"wsh(andor(pk(022222222222222222222222222222222222222222222222222222222222222222),sha256(1111111111111111111111111111111111111111111111111111111111111111),and_v(v:pkh(51814f108670aced2d77c1805ddd6634bc9d4731),older(4444))))#s0qq76ng"
5049
);
5150

51+
// Lift the descriptor into an abstract policy.
5252
assert_eq!(
5353
format!("{}", htlc_descriptor.lift().unwrap()),
5454
"or(and(pkh(4377a5acd66dc5cb67148a24818d1e51fa183bd2),sha256(1111111111111111111111111111111111111111111111111111111111111111)),and(pkh(51814f108670aced2d77c1805ddd6634bc9d4731),older(4444)))"
5555
);
5656

57+
// Get the scriptPpubkey for this Wsh descriptor.
5758
assert_eq!(
5859
format!("{:x}", htlc_descriptor.spk()),
5960
"0020d853877af928a8d2a569c9c0ed14bd16f6a80ce9cccaf8a6150fd8f7f8867ae2"
6061
);
6162

63+
// Encode the Wsh descriptor into a Bitcoin script.
6264
assert_eq!(
6365
format!("{:x}", htlc_descriptor.inner_script()),
6466
"21022222222222222222222222222222222222222222222222222222222222222222ac6476a91451814f108670aced2d77c1805ddd6634bc9d473188ad025c11b26782012088a82011111111111111111111111111111111111111111111111111111111111111118768"
6567
);
6668

69+
// Get the address for this Wsh descriptor.v
6770
assert_eq!(
6871
format!("{}", htlc_descriptor.address(Network::Bitcoin).unwrap()),
6972
"bc1qmpfcw7he9z5d9ftfe8qw699azmm2sr8fen903fs4plv007yx0t3qxfmqv5"

0 commit comments

Comments
 (0)