From ef50becf1b18a4ef8a78d2d30364b73c40b5ab48 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Mon, 11 Jul 2022 06:41:37 -0500 Subject: [PATCH] Enforce MAX_PUSH_SIZE for taproot (#4486) --- .../interpreter/ScriptInterpreter.scala | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala b/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala index 3bb65ada21..92800b6e24 100644 --- a/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala +++ b/core/src/main/scala/org/bitcoins/core/script/interpreter/ScriptInterpreter.scala @@ -670,7 +670,6 @@ sealed abstract class ScriptInterpreter { scriptPubKeyExecutedProgram.flags.exists( _ == ScriptVerifyDiscourageUpgradableTaprootVersion) if (controlBlock.isTapLeafMask) { - //drop the control block & script in the witness val stackNoControlBlockOrScript = { if (scriptPubKeyExecutedProgram.getAnnexHashOpt.isDefined) { @@ -682,17 +681,27 @@ sealed abstract class ScriptInterpreter { stack.tail.tail } } - val newProgram = PreExecutionScriptProgram( - txSignatureComponent = taprootTxSigComponent, - stack = stackNoControlBlockOrScript.toList, - script = rebuiltSPK.asm.toList, - originalScript = rebuiltSPK.asm.toList, - altStack = Nil, - flags = taprootTxSigComponent.flags - ) - val evaluated = executeProgram(newProgram) - val segwitChecks = postSegWitProgramChecks(evaluated) - Success(segwitChecks) + if ( + stackNoControlBlockOrScript.exists( + _.bytes.length > MAX_PUSH_SIZE) + ) { + val fail = + scriptPubKeyExecutedProgram.failExecution( + ScriptErrorPushSize) + Success(fail) + } else { + val newProgram = PreExecutionScriptProgram( + txSignatureComponent = taprootTxSigComponent, + stack = stackNoControlBlockOrScript.toList, + script = rebuiltSPK.asm.toList, + originalScript = rebuiltSPK.asm.toList, + altStack = Nil, + flags = taprootTxSigComponent.flags + ) + val evaluated = executeProgram(newProgram) + val segwitChecks = postSegWitProgramChecks(evaluated) + Success(segwitChecks) + } } else if (isDiscouragedTaprootVersion) { val p = scriptPubKeyExecutedProgram.failExecution( ScriptErrorDiscourageUpgradableTaprootVersion)