@@ -196,15 +196,66 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
196196 }
197197 }
198198
199+ /// Computes an upper bound on the difference between a non-satisfied
200+ /// `TxIn`'s `segwit_weight` and a satisfied `TxIn`'s `segwit_weight`
201+ ///
202+ /// Since this method uses `segwit_weight` instead of `legacy_weight`,
203+ /// if you want to include only legacy inputs in your transaction,
204+ /// you should remove 1WU from each input's `max_weight_to_satisfy`
205+ /// for a more accurate estimate.
206+ ///
207+ /// Assumes all ec-signatures are 73 bytes, including push opcode and
208+ /// sighash suffix.
209+ ///
210+ /// # Errors
211+ /// When the descriptor is impossible to safisfy (ex: sh(OP_FALSE)).
212+ pub fn max_weight_to_satisfy ( & self ) -> Result < usize , Error > {
213+ let ( scriptsig_size, witness_size) = match self . inner {
214+ // add weighted script sig, len byte stays the same
215+ ShInner :: Wsh ( ref wsh) => {
216+ // scriptSig: OP_34 <OP_0 OP_32 <32-byte-hash>>
217+ let scriptsig_size = 1 + 1 + 1 + 32 ;
218+ let witness_size = wsh. max_weight_to_satisfy ( ) ?;
219+ ( scriptsig_size, witness_size)
220+ }
221+ ShInner :: SortedMulti ( ref smv) => {
222+ let ss = smv. script_size ( ) ;
223+ let ps = push_opcode_size ( ss) ;
224+ let scriptsig_size = ps + ss + smv. max_satisfaction_size ( ) ;
225+ ( scriptsig_size, 0 )
226+ }
227+ // add weighted script sig, len byte stays the same
228+ ShInner :: Wpkh ( ref wpkh) => {
229+ // scriptSig: OP_22 <OP_0 OP_20 <20-byte-hash>>
230+ let scriptsig_size = 1 + 1 + 1 + 20 ;
231+ let witness_size = wpkh. max_weight_to_satisfy ( ) ;
232+ ( scriptsig_size, witness_size)
233+ }
234+ ShInner :: Ms ( ref ms) => {
235+ let ss = ms. script_size ( ) ;
236+ let ps = push_opcode_size ( ss) ;
237+ let scriptsig_size = ps + ss + ms. max_satisfaction_size ( ) ?;
238+ ( scriptsig_size, 0 )
239+ }
240+ } ;
241+
242+ // scriptSigLen varint difference between non-satisfied (0) and satisfied
243+ let scriptsig_varint_diff = varint_len ( scriptsig_size) - varint_len ( 0 ) ;
244+
245+ Ok ( 4 * ( scriptsig_varint_diff + scriptsig_size) + witness_size)
246+ }
247+
199248 /// Computes an upper bound on the weight of a satisfying witness to the
200249 /// transaction.
201250 ///
202- /// Assumes all ec- signatures are 73 bytes, including push opcode and
251+ /// Assumes all ECDSA signatures are 73 bytes, including push opcode and
203252 /// sighash suffix. Includes the weight of the VarInts encoding the
204253 /// scriptSig and witness stack length.
205254 ///
206255 /// # Errors
207256 /// When the descriptor is impossible to safisfy (ex: sh(OP_FALSE)).
257+ #[ deprecated( note = "use max_weight_to_satisfy instead" ) ]
258+ #[ allow( deprecated) ]
208259 pub fn max_satisfaction_weight ( & self ) -> Result < usize , Error > {
209260 Ok ( match self . inner {
210261 // add weighted script sig, len byte stays the same
0 commit comments