@@ -82,15 +82,45 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {
8282 Ok ( ( ) )
8383 }
8484
85+ /// Computes an upper bound on the difference in weight between a
86+ /// non-satisfied `TxIn` (with empty `scriptSig` and `witness` fields) and a
87+ /// satisfied `TxIn`.
88+ ///
89+ /// Assumes all ECDSA signatures are 73 bytes, including push opcode and
90+ /// sighash suffix.
91+ ///
92+ /// # Errors
93+ /// When the descriptor is impossible to safisfy (ex: sh(OP_FALSE)).
94+ pub fn max_weight_to_satisfy ( & self ) -> Result < usize , Error > {
95+ let ( redeem_script_size, max_sat_elems, max_sat_size) = match self . inner {
96+ WshInner :: SortedMulti ( ref smv) => (
97+ smv. script_size ( ) ,
98+ smv. max_satisfaction_witness_elements ( ) ,
99+ smv. max_satisfaction_size ( ) ,
100+ ) ,
101+ WshInner :: Ms ( ref ms) => (
102+ ms. script_size ( ) ,
103+ ms. max_satisfaction_witness_elements ( ) ?,
104+ ms. max_satisfaction_size ( ) ?,
105+ ) ,
106+ } ;
107+ // stack size varint difference between non-satisfied (0) and satisfied
108+ // `max_sat_elems` is inclusive of the "witness script" (redeem script)
109+ let stack_varint_diff = varint_len ( max_sat_elems) - varint_len ( 0 ) ;
110+
111+ Ok ( stack_varint_diff + varint_len ( redeem_script_size) + redeem_script_size + max_sat_size)
112+ }
113+
85114 /// Computes an upper bound on the weight of a satisfying witness to the
86115 /// transaction.
87116 ///
88- /// Assumes all ec- signatures are 73 bytes, including push opcode and
117+ /// Assumes all ECDSA signatures are 73 bytes, including push opcode and
89118 /// sighash suffix. Includes the weight of the VarInts encoding the
90119 /// scriptSig and witness stack length.
91120 ///
92121 /// # Errors
93122 /// When the descriptor is impossible to safisfy (ex: sh(OP_FALSE)).
123+ #[ deprecated( note = "use max_weight_to_satisfy instead" ) ]
94124 pub fn max_satisfaction_weight ( & self ) -> Result < usize , Error > {
95125 let ( script_size, max_sat_elems, max_sat_size) = match self . inner {
96126 WshInner :: SortedMulti ( ref smv) => (
@@ -325,10 +355,24 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
325355 }
326356 }
327357
358+ /// Computes an upper bound on the difference in weight between a
359+ /// non-satisfied `TxIn` (with empty `scriptSig` and `witness` fields) and a
360+ /// satisfied `TxIn`.
361+ ///
362+ /// Assumes all ECDSA signatures are 73 bytes, including push opcode and
363+ /// sighash suffix.
364+ pub fn max_weight_to_satisfy ( & self ) -> usize {
365+ // stack items: <varint(sig+sigHash)> <sig(71)+sigHash(1)> <varint(pubkey)> <pubkey>
366+ let stack_items_size = 73 + Segwitv0 :: pk_len ( & self . pk ) ;
367+ // stackLen varint difference between non-satisfied (0) and satisfied
368+ let stack_varint_diff = varint_len ( 2 ) - varint_len ( 0 ) ;
369+ stack_varint_diff + stack_items_size
370+ }
371+
328372 /// Computes an upper bound on the weight of a satisfying witness to the
329373 /// transaction.
330374 ///
331- /// Assumes all ec- signatures are 73 bytes, including push opcode and
375+ /// Assumes all ECDSA signatures are 73 bytes, including push opcode and
332376 /// sighash suffix. Includes the weight of the VarInts encoding the
333377 /// scriptSig and witness stack length.
334378 pub fn max_satisfaction_weight ( & self ) -> usize {
0 commit comments