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