12
12
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13
13
//
14
14
15
- //! Example: Parsing a descriptor from a string
15
+ //! Example: Parsing a descriptor from a string.
16
16
17
17
use bitcoin;
18
18
use miniscript;
@@ -21,54 +21,49 @@ use miniscript::{descriptor::DescriptorType, Descriptor, DescriptorTrait};
21
21
use std:: str:: FromStr ;
22
22
23
23
fn main ( ) {
24
- let my_descriptor = miniscript:: Descriptor :: < bitcoin:: PublicKey > :: from_str (
24
+ let desc = miniscript:: Descriptor :: < bitcoin:: PublicKey > :: from_str (
25
25
"wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202))" ,
26
26
)
27
27
. unwrap ( ) ;
28
28
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( ) ) ;
34
33
35
34
// 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.
37
36
assert_eq ! (
38
- format!( "{:x}" , my_descriptor . script_pubkey( ) ) ,
37
+ format!( "{:x}" , desc . script_pubkey( ) ) ,
39
38
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
40
39
) ;
41
40
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 ( ) ;
45
43
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 {
49
47
assert_eq ! (
50
48
format!( "{:x}" , wsh. spk( ) ) ,
51
49
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
52
50
) ;
53
- } else {
54
- // We checked for the descriptor type earlier
55
51
}
56
52
57
- // Get the inner script inside the descriptor
53
+ // Get the inner script inside the descriptor.
58
54
assert_eq ! (
59
55
format!(
60
56
"{:x}" ,
61
- my_descriptor
62
- . explicit_script( )
57
+ desc. explicit_script( )
63
58
. expect( "Wsh descriptors have inner scripts" )
64
59
) ,
65
60
"21020202020202020202020202020202020202020202020202020202020202020202ac"
66
61
) ;
67
62
63
+ // In a similar fashion we can parse a wrapped segwit script.
68
64
let desc = miniscript:: Descriptor :: < bitcoin:: PublicKey > :: from_str (
69
65
"sh(wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202)))" ,
70
66
)
71
67
. unwrap ( ) ;
72
-
73
68
assert ! ( desc. desc_type( ) == DescriptorType :: ShWsh ) ;
74
69
}
0 commit comments