core: Add TaprootWitness.sigVersion (#5954)

* core: Add TaprootWitness.sigVersion

* Remove uneeded pattern match

* Be more precise with types
This commit is contained in:
Chris Stewart 2025-03-07 13:25:10 -06:00 committed by GitHub
parent 6287e41281
commit 6d240d5c54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 6 deletions

View file

@ -448,12 +448,8 @@ case class TaprootTxSigComponent(
override val witnessVersion: WitnessVersion1.type = WitnessVersion1 override val witnessVersion: WitnessVersion1.type = WitnessVersion1
override def sigVersion: SigVersionTaproot = { override def sigVersion: SigVersionTaproot = witness.sigVersion
witness match {
case _: TaprootKeyPath => SigVersionTaprootKeySpend
case _: TaprootScriptPath | _: TaprootUnknownPath => SigVersionTapscript
}
}
} }
object BaseTxSigComponent { object BaseTxSigComponent {

View file

@ -1,6 +1,7 @@
package org.bitcoins.core.protocol.script package org.bitcoins.core.protocol.script
import org.bitcoins.core.protocol.CompactSizeUInt import org.bitcoins.core.protocol.CompactSizeUInt
import org.bitcoins.core.protocol.script.LeafVersion.UnknownLeafVersion
import org.bitcoins.core.script.constant.{OP_0, ScriptNumberOperation} import org.bitcoins.core.script.constant.{OP_0, ScriptNumberOperation}
import org.bitcoins.core.serializers.script.{ import org.bitcoins.core.serializers.script.{
RawScriptWitnessParser, RawScriptWitnessParser,
@ -236,6 +237,8 @@ sealed trait TaprootWitness extends ScriptWitness {
CryptoUtil.sha256(cmpct.bytes ++ annex) CryptoUtil.sha256(cmpct.bytes ++ annex)
} }
} }
def sigVersion: SigVersionTaproot
} }
object TaprootWitness extends Factory[TaprootWitness] { object TaprootWitness extends Factory[TaprootWitness] {
@ -277,6 +280,9 @@ case class TaprootKeyPath(
Vector(signature.bytes) Vector(signature.bytes)
} }
} }
override def sigVersion: SigVersionTaprootKeySpend.type =
SigVersionTaprootKeySpend
} }
object TaprootKeyPath extends Factory[TaprootKeyPath] { object TaprootKeyPath extends Factory[TaprootKeyPath] {
@ -401,6 +407,14 @@ case class TaprootScriptPath(stack: Vector[ByteVector]) extends TaprootWitness {
* defined as in BIP340. Fail if this point is not on the curve. * defined as in BIP340. Fail if this point is not on the curve.
*/ */
def p: XOnlyPubKey = controlBlock.p def p: XOnlyPubKey = controlBlock.p
def leafVersion: LeafVersion = controlBlock.leafVersion
override def sigVersion: SigVersionTapscript.type = leafVersion match {
case LeafVersion.Tapscript => SigVersionTapscript
case UnknownLeafVersion(toByte) =>
sys.error(s"Unknown leaf version=$toByte, cannot determine sigVersion")
}
} }
object TaprootScriptPath extends Factory[TaprootScriptPath] { object TaprootScriptPath extends Factory[TaprootScriptPath] {
@ -567,4 +581,9 @@ case class TaprootUnknownPath(stack: Vector[ByteVector])
None None
} }
} }
override def sigVersion: SigVersionTaproot = {
// default to tapscript for unknown leafver and sigversion?
SigVersionTapscript
}
} }