Skip to content

Commit

Permalink
txscript: Make typeOfScript accept raw script.
Browse files Browse the repository at this point in the history
This converts the typeOfScript function to accept a script version and
raw script instead of an array of internal parsed opcodes in order to
make it more flexible for raw script analysis.

Also, this adds a comment to CalcScriptInfo to call out the specific
version semantics and deprecates the function since nothing currently
uses it, and the relevant information can now be obtained by callers
more directly through the use of the new script tokenizer.

All other callers are updated accordingly.
  • Loading branch information
davecgh authored and cfromknecht committed Feb 5, 2021
1 parent ca395fe commit e9d7686
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions txscript/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,19 @@ func isNullDataScript(scriptVersion uint16, script []byte) bool {

// scriptType returns the type of the script being inspected from the known
// standard types.
func typeOfScript(pops []parsedOpcode) ScriptClass {
//
// NOTE: All scripts that are not version 0 are currently considered non
// standard.
func typeOfScript(scriptVersion uint16, script []byte) ScriptClass {
if scriptVersion != 0 {
return NonStandardTy
}

pops, err := parseScript(script)
if err != nil {
return NonStandardTy
}

if isPubkey(pops) {
return PubKeyTy
} else if isPubkeyHash(pops) {
Expand All @@ -513,11 +525,8 @@ func typeOfScript(pops []parsedOpcode) ScriptClass {
//
// NonStandardTy will be returned when the script does not parse.
func GetScriptClass(script []byte) ScriptClass {
pops, err := parseScript(script)
if err != nil {
return NonStandardTy
}
return typeOfScript(pops)
const scriptVersion = 0
return typeOfScript(scriptVersion, script)
}

// NewScriptClass returns the ScriptClass corresponding to the string name
Expand Down Expand Up @@ -600,9 +609,17 @@ type ScriptInfo struct {
// pair. It will error if the pair is in someway invalid such that they can not
// be analysed, i.e. if they do not parse or the pkScript is not a push-only
// script
//
// NOTE: This function is only valid for version 0 scripts. Since the function
// does not accept a script version, the results are undefined for other script
// versions.
//
// DEPRECATED. This will be removed in the next major version bump.
func CalcScriptInfo(sigScript, pkScript []byte, witness wire.TxWitness,
bip16, segwit bool) (*ScriptInfo, error) {

const scriptVersion = 0

sigPops, err := parseScript(sigScript)
if err != nil {
return nil, err
Expand All @@ -613,9 +630,8 @@ func CalcScriptInfo(sigScript, pkScript []byte, witness wire.TxWitness,
return nil, err
}

// Push only sigScript makes little sense.
si := new(ScriptInfo)
si.PkScriptClass = typeOfScript(pkPops)
si.PkScriptClass = typeOfScript(scriptVersion, pkScript)

// Can't have a signature script that doesn't just push data.
if !isPushOnly(sigPops) {
Expand All @@ -636,7 +652,8 @@ func CalcScriptInfo(sigScript, pkScript []byte, witness wire.TxWitness,
return nil, err
}

shInputs := expectedInputs(shPops, typeOfScript(shPops))
redeemClass := typeOfScript(scriptVersion, script)
shInputs := expectedInputs(shPops, redeemClass)
if shInputs == -1 {
si.ExpectedInputs = -1
} else {
Expand All @@ -663,7 +680,9 @@ func CalcScriptInfo(sigScript, pkScript []byte, witness wire.TxWitness,
// Extract the pushed witness program from the sigScript so we
// can determine the number of expected inputs.
pkPops, _ := parseScript(sigScript[1:])
shInputs := expectedInputs(pkPops, typeOfScript(pkPops))

redeemClass := typeOfScript(scriptVersion, sigScript[1:])
shInputs := expectedInputs(pkPops, redeemClass)
if shInputs == -1 {
si.ExpectedInputs = -1
} else {
Expand All @@ -683,7 +702,8 @@ func CalcScriptInfo(sigScript, pkScript []byte, witness wire.TxWitness,
witnessScript := witness[len(witness)-1]
pops, _ := parseScript(witnessScript)

shInputs := expectedInputs(pops, typeOfScript(pops))
redeemClass := typeOfScript(scriptVersion, witnessScript)
shInputs := expectedInputs(pops, redeemClass)
if shInputs == -1 {
si.ExpectedInputs = -1
} else {
Expand Down Expand Up @@ -880,7 +900,9 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
return NonStandardTy, nil, 0, err
}

scriptClass := typeOfScript(pops)
const scriptVersion = 0
scriptClass := typeOfScript(scriptVersion, pkScript)

switch scriptClass {
case PubKeyHashTy:
// A pay-to-pubkey-hash script is of the form:
Expand Down

0 comments on commit e9d7686

Please sign in to comment.