18
18
use bitcoin:: hashes:: hex:: FromHex ;
19
19
use bitcoin:: hashes:: { hash160, ripemd160, sha256, sha256d} ;
20
20
use std:: collections:: HashSet ;
21
+ #[ cfg( feature = "compiler" ) ]
22
+ use std:: sync:: Arc ;
21
23
use std:: { error, fmt, str} ;
22
24
23
25
use super :: ENTAILMENT_MAX_TERMINALS ;
26
+ #[ cfg( feature = "compiler" ) ]
27
+ use descriptor:: TapTree ;
24
28
use errstr;
25
29
use expression:: { self , FromTree } ;
26
30
use miniscript:: limits:: { HEIGHT_TIME_THRESHOLD , SEQUENCE_LOCKTIME_TYPE_FLAG } ;
@@ -32,8 +36,13 @@ use policy::compiler;
32
36
#[ cfg( feature = "compiler" ) ]
33
37
use policy:: compiler:: CompilerError ;
34
38
#[ cfg( feature = "compiler" ) ]
39
+ use Descriptor ;
40
+ #[ cfg( feature = "compiler" ) ]
35
41
use Miniscript ;
42
+ #[ cfg( feature = "compiler" ) ]
43
+ use Tap ;
36
44
use { Error , ForEach , ForEachKey , MiniscriptKey } ;
45
+
37
46
/// Concrete policy which corresponds directly to a Miniscript structure,
38
47
/// and whose disjunctions are annotated with satisfaction probabilities
39
48
/// to assist the compiler
@@ -128,6 +137,33 @@ impl fmt::Display for PolicyError {
128
137
}
129
138
130
139
impl < Pk : MiniscriptKey > Policy < Pk > {
140
+ /// Single-Node compilation
141
+ #[ cfg( feature = "compiler" ) ]
142
+ fn compile_huffman_taptree ( policy : & Self ) -> Result < TapTree < Pk > , Error > {
143
+ let compilation = policy. compile :: < Tap > ( ) . unwrap ( ) ;
144
+ Ok ( TapTree :: Leaf ( Arc :: new ( compilation) ) )
145
+ }
146
+ /// Extract the Taproot internal_key from policy tree.
147
+ #[ cfg( feature = "compiler" ) ]
148
+ fn extract_key ( policy : & Self , unspendable_key : Option < Pk > ) -> Result < ( Pk , & Policy < Pk > ) , Error > {
149
+ match unspendable_key {
150
+ Some ( key) => Ok ( ( key, policy) ) ,
151
+ None => Err ( errstr ( "No internal key found" ) ) ,
152
+ }
153
+ }
154
+
155
+ /// Compile the [`Tr`] descriptor into optimized [`TapTree`] implementation
156
+ #[ cfg( feature = "compiler" ) ]
157
+ pub fn compile_tr ( & self , unspendable_key : Option < Pk > ) -> Result < Descriptor < Pk > , Error > {
158
+ let ( internal_key, policy) = Self :: extract_key ( self , unspendable_key) . unwrap ( ) ;
159
+ let tree = Descriptor :: new_tr (
160
+ internal_key,
161
+ Some ( Self :: compile_huffman_taptree ( policy) . unwrap ( ) ) ,
162
+ )
163
+ . unwrap ( ) ;
164
+ Ok ( tree)
165
+ }
166
+
131
167
/// Compile the descriptor into an optimized `Miniscript` representation
132
168
#[ cfg( feature = "compiler" ) ]
133
169
pub fn compile < Ctx : ScriptContext > ( & self ) -> Result < Miniscript < Pk , Ctx > , CompilerError > {
0 commit comments