Skip to content

Commit be83ac7

Browse files
committed
Update Control block verification
1 parent 1929518 commit be83ac7

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/interpreter/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl fmt::Display for Error {
246246
Error::Secp(ref e) => fmt::Display::fmt(e, f),
247247
Error::SchnorrSig(ref s) => write!(f, "Schnorr sig error: {}", s),
248248
Error::SighashError(ref e) => fmt::Display::fmt(e, f),
249-
Error::TapAnnexUnsupported => f.write_str("Encounter Annex element"),
249+
Error::TapAnnexUnsupported => f.write_str("Encountered annex element"),
250250
Error::UncompressedPubkey => {
251251
f.write_str("uncompressed pubkey in non-legacy descriptor")
252252
}

src/interpreter/inner.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -208,25 +208,30 @@ pub(super) fn from_txdata<'txin>(
208208
} else {
209209
let output_key = bitcoin::XOnlyPublicKey::from_slice(&spk[2..])
210210
.map_err(|_| Error::XOnlyPublicKeyParseError)?;
211-
if wit_stack.len() == 1 {
212-
// Key spend
213-
Ok((
211+
let has_annex = wit_stack
212+
.last()
213+
.and_then(|x| x.as_push().ok())
214+
.map(|x| x.len() > 0 && x[0] == TAPROOT_ANNEX_PREFIX)
215+
.unwrap_or(false);
216+
let has_annex = has_annex && (wit_stack.len() >= 2);
217+
if has_annex {
218+
// Annex is non-standard, bitcoin consensus rules ignore it.
219+
// Our sighash structure and signature verification
220+
// does not support annex, return error
221+
return Err(Error::TapAnnexUnsupported);
222+
}
223+
match wit_stack.len() {
224+
0 => Err(Error::UnexpectedStackEnd),
225+
1 => Ok((
214226
Inner::PublicKey(output_key.into(), PubkeyType::Tr),
215227
wit_stack,
216-
None, // Tr script code None
217-
))
218-
} else {
219-
// wit_stack.len() >=2
220-
// Check for annex
221-
let ctrl_blk = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
222-
let ctrl_blk = ctrl_blk.as_push()?;
223-
let tap_script = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
224-
if ctrl_blk.len() > 0 && ctrl_blk[0] == TAPROOT_ANNEX_PREFIX {
225-
// Annex is non-standard, bitcoin consensus rules ignore it.
226-
// Our sighash structure and signature verification
227-
// does not support annex, return error
228-
return Err(Error::TapAnnexUnsupported);
229-
} else if wit_stack.len() >= 2 {
228+
None, // Tr key spend script code None
229+
)),
230+
_ => {
231+
// Script spend
232+
let ctrl_blk = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
233+
let ctrl_blk = ctrl_blk.as_push()?;
234+
let tap_script = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
230235
let ctrl_blk = ControlBlock::from_slice(ctrl_blk)
231236
.map_err(|e| Error::ControlBlockParse(e))?;
232237
let tap_script = script_from_stackelem::<Tap>(&tap_script)?;
@@ -255,8 +260,6 @@ pub(super) fn from_txdata<'txin>(
255260
} else {
256261
return Err(Error::ControlBlockVerificationError);
257262
}
258-
} else {
259-
return Err(Error::UnexpectedStackBoolean);
260263
}
261264
}
262265
}

0 commit comments

Comments
 (0)