From 8c68575331fcabdc6a00f907660294d4bf0ca9ac Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:11:11 -0500 Subject: [PATCH] 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. --- txscript/script.go | 10 +++++----- txscript/standard.go | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/txscript/script.go b/txscript/script.go index 58b8e69f..3dfd82e1 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -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 } diff --git a/txscript/standard.go b/txscript/standard.go index a447303d..94b3cce5 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -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 }