From 44cbc3176c63fdfe7621d200c234fb1ed64a8703 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:11:09 -0500 Subject: [PATCH] txscript: Make isSmallInt accept raw opcode. This converts the isSmallInt 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. The comment is modified to explicitly call out the script version semantics. Finally, it updates all callers accordingly. --- txscript/script.go | 13 +++++++------ txscript/standard.go | 10 +++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/txscript/script.go b/txscript/script.go index e2a82233..97e6adec 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -1,5 +1,5 @@ // Copyright (c) 2013-2017 The btcsuite developers -// Copyright (c) 2015-2018 The Decred developers +// Copyright (c) 2015-2019 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -20,11 +20,12 @@ const ( // isSmallInt returns whether or not the opcode is considered a small integer, // which is an OP_0, or OP_1 through OP_16. -func isSmallInt(op *opcode) bool { - if op.value == OP_0 || (op.value >= OP_1 && op.value <= OP_16) { - return true - } - return false +// +// NOTE: This function is only valid for version 0 opcodes. Since the function +// does not accept a script version, the results are undefined for other script +// versions. +func isSmallInt(op byte) bool { + return op == OP_0 || (op >= OP_1 && op <= OP_16) } // IsPayToScriptHash returns true if the script is in the standard diff --git a/txscript/standard.go b/txscript/standard.go index 06aa52a3..c2617518 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -147,10 +147,10 @@ func isMultiSig(pops []parsedOpcode) bool { if l < 4 { return false } - if !isSmallInt(pops[0].opcode) { + if !isSmallInt(pops[0].opcode.value) { return false } - if !isSmallInt(pops[l-2].opcode) { + if !isSmallInt(pops[l-2].opcode.value) { return false } if pops[l-1].opcode.value != OP_CHECKMULTISIG { @@ -214,7 +214,7 @@ func isNullData(pops []parsedOpcode) bool { return l == 2 && pops[0].opcode.value == OP_RETURN && - (isSmallInt(pops[1].opcode) || pops[1].opcode.value <= + (isSmallInt(pops[1].opcode.value) || pops[1].opcode.value <= OP_PUSHDATA4) && len(pops[1].data) <= MaxDataCarrierSize } @@ -1407,7 +1407,7 @@ func ExtractAtomicSwapDataPushes(version uint16, pkScript []byte) (*AtomicSwapDa return nil, nil } pushes.SecretSize = int64(locktime) - } else if op := pops[2].opcode; isSmallInt(op) { + } else if op := pops[2].opcode; isSmallInt(op.value) { pushes.SecretSize = int64(asSmallInt(op)) } else { return nil, nil @@ -1418,7 +1418,7 @@ func ExtractAtomicSwapDataPushes(version uint16, pkScript []byte) (*AtomicSwapDa return nil, nil } pushes.LockTime = int64(locktime) - } else if op := pops[11].opcode; isSmallInt(op) { + } else if op := pops[11].opcode; isSmallInt(op.value) { pushes.LockTime = int64(asSmallInt(op)) } else { return nil, nil