Skip to content

Commit 054313c

Browse files
committed
Stack len for legacy max_satisfaction_weight calculations
As per the definiton of `max_satisfaction_weight`, we should take into consideration the varint that records witness stack size. Even legacy spends may require this field if the tx has at least 1 segwit spend, so we should have it in our calculations.
1 parent 2f1535e commit 054313c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/descriptor/bare.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
7777
/// When the descriptor is impossible to safisfy (ex: sh(OP_FALSE)).
7878
pub fn max_satisfaction_weight(&self) -> Result<usize, Error> {
7979
let scriptsig_len = self.ms.max_satisfaction_size()?;
80-
Ok(4 * (varint_len(scriptsig_len) + scriptsig_len))
80+
Ok(4 * (varint_len(scriptsig_len) + scriptsig_len) +
81+
// witness stack size varint (always 1WU for non-segwit)
82+
1)
8183
}
8284
}
8385

@@ -219,7 +221,9 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
219221
/// sighash suffix. Includes the weight of the VarInts encoding the
220222
/// scriptSig and witness stack length.
221223
pub fn max_satisfaction_weight(&self) -> usize {
222-
4 * (1 + 73 + BareCtx::pk_len(&self.pk))
224+
4 * (1 + 73 + BareCtx::pk_len(&self.pk)) +
225+
// witness stack size varint (always 1 WU for non-segwit)
226+
1
223227
}
224228
}
225229

src/descriptor/sh.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,19 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
222222
let ss = smv.script_size();
223223
let ps = push_opcode_size(ss);
224224
let scriptsig_len = ps + ss + smv.max_satisfaction_size();
225-
4 * (varint_len(scriptsig_len) + scriptsig_len)
225+
4 * (varint_len(scriptsig_len) + scriptsig_len) +
226+
// witnessData stack length
227+
1
226228
}
227229
// add weighted script sig, len byte stays the same
228230
ShInner::Wpkh(ref wpkh) => 4 * 23 + wpkh.max_satisfaction_weight(),
229231
ShInner::Ms(ref ms) => {
230232
let ss = ms.script_size();
231233
let ps = push_opcode_size(ss);
232234
let scriptsig_len = ps + ss + ms.max_satisfaction_size()?;
233-
4 * (varint_len(scriptsig_len) + scriptsig_len)
235+
4 * (varint_len(scriptsig_len) + scriptsig_len) +
236+
// witnessData stack length
237+
1
234238
}
235239
})
236240
}

0 commit comments

Comments
 (0)