txscript: Make asSmallInt accept raw opcode.

This converts the asSmallInt function to accept an opcode as a byte
instead of the internal opcode data struct in order to make it more
flexible for raw script analysis.

It also updates all callers accordingly.
This commit is contained in:
Dave Collins 2019-03-13 01:11:11 -05:00 committed by Olaoluwa Osuntokun
parent 583b74040d
commit dfb1a6797b
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306
2 changed files with 13 additions and 13 deletions

View file

@ -159,7 +159,7 @@ func ExtractWitnessProgramInfo(script []byte) (int, []byte, error) {
"unable to extract version or witness program")
}
witnessVersion := asSmallInt(pops[0].opcode)
witnessVersion := asSmallInt(pops[0].opcode.value)
witnessProgram := pops[1].data
return witnessVersion, witnessProgram, nil
@ -700,12 +700,12 @@ func calcSignatureHash(prevOutScript []parsedOpcode, hashType SigHashType,
// asSmallInt returns the passed opcode, which must be true according to
// isSmallInt(), as an integer.
func asSmallInt(op *opcode) int {
if op.value == OP_0 {
func asSmallInt(op byte) int {
if op == OP_0 {
return 0
}
return int(op.value - (OP_1 - 1))
return int(op - (OP_1 - 1))
}
// getSigOpCount is the implementation function for counting the number of
@ -730,7 +730,7 @@ func getSigOpCount(pops []parsedOpcode, precise bool) int {
if precise && i > 0 &&
pops[i-1].opcode.value >= OP_1 &&
pops[i-1].opcode.value <= OP_16 {
nSigs += asSmallInt(pops[i-1].opcode)
nSigs += asSmallInt(pops[i-1].opcode.value)
} else {
nSigs += MaxPubKeysPerMultiSig
}

View file

@ -127,7 +127,7 @@ func isMultiSig(pops []parsedOpcode) bool {
// Verify the number of pubkeys specified matches the actual number
// of pubkeys provided.
if l-2-1 != asSmallInt(pops[l-2].opcode) {
if l-2-1 != asSmallInt(pops[l-2].opcode.value) {
return false
}
@ -238,7 +238,7 @@ func expectedInputs(pops []parsedOpcode, class ScriptClass) int {
// the original bitcoind bug where OP_CHECKMULTISIG pops an
// additional item from the stack, add an extra expected input
// for the extra push that is required to compensate.
return asSmallInt(pops[0].opcode) + 1
return asSmallInt(pops[0].opcode.value) + 1
case NullDataTy:
fallthrough
@ -395,8 +395,8 @@ func CalcMultiSigStats(script []byte) (int, int, error) {
return 0, 0, scriptError(ErrNotMultisigScript, str)
}
numSigs := asSmallInt(pops[0].opcode)
numPubKeys := asSmallInt(pops[len(pops)-2].opcode)
numSigs := asSmallInt(pops[0].opcode.value)
numPubKeys := asSmallInt(pops[len(pops)-2].opcode.value)
return numPubKeys, numSigs, nil
}
@ -617,8 +617,8 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
// Therefore the number of required signatures is the 1st item
// on the stack and the number of public keys is the 2nd to last
// item on the stack.
requiredSigs = asSmallInt(pops[0].opcode)
numPubKeys := asSmallInt(pops[len(pops)-2].opcode)
requiredSigs = asSmallInt(pops[0].opcode.value)
numPubKeys := asSmallInt(pops[len(pops)-2].opcode.value)
// Extract the public keys while skipping any that are invalid.
addrs = make([]btcutil.Address, 0, numPubKeys)
@ -706,7 +706,7 @@ func ExtractAtomicSwapDataPushes(version uint16, pkScript []byte) (*AtomicSwapDa
}
pushes.SecretSize = int64(locktime)
} else if op := pops[2].opcode; isSmallInt(op.value) {
pushes.SecretSize = int64(asSmallInt(op))
pushes.SecretSize = int64(asSmallInt(op.value))
} else {
return nil, nil
}
@ -717,7 +717,7 @@ func ExtractAtomicSwapDataPushes(version uint16, pkScript []byte) (*AtomicSwapDa
}
pushes.LockTime = int64(locktime)
} else if op := pops[11].opcode; isSmallInt(op.value) {
pushes.LockTime = int64(asSmallInt(op))
pushes.LockTime = int64(asSmallInt(op.value))
} else {
return nil, nil
}