Tighten P2WSHWitnessSPKV0.apply() to only take RawScriptPubKey (#5509)

This commit is contained in:
Chris Stewart 2024-04-06 16:57:21 -05:00 committed by GitHub
parent 4ae9067083
commit 790327639a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View File

@ -1410,7 +1410,7 @@ object P2WSHWitnessSPKV0 extends ScriptFactory[P2WSHWitnessSPKV0] {
fromAsm(Seq(OP_0) ++ pushop ++ Seq(ScriptConstant(hash.bytes)))
}
def apply(spk: ScriptPubKey): P2WSHWitnessSPKV0 = {
def apply(spk: RawScriptPubKey): P2WSHWitnessSPKV0 = {
require(
BitcoinScriptUtil.isOnlyCompressedPubKey(spk),
s"Public key must be compressed to be used in a segwit script, see BIP143")

View File

@ -378,7 +378,16 @@ case class PSBT(
OutputPSBTRecord.RedeemScript(redeemScript)
}
case p2wsh: P2WSHWitnessSPKV0 =>
val scriptHash = P2WSHWitnessSPKV0(redeemScript).scriptHash
val scriptHash = {
redeemScript match {
case raw: RawScriptPubKey => P2WSHWitnessSPKV0(raw).scriptHash
case nonraw @ (_: P2SHScriptPubKey | _: P2WPKHWitnessSPKV0 |
_: TaprootScriptPubKey | _: UnassignedWitnessScriptPubKey |
_: P2WSHWitnessSPKV0) =>
throw new IllegalArgumentException(
s"Cannot make p2wsh from non raw spk=$nonraw")
}
}
if (scriptHash != p2wsh.scriptHash) {
throw new IllegalArgumentException(
s"The given script's hash does not match the expected script has, got: $scriptHash, expected ${p2wsh.scriptHash}")

View File

@ -346,7 +346,7 @@ sealed abstract class ScriptGenerators {
/** Generates a random P2WSHWitnessSPKV0 as well as it's corresponding private keys and redeem script */
def p2wshSPKV0: Gen[(P2WSHWitnessSPKV0, Seq[ECPrivateKey], ScriptPubKey)] =
randomNonP2SHScriptPubKey
rawScriptPubKey
.suchThat { case (spk, _) =>
!redeemScriptTooBig(spk)
}