add GetScriptClass to return the internal script type of a script.

To be used to tell which class a script is (multisig, scripthash, pubkey,
pkhash, or nonstandard)
This commit is contained in:
Owain G. Ainsworth 2013-07-05 00:54:55 +01:00
parent 4d31b2c850
commit bedaddb790
4 changed files with 79 additions and 86 deletions

View file

@ -125,16 +125,16 @@ const (
MaxScriptElementSize = 520 // Max bytes pushable to the stack.
)
// ScriptType is an enumeration for the list of standard types of script.
type scriptType byte
// ScriptClass is an enumeration for the list of standard types of script.
type ScriptClass byte
// Types of script payment known about in the blockchain.
// Classes of script payment known about in the blockchain.
const (
pubKeyTy scriptType = iota // Pay pubkey.
pubKeyHashTy // Pay pubkey hash.
scriptHashTy // Pay to script hash.
multiSigTy // Multi signature.
nonStandardTy // None of the above.
PubKeyTy ScriptClass = iota // Pay pubkey.
PubKeyHashTy // Pay pubkey hash.
ScriptHashTy // Pay to script hash.
MultiSigTy // Multi signature.
NonStandardTy // None of the above.
)
// Script is the virtual machine that executes btcscripts.
@ -237,21 +237,31 @@ func isPushOnly(pops []parsedOpcode) bool {
return true
}
// GetScriptClass returns the class of the script passed. If the script does not
// parse then NonStandardTy will be returned.
func GetScriptClass(script []byte) ScriptClass {
pops, err := parseScript(script)
if err != nil {
return NonStandardTy
}
return typeOfScript(pops)
}
// scriptType returns the type of the script being inspected from the known
// standard types.
func typeOfScript(pops []parsedOpcode) scriptType {
func typeOfScript(pops []parsedOpcode) ScriptClass {
// XXX dubious optimisation: order these in order of popularity in the
// blockchain
if isPubkey(pops) {
return pubKeyTy
return PubKeyTy
} else if isPubkeyHash(pops) {
return pubKeyHashTy
return PubKeyHashTy
} else if isScriptHash(pops) {
return scriptHashTy
return ScriptHashTy
} else if isMultiSig(pops) {
return multiSigTy
return MultiSigTy
}
return nonStandardTy
return NonStandardTy
}