diff --git a/txscript/bench_test.go b/txscript/bench_test.go index 11275ba2..b8fa7506 100644 --- a/txscript/bench_test.go +++ b/txscript/bench_test.go @@ -234,7 +234,6 @@ func BenchmarkIsAnyKindOfScriptHash(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - pops, _ := parseScript(script) - _ = isAnyKindOfScriptHash(pops) + _ = isAnyKindOfScriptHash(script) } } diff --git a/txscript/engine.go b/txscript/engine.go index 8d62de33..1cc101bd 100644 --- a/txscript/engine.go +++ b/txscript/engine.go @@ -718,7 +718,9 @@ func NewEngine(scriptPubKey []byte, tx *wire.MsgTx, txIdx int, flags ScriptFlags vm.scriptIdx++ } - if isAnyKindOfScriptHash(vm.scripts[1]) { + // The signature script must only contain data pushes for P2SH which is + // determined based on the form of the public key script. + if isAnyKindOfScriptHash(scriptPubKey) { // Only accept input scripts that push data for P2SH. if !isPushOnly(vm.scripts[0]) { return nil, scriptError(ErrNotPushOnly, diff --git a/txscript/script.go b/txscript/script.go index caa22061..87644ca1 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -112,20 +112,21 @@ func isScriptHashScript(script []byte) bool { return extractScriptHash(script) != nil } -// isStakeScriptHash returns whether or not the passed script is a stake -// pay-to-script-hash script. -func isStakeScriptHash(pops []parsedOpcode) bool { - return len(pops) == 4 && - isStakeOpcode(pops[0].opcode.value) && - pops[1].opcode.value == OP_HASH160 && - pops[2].opcode.value == OP_DATA_20 && - pops[3].opcode.value == OP_EQUAL +// isStakeScriptHashScript returns whether or not the passed script is a +// stake-tagged pay-to-script-hash script. +func isStakeScriptHashScript(script []byte) bool { + return len(script) == 24 && + isStakeOpcode(script[0]) && + script[1] == OP_HASH160 && + script[2] == OP_DATA_20 && + script[23] == OP_EQUAL } // isAnyKindOfScriptHash returns whether or not the passed script is either a -// regular pay-to-script-hash script or a stake pay-to-script-hash script. -func isAnyKindOfScriptHash(pops []parsedOpcode) bool { - return isScriptHash(pops) || isStakeScriptHash(pops) +// regular pay-to-script-hash script or a stake-tagged pay-to-script-hash +// script. +func isAnyKindOfScriptHash(script []byte) bool { + return isScriptHashScript(script) || isStakeScriptHashScript(script) } // HasP2SHScriptSigStakeOpCodes returns an error is the p2sh script has either diff --git a/txscript/script_test.go b/txscript/script_test.go index cd22e3cd..c658a8e2 100644 --- a/txscript/script_test.go +++ b/txscript/script_test.go @@ -4123,9 +4123,8 @@ func TestIsAnyKindOfScriptHash(t *testing.T) { for _, test := range scriptClassTests { script := mustParseShortForm(test.script) - pops, _ := parseScript(script) want := (test.class == ScriptHashTy || test.subClass == ScriptHashTy) - p2sh := isAnyKindOfScriptHash(pops) + p2sh := isAnyKindOfScriptHash(script) if p2sh != want { t.Errorf("%s: epxected p2sh %v, got %v", test.name, want, p2sh)