Skip to content

Commit c79235a

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

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

examples/parse.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! Example: Parsing a descriptor from a string
15+
//! Example: Parsing a descriptor from a string.
1616
1717
use bitcoin;
1818
use miniscript;
@@ -21,54 +21,49 @@ use miniscript::{descriptor::DescriptorType, Descriptor, DescriptorTrait};
2121
use std::str::FromStr;
2222

2323
fn main() {
24-
let my_descriptor = miniscript::Descriptor::<bitcoin::PublicKey>::from_str(
24+
let desc = miniscript::Descriptor::<bitcoin::PublicKey>::from_str(
2525
"wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202))",
2626
)
2727
.unwrap();
2828

29-
// Check whether the descriptor is safe
30-
// This checks whether all spend paths are accessible in bitcoin network.
31-
// It maybe possible that some of the spend require more than 100 elements in Wsh scripts
32-
// Or they contain a combination of timelock and heightlock.
33-
assert!(my_descriptor.sanity_check().is_ok());
29+
// Check whether the descriptor is safe. This checks whether all spend paths are accessible in
30+
// the Bitcoin network. It may be possible that some of the spend paths require more than 100
31+
// elements in Wsh scripts or they contain a combination of timelock and heightlock.
32+
assert!(desc.sanity_check().is_ok());
3433

3534
// Compute the script pubkey. As mentioned in the documentation, script_pubkey only fails
36-
// for Tr descriptors that don't have some pre-computed data
35+
// for Tr descriptors that don't have some pre-computed data.
3736
assert_eq!(
38-
format!("{:x}", my_descriptor.script_pubkey()),
37+
format!("{:x}", desc.script_pubkey()),
3938
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
4039
);
4140

42-
// Another way to compute script pubkey
43-
// We can also compute the type of descriptor
44-
let desc_type = my_descriptor.desc_type();
41+
// As another way to compute script pubkey; we can also compute the type of the descriptor.
42+
let desc_type = desc.desc_type();
4543
assert_eq!(desc_type, DescriptorType::Wsh);
46-
// Since we know the type of descriptor, we can get the Wsh struct from Descriptor
47-
// This allows us to call infallible methods for getting script pubkey
48-
if let Descriptor::Wsh(wsh) = &my_descriptor {
44+
// Since we know the type of descriptor, we can get the Wsh struct from Descriptor. This allows
45+
// us to call infallible methods for getting script pubkey.
46+
if let Descriptor::Wsh(wsh) = &desc {
4947
assert_eq!(
5048
format!("{:x}", wsh.spk()),
5149
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
5250
);
53-
} else {
54-
// We checked for the descriptor type earlier
5551
}
5652

57-
// Get the inner script inside the descriptor
53+
// Get the inner script inside the descriptor.
5854
assert_eq!(
5955
format!(
6056
"{:x}",
61-
my_descriptor
62-
.explicit_script()
57+
desc.explicit_script()
6358
.expect("Wsh descriptors have inner scripts")
6459
),
6560
"21020202020202020202020202020202020202020202020202020202020202020202ac"
6661
);
6762

63+
// In a similar fashion we can parse a wrapped segwit script.
6864
let desc = miniscript::Descriptor::<bitcoin::PublicKey>::from_str(
6965
"sh(wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202)))",
7066
)
7167
.unwrap();
72-
7368
assert!(desc.desc_type() == DescriptorType::ShWsh);
7469
}

0 commit comments

Comments
 (0)