File tree Expand file tree Collapse file tree 2 files changed +29
-9
lines changed Expand file tree Collapse file tree 2 files changed +29
-9
lines changed Original file line number Diff line number Diff line change @@ -245,15 +245,24 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
245245 // TODO: We might require other compile errors for Taproot.
246246 #[ cfg( feature = "compiler" ) ]
247247 pub fn compile_tr ( & self , unspendable_key : Option < Pk > ) -> Result < Descriptor < Pk > , Error > {
248- let ( internal_key, policy) = self . clone ( ) . extract_key ( unspendable_key) ?;
249- let tree = Descriptor :: new_tr (
250- internal_key,
251- match policy {
252- Policy :: Trivial => None ,
253- policy => Some ( policy. compile_tr_policy ( ) ?) ,
254- } ,
255- ) ?;
256- Ok ( tree)
248+ self . is_valid ( ) ?; // Check for validity
249+ match self . is_safe_nonmalleable ( ) {
250+ ( false , _) => Err ( Error :: from ( CompilerError :: TopLevelNonSafe ) ) ,
251+ ( _, false ) => Err ( Error :: from (
252+ CompilerError :: ImpossibleNonMalleableCompilation ,
253+ ) ) ,
254+ _ => {
255+ let ( internal_key, policy) = self . clone ( ) . extract_key ( unspendable_key) ?;
256+ let tree = Descriptor :: new_tr (
257+ internal_key,
258+ match policy {
259+ Policy :: Trivial => None ,
260+ policy => Some ( policy. compile_tr_policy ( ) ?) ,
261+ } ,
262+ ) ?;
263+ Ok ( tree)
264+ }
265+ }
257266 }
258267
259268 /// Compile the descriptor into an optimized `Miniscript` representation
Original file line number Diff line number Diff line change @@ -398,5 +398,16 @@ mod tests {
398398 Descriptor :: new_tr ( unspendable_key. clone ( ) , Some ( tree) ) . unwrap ( ) ;
399399 assert_eq ! ( descriptor, expected_descriptor) ;
400400 }
401+
402+ {
403+ // Invalid policy compilation (Duplicate PubKeys)
404+ let policy: Concrete < String > = policy_str ! ( "or(and(pk(A),pk(B)),and(pk(A),pk(D)))" ) ;
405+ let descriptor = policy. compile_tr ( Some ( unspendable_key. clone ( ) ) ) ;
406+
407+ assert_eq ! (
408+ descriptor. unwrap_err( ) . to_string( ) ,
409+ "Policy contains duplicate keys"
410+ ) ;
411+ }
401412 }
402413}
You can’t perform that action at this time.
0 commit comments