diff --git a/txscript/script.go b/txscript/script.go index 59dc2e27..5968cec7 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -100,11 +100,7 @@ func isWitnessScriptHash(pops []parsedOpcode) bool { // IsPayToWitnessScriptHash returns true if the is in the standard // pay-to-witness-script-hash (P2WSH) format, false otherwise. func IsPayToWitnessScriptHash(script []byte) bool { - pops, err := parseScript(script) - if err != nil { - return false - } - return isWitnessScriptHash(pops) + return isWitnessScriptHashScript(script) } // IsPayToWitnessPubKeyHash returns true if the is in the standard diff --git a/txscript/standard.go b/txscript/standard.go index 513c9ce1..a53180f1 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -421,6 +421,28 @@ func isWitnessPubKeyHashScript(script []byte) bool { return extractWitnessPubKeyHash(script) != nil } +// extractWitnessScriptHash extracts the witness script hash from the passed +// script if it is standard pay-to-witness-script-hash script. It will return +// nil otherwise. +func extractWitnessScriptHash(script []byte) []byte { + // A pay-to-witness-script-hash script is of the form: + // OP_0 OP_DATA_32 <32-byte-hash> + if len(script) == 34 && + script[0] == OP_0 && + script[1] == OP_DATA_32 { + + return script[2:34] + } + + return nil +} + +// isWitnessScriptHashScript returns whether or not the passed script is a +// standard pay-to-witness-script-hash script. +func isWitnessScriptHashScript(script []byte) bool { + return extractWitnessScriptHash(script) != nil +} + // isNullData returns true if the passed script is a null data transaction, // false otherwise. func isNullData(pops []parsedOpcode) bool {