From a3166c8d9b0b2ff66a33fa63406a13d1351851b1 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 19 Apr 2019 19:43:43 -0700 Subject: [PATCH] txscript: Optimize ExtractWitnessProgramInfo --- txscript/script.go | 13 +++---------- txscript/standard.go | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/txscript/script.go b/txscript/script.go index 57c7b9be..9d7de56c 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -131,23 +131,16 @@ func IsNullData(script []byte) bool { // ExtractWitnessProgramInfo attempts to extract the witness program version, // as well as the witness program itself from the passed script. func ExtractWitnessProgramInfo(script []byte) (int, []byte, error) { - pops, err := parseScript(script) - if err != nil { - return 0, nil, err - } - // If at this point, the scripts doesn't resemble a witness program, // then we'll exit early as there isn't a valid version or program to // extract. - if !isWitnessProgram(pops) { + version, program, valid := extractWitnessProgramInfo(script) + if !valid { return 0, nil, fmt.Errorf("script is not a witness program, " + "unable to extract version or witness program") } - witnessVersion := asSmallInt(pops[0].opcode.value) - witnessProgram := pops[1].data - - return witnessVersion, witnessProgram, nil + return version, program, nil } // IsPushOnlyScript returns whether or not the passed script only pushes data diff --git a/txscript/standard.go b/txscript/standard.go index e3cf7707..44902971 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -388,7 +388,7 @@ func isWitnessScriptHashScript(script []byte) bool { } // extractWitnessProgramInfo returns the version and program if the passed -// script constitutes a valid witness program. The alst return value indicates +// script constitutes a valid witness program. The last return value indicates // whether or not the script is a valid witness program. func extractWitnessProgramInfo(script []byte) (int, []byte, bool) { // Skip parsing if we know the program is invalid based on size.