Skip to content

Commit

Permalink
txscript: Make asSmallInt accept raw opcode.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davecgh authored and cfromknecht committed Feb 5, 2021
1 parent b4abc15 commit cd6f1f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions txscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions txscript/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit cd6f1f9

Please sign in to comment.