@@ -82,8 +82,20 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
8282 fmt_2 ( f, "or_i(" , l, r, is_debug)
8383 }
8484 Terminal :: Thresh ( k, ref subs) => fmt_n ( f, "thresh(" , k, subs, is_debug) ,
85- Terminal :: Multi ( k, ref keys) => fmt_n ( f, "multi(" , k, keys, is_debug) ,
86- Terminal :: MultiA ( k, ref keys) => fmt_n ( f, "multi_a(" , k, keys, is_debug) ,
85+ Terminal :: Multi ( ref thresh) => {
86+ if is_debug {
87+ fmt:: Debug :: fmt ( & thresh. debug ( "multi" , true ) , f)
88+ } else {
89+ fmt:: Display :: fmt ( & thresh. display ( "multi" , true ) , f)
90+ }
91+ }
92+ Terminal :: MultiA ( ref thresh) => {
93+ if is_debug {
94+ fmt:: Debug :: fmt ( & thresh. debug ( "multi_a" , true ) , f)
95+ } else {
96+ fmt:: Display :: fmt ( & thresh. display ( "multi_a" , true ) , f)
97+ }
98+ }
8799 // wrappers
88100 _ => {
89101 if let Some ( ( ch, sub) ) = self . wrap_char ( ) {
@@ -314,27 +326,16 @@ impl<Pk: FromStrKey, Ctx: ScriptContext> crate::expression::FromTree for Termina
314326
315327 Ok ( Terminal :: Thresh ( k, subs?) )
316328 }
317- ( "multi" , n) | ( "multi_a" , n) => {
318- if n == 0 {
319- return Err ( errstr ( "no arguments given" ) ) ;
320- }
321- let k = expression:: terminal ( & top. args [ 0 ] , expression:: parse_num) ? as usize ;
322- if k > n - 1 {
323- return Err ( errstr ( "higher threshold than there were keys in multi" ) ) ;
324- }
325-
326- let pks: Result < Vec < Pk > , _ > = top. args [ 1 ..]
327- . iter ( )
328- . map ( |sub| expression:: terminal ( sub, Pk :: from_str) )
329- . collect ( ) ;
330-
331- if frag_name == "multi" {
332- pks. map ( |pks| Terminal :: Multi ( k, pks) )
333- } else {
334- // must be multi_a
335- pks. map ( |pks| Terminal :: MultiA ( k, pks) )
336- }
337- }
329+ ( "multi" , _) => top
330+ . to_null_threshold ( )
331+ . map_err ( Error :: ParseThreshold ) ?
332+ . translate_by_index ( |i| expression:: terminal ( & top. args [ 1 + i] , Pk :: from_str) )
333+ . map ( Terminal :: Multi ) ,
334+ ( "multi_a" , _) => top
335+ . to_null_threshold ( )
336+ . map_err ( Error :: ParseThreshold ) ?
337+ . translate_by_index ( |i| expression:: terminal ( & top. args [ 1 + i] , Pk :: from_str) )
338+ . map ( Terminal :: MultiA ) ,
338339 _ => Err ( Error :: Unexpected ( format ! (
339340 "{}({} args) while parsing Miniscript" ,
340341 top. name,
@@ -483,27 +484,27 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
483484 . push_int ( k as i64 )
484485 . push_opcode ( opcodes:: all:: OP_EQUAL )
485486 }
486- Terminal :: Multi ( k , ref keys ) => {
487+ Terminal :: Multi ( ref thresh ) => {
487488 debug_assert ! ( Ctx :: sig_type( ) == SigType :: Ecdsa ) ;
488- builder = builder. push_int ( k as i64 ) ;
489- for pk in keys {
489+ builder = builder. push_int ( thresh . k ( ) as i64 ) ;
490+ for pk in thresh . data ( ) {
490491 builder = builder. push_key ( & pk. to_public_key ( ) ) ;
491492 }
492493 builder
493- . push_int ( keys . len ( ) as i64 )
494+ . push_int ( thresh . n ( ) as i64 )
494495 . push_opcode ( opcodes:: all:: OP_CHECKMULTISIG )
495496 }
496- Terminal :: MultiA ( k , ref keys ) => {
497+ Terminal :: MultiA ( ref thresh ) => {
497498 debug_assert ! ( Ctx :: sig_type( ) == SigType :: Schnorr ) ;
498499 // keys must be atleast len 1 here, guaranteed by typing rules
499- builder = builder. push_ms_key :: < _ , Ctx > ( & keys [ 0 ] ) ;
500+ builder = builder. push_ms_key :: < _ , Ctx > ( & thresh . data ( ) [ 0 ] ) ;
500501 builder = builder. push_opcode ( opcodes:: all:: OP_CHECKSIG ) ;
501- for pk in keys . iter ( ) . skip ( 1 ) {
502+ for pk in thresh . iter ( ) . skip ( 1 ) {
502503 builder = builder. push_ms_key :: < _ , Ctx > ( pk) ;
503504 builder = builder. push_opcode ( opcodes:: all:: OP_CHECKSIGADD ) ;
504505 }
505506 builder
506- . push_int ( k as i64 )
507+ . push_int ( thresh . k ( ) as i64 )
507508 . push_opcode ( opcodes:: all:: OP_NUMEQUAL )
508509 }
509510 }
0 commit comments