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