2017-01-07 18:31:03 +01:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
2019-03-13 07:11:09 +01:00
|
|
|
// Copyright (c) 2015-2019 The Decred developers
|
2013-06-12 23:35:27 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 07:03:01 +01:00
|
|
|
package txscript
|
2013-06-12 23:35:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-03-13 07:11:05 +01:00
|
|
|
"strings"
|
2014-07-03 02:37:49 +02:00
|
|
|
"time"
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2014-03-14 19:40:35 +01:00
|
|
|
)
|
2014-03-14 19:35:36 +01:00
|
|
|
|
2013-06-12 23:35:27 +02:00
|
|
|
// Bip16Activation is the timestamp where BIP0016 is valid to use in the
|
|
|
|
// blockchain. To be used to determine if BIP0016 should be called for or not.
|
|
|
|
// This timestamp corresponds to Sun Apr 1 00:00:00 UTC 2012.
|
|
|
|
var Bip16Activation = time.Unix(1333238400, 0)
|
|
|
|
|
|
|
|
const (
|
2022-01-07 02:15:57 +01:00
|
|
|
// TaprootAnnexTag is the tag for an annex. This value is used to
|
|
|
|
// identify the annex during tapscript spends. If there're at least two
|
|
|
|
// elements in the taproot witness stack, and the first byte of the
|
|
|
|
// last element matches this tag, then we'll extract this as a distinct
|
|
|
|
// item.
|
|
|
|
TaprootAnnexTag = 0x50
|
|
|
|
|
|
|
|
// TaprootLeafMask is the mask applied to the control block to extract
|
2022-01-07 02:39:55 +01:00
|
|
|
// the leaf version and parity of the y-coordinate of the output key if
|
|
|
|
// the taproot script leaf being spent.
|
2022-01-07 02:15:57 +01:00
|
|
|
TaprootLeafMask = 0xfe
|
2013-06-12 23:35:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// These are the constants specified for maximums in individual scripts.
|
|
|
|
const (
|
|
|
|
MaxOpsPerScript = 201 // Max number of non-push operations.
|
|
|
|
MaxPubKeysPerMultiSig = 20 // Multisig can't have more sigs than this.
|
|
|
|
MaxScriptElementSize = 520 // Max bytes pushable to the stack.
|
|
|
|
)
|
|
|
|
|
2023-03-03 20:08:04 +01:00
|
|
|
// IsSmallInt returns whether or not the opcode is considered a small integer,
|
2014-02-19 23:17:02 +01:00
|
|
|
// which is an OP_0, or OP_1 through OP_16.
|
2019-03-13 07:11:09 +01:00
|
|
|
//
|
|
|
|
// 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.
|
2023-03-03 20:08:04 +01:00
|
|
|
func IsSmallInt(op byte) bool {
|
2019-03-13 07:11:09 +01:00
|
|
|
return op == OP_0 || (op >= OP_1 && op <= OP_16)
|
2014-02-19 23:17:02 +01:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:38 +01:00
|
|
|
// IsPayToPubKey returns true if the script is in the standard pay-to-pubkey
|
|
|
|
// (P2PK) format, false otherwise.
|
|
|
|
func IsPayToPubKey(script []byte) bool {
|
2021-02-04 23:06:56 +01:00
|
|
|
return isPubKeyScript(script)
|
2019-03-13 07:11:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 23:52:11 +01:00
|
|
|
// IsPayToPubKeyHash returns true if the script is in the standard
|
|
|
|
// pay-to-pubkey-hash (P2PKH) format, false otherwise.
|
|
|
|
func IsPayToPubKeyHash(script []byte) bool {
|
2021-02-04 23:09:28 +01:00
|
|
|
return isPubKeyHashScript(script)
|
2021-02-04 23:52:11 +01:00
|
|
|
}
|
|
|
|
|
2013-06-27 01:32:11 +02:00
|
|
|
// IsPayToScriptHash returns true if the script is in the standard
|
2015-04-29 20:08:56 +02:00
|
|
|
// pay-to-script-hash (P2SH) format, false otherwise.
|
2019-03-13 07:11:14 +01:00
|
|
|
//
|
|
|
|
// WARNING: This function always treats the passed script as version 0. Great
|
|
|
|
// care must be taken if introducing a new script version because it is used in
|
|
|
|
// consensus which, unfortunately as of the time of this writing, does not check
|
|
|
|
// script versions before determining if the script is a P2SH which means nodes
|
|
|
|
// on existing rules will analyze new version scripts as if they were version 0.
|
2013-06-27 01:32:11 +02:00
|
|
|
func IsPayToScriptHash(script []byte) bool {
|
2019-03-13 07:11:14 +01:00
|
|
|
return isScriptHashScript(script)
|
2013-06-27 01:32:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-04 22:24:28 +01:00
|
|
|
// IsPayToWitnessScriptHash returns true if the script is in the standard
|
2016-10-19 02:45:40 +02:00
|
|
|
// pay-to-witness-script-hash (P2WSH) format, false otherwise.
|
|
|
|
func IsPayToWitnessScriptHash(script []byte) bool {
|
2021-02-04 22:39:10 +01:00
|
|
|
return isWitnessScriptHashScript(script)
|
2016-10-19 02:45:40 +02:00
|
|
|
}
|
|
|
|
|
2023-03-04 22:24:28 +01:00
|
|
|
// IsPayToWitnessPubKeyHash returns true if the script is in the standard
|
2016-10-19 02:45:40 +02:00
|
|
|
// pay-to-witness-pubkey-hash (P2WKH) format, false otherwise.
|
|
|
|
func IsPayToWitnessPubKeyHash(script []byte) bool {
|
2021-02-04 22:22:40 +01:00
|
|
|
return isWitnessPubKeyHashScript(script)
|
2016-10-19 02:45:40 +02:00
|
|
|
}
|
|
|
|
|
2023-03-04 22:24:28 +01:00
|
|
|
// IsPayToTaproot returns true if the passed script is a standard
|
2022-01-07 02:22:49 +01:00
|
|
|
// pay-to-taproot (PTTR) scripts, and false otherwise.
|
|
|
|
func IsPayToTaproot(script []byte) bool {
|
|
|
|
return isWitnessTaprootScript(script)
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:45:40 +02:00
|
|
|
// IsWitnessProgram returns true if the passed script is a valid witness
|
|
|
|
// program which is encoded according to the passed witness program version. A
|
|
|
|
// witness program must be a small integer (from 0-16), followed by 2-40 bytes
|
|
|
|
// of pushed data.
|
|
|
|
func IsWitnessProgram(script []byte) bool {
|
2019-04-20 03:58:28 +02:00
|
|
|
return isWitnessProgramScript(script)
|
2016-10-19 02:45:40 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:49 +01:00
|
|
|
// IsNullData returns true if the passed script is a null data script, false
|
|
|
|
// otherwise.
|
|
|
|
func IsNullData(script []byte) bool {
|
2021-02-05 00:15:34 +01:00
|
|
|
const scriptVersion = 0
|
|
|
|
return isNullDataScript(scriptVersion, script)
|
2019-03-13 07:11:49 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 02:45:40 +02:00
|
|
|
// ExtractWitnessProgramInfo attempts to extract the witness program version,
|
|
|
|
// as well as the witness program itself from the passed script.
|
|
|
|
func ExtractWitnessProgramInfo(script []byte) (int, []byte, error) {
|
|
|
|
// If at this point, the scripts doesn't resemble a witness program,
|
|
|
|
// then we'll exit early as there isn't a valid version or program to
|
|
|
|
// extract.
|
2019-04-20 04:43:43 +02:00
|
|
|
version, program, valid := extractWitnessProgramInfo(script)
|
|
|
|
if !valid {
|
2016-10-19 02:45:40 +02:00
|
|
|
return 0, nil, fmt.Errorf("script is not a witness program, " +
|
|
|
|
"unable to extract version or witness program")
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:43:43 +02:00
|
|
|
return version, program, nil
|
2016-10-19 02:45:40 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:26 +01:00
|
|
|
// IsPushOnlyScript returns whether or not the passed script only pushes data
|
|
|
|
// according to the consensus definition of pushing data.
|
2015-04-29 20:08:56 +02:00
|
|
|
//
|
2019-03-13 07:11:26 +01:00
|
|
|
// WARNING: This function always treats the passed script as version 0. Great
|
|
|
|
// care must be taken if introducing a new script version because it is used in
|
|
|
|
// consensus which, unfortunately as of the time of this writing, does not check
|
|
|
|
// script versions before checking if it is a push only script which means nodes
|
|
|
|
// on existing rules will treat new version scripts as if they were version 0.
|
2013-09-27 03:34:25 +02:00
|
|
|
func IsPushOnlyScript(script []byte) bool {
|
2019-03-13 07:11:26 +01:00
|
|
|
const scriptVersion = 0
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
// All opcodes up to OP_16 are data push instructions.
|
|
|
|
// NOTE: This does consider OP_RESERVED to be a data push instruction,
|
|
|
|
// but execution of OP_RESERVED will fail anyway and matches the
|
|
|
|
// behavior required by consensus.
|
|
|
|
if tokenizer.Opcode() > OP_16 {
|
|
|
|
return false
|
|
|
|
}
|
2013-09-27 03:34:25 +02:00
|
|
|
}
|
2019-03-13 07:11:26 +01:00
|
|
|
return tokenizer.Err() == nil
|
2013-09-27 03:34:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 20:08:56 +02:00
|
|
|
// DisasmString formats a disassembled script for one line printing. When the
|
|
|
|
// script fails to parse, the returned string will contain the disassembled
|
|
|
|
// script up to the point the failure occurred along with the string '[error]'
|
|
|
|
// appended. In addition, the reason the script failed to parse is returned
|
|
|
|
// if the caller wants more information about the failure.
|
2019-03-13 07:11:05 +01:00
|
|
|
//
|
|
|
|
// NOTE: This function is only valid for version 0 scripts. Since the function
|
|
|
|
// does not accept a script version, the results are undefined for other script
|
|
|
|
// versions.
|
|
|
|
func DisasmString(script []byte) (string, error) {
|
|
|
|
const scriptVersion = 0
|
|
|
|
|
|
|
|
var disbuf strings.Builder
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
if tokenizer.Next() {
|
|
|
|
disasmOpcode(&disbuf, tokenizer.op, tokenizer.Data(), true)
|
2015-04-29 20:08:56 +02:00
|
|
|
}
|
2019-03-13 07:11:05 +01:00
|
|
|
for tokenizer.Next() {
|
|
|
|
disbuf.WriteByte(' ')
|
|
|
|
disasmOpcode(&disbuf, tokenizer.op, tokenizer.Data(), true)
|
2015-04-29 20:08:56 +02:00
|
|
|
}
|
2019-03-13 07:11:05 +01:00
|
|
|
if tokenizer.Err() != nil {
|
|
|
|
if tokenizer.ByteIndex() != 0 {
|
|
|
|
disbuf.WriteByte(' ')
|
|
|
|
}
|
2015-09-28 19:56:25 +02:00
|
|
|
disbuf.WriteString("[error]")
|
2015-04-29 20:08:56 +02:00
|
|
|
}
|
2019-03-13 07:11:05 +01:00
|
|
|
return disbuf.String(), tokenizer.Err()
|
2015-04-29 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 01:26:22 +02:00
|
|
|
// removeOpcodeRaw will return the script after removing any opcodes that match
|
|
|
|
// `opcode`. If the opcode does not appear in script, the original script will
|
|
|
|
// be returned unmodified. Otherwise, a new script will be allocated to contain
|
|
|
|
// the filtered script. This metehod assumes that the script parses
|
|
|
|
// successfully.
|
|
|
|
//
|
|
|
|
// NOTE: This function is only valid for version 0 scripts. Since the function
|
|
|
|
// does not accept a script version, the results are undefined for other script
|
|
|
|
// versions.
|
|
|
|
func removeOpcodeRaw(script []byte, opcode byte) []byte {
|
|
|
|
// Avoid work when possible.
|
|
|
|
if len(script) == 0 {
|
|
|
|
return script
|
|
|
|
}
|
|
|
|
|
|
|
|
const scriptVersion = 0
|
|
|
|
var result []byte
|
|
|
|
var prevOffset int32
|
|
|
|
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
if tokenizer.Opcode() == opcode {
|
|
|
|
if result == nil {
|
|
|
|
result = make([]byte, 0, len(script))
|
|
|
|
result = append(result, script[:prevOffset]...)
|
|
|
|
}
|
|
|
|
} else if result != nil {
|
|
|
|
result = append(result, script[prevOffset:tokenizer.ByteIndex()]...)
|
|
|
|
}
|
|
|
|
prevOffset = tokenizer.ByteIndex()
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
return script
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:22 +01:00
|
|
|
// isCanonicalPush returns true if the opcode is either not a push instruction
|
|
|
|
// or the data associated with the push instruction uses the smallest
|
|
|
|
// instruction to do the job. False otherwise.
|
|
|
|
//
|
|
|
|
// For example, it is possible to push a value of 1 to the stack as "OP_1",
|
|
|
|
// "OP_DATA_1 0x01", "OP_PUSHDATA1 0x01 0x01", and others, however, the first
|
|
|
|
// only takes a single byte, while the rest take more. Only the first is
|
|
|
|
// considered canonical.
|
|
|
|
func isCanonicalPush(opcode byte, data []byte) bool {
|
|
|
|
dataLen := len(data)
|
2015-04-29 20:08:56 +02:00
|
|
|
if opcode > OP_16 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if opcode < OP_PUSHDATA1 && opcode > OP_0 && (dataLen == 1 && data[0] <= 16) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if opcode == OP_PUSHDATA1 && dataLen < OP_PUSHDATA1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if opcode == OP_PUSHDATA2 && dataLen <= 0xff {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if opcode == OP_PUSHDATA4 && dataLen <= 0xffff {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:13:00 +01:00
|
|
|
// removeOpcodeByData will return the script minus any opcodes that perform a
|
2019-03-13 07:12:51 +01:00
|
|
|
// canonical push of data that contains the passed data to remove. This
|
|
|
|
// function assumes it is provided a version 0 script as any future version of
|
|
|
|
// script should avoid this functionality since it is unncessary due to the
|
|
|
|
// signature scripts not being part of the witness-free transaction hash.
|
|
|
|
//
|
|
|
|
// WARNING: This will return the passed script unmodified unless a modification
|
|
|
|
// is necessary in which case the modified script is returned. This implies
|
|
|
|
// callers may NOT rely on being able to safely mutate either the passed or
|
|
|
|
// returned script without potentially modifying the same data.
|
|
|
|
//
|
|
|
|
// NOTE: This function is only valid for version 0 scripts. Since the function
|
|
|
|
// does not accept a script version, the results are undefined for other script
|
|
|
|
// versions.
|
2019-03-13 07:13:00 +01:00
|
|
|
func removeOpcodeByData(script []byte, dataToRemove []byte) []byte {
|
2019-03-13 07:12:51 +01:00
|
|
|
// Avoid work when possible.
|
|
|
|
if len(script) == 0 || len(dataToRemove) == 0 {
|
|
|
|
return script
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse through the script looking for a canonical data push that contains
|
|
|
|
// the data to remove.
|
|
|
|
const scriptVersion = 0
|
|
|
|
var result []byte
|
|
|
|
var prevOffset int32
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
// In practice, the script will basically never actually contain the
|
|
|
|
// data since this function is only used during signature verification
|
|
|
|
// to remove the signature itself which would require some incredibly
|
|
|
|
// non-standard code to create.
|
|
|
|
//
|
|
|
|
// Thus, as an optimization, avoid allocating a new script unless there
|
|
|
|
// is actually a match that needs to be removed.
|
|
|
|
op, data := tokenizer.Opcode(), tokenizer.Data()
|
|
|
|
if isCanonicalPush(op, data) && bytes.Contains(data, dataToRemove) {
|
|
|
|
if result == nil {
|
|
|
|
fullPushLen := tokenizer.ByteIndex() - prevOffset
|
|
|
|
result = make([]byte, 0, int32(len(script))-fullPushLen)
|
|
|
|
result = append(result, script[0:prevOffset]...)
|
|
|
|
}
|
|
|
|
} else if result != nil {
|
|
|
|
result = append(result, script[prevOffset:tokenizer.ByteIndex()]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
prevOffset = tokenizer.ByteIndex()
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
result = script
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-03-03 20:08:04 +01:00
|
|
|
// AsSmallInt returns the passed opcode, which must be true according to
|
|
|
|
// IsSmallInt(), as an integer.
|
|
|
|
func AsSmallInt(op byte) int {
|
2019-03-13 07:11:11 +01:00
|
|
|
if op == OP_0 {
|
2013-07-25 15:27:58 +02:00
|
|
|
return 0
|
2013-06-20 19:07:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:11 +01:00
|
|
|
return int(op - (OP_1 - 1))
|
2013-06-20 19:07:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:20 +01:00
|
|
|
// countSigOpsV0 returns the number of signature operations in the provided
|
|
|
|
// script up to the point of the first parse failure or the entire script when
|
|
|
|
// there are no parse failures. The precise flag attempts to accurately count
|
|
|
|
// the number of operations for a multisig operation versus using the maximum
|
|
|
|
// allowed.
|
|
|
|
//
|
|
|
|
// WARNING: This function always treats the passed script as version 0. Great
|
|
|
|
// care must be taken if introducing a new script version because it is used in
|
|
|
|
// consensus which, unfortunately as of the time of this writing, does not check
|
|
|
|
// script versions before counting their signature operations which means nodes
|
|
|
|
// on existing rules will count new version scripts as if they were version 0.
|
|
|
|
func countSigOpsV0(script []byte, precise bool) int {
|
|
|
|
const scriptVersion = 0
|
|
|
|
|
|
|
|
numSigOps := 0
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
prevOp := byte(OP_INVALIDOPCODE)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
switch tokenizer.Opcode() {
|
|
|
|
case OP_CHECKSIG, OP_CHECKSIGVERIFY:
|
|
|
|
numSigOps++
|
|
|
|
|
|
|
|
case OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY:
|
|
|
|
// Note that OP_0 is treated as the max number of sigops here in
|
|
|
|
// precise mode despite it being a valid small integer in order to
|
|
|
|
// highly discourage multisigs with zero pubkeys.
|
|
|
|
//
|
|
|
|
// Also, even though this is referred to as "precise" counting, it's
|
|
|
|
// not really precise at all due to the small int opcodes only
|
|
|
|
// covering 1 through 16 pubkeys, which means this will count any
|
|
|
|
// more than that value (e.g. 17, 18 19) as the maximum number of
|
|
|
|
// allowed pubkeys. This is, unfortunately, now part of
|
2021-11-01 19:42:22 +01:00
|
|
|
// the Bitcoin consensus rules, due to historical
|
2019-03-13 07:11:20 +01:00
|
|
|
// reasons. This could be made more correct with a new
|
|
|
|
// script version, however, ideally all multisignaure
|
|
|
|
// operations in new script versions should move to
|
|
|
|
// aggregated schemes such as Schnorr instead.
|
|
|
|
if precise && prevOp >= OP_1 && prevOp <= OP_16 {
|
2023-03-03 20:08:04 +01:00
|
|
|
numSigOps += AsSmallInt(prevOp)
|
2019-03-13 07:11:20 +01:00
|
|
|
} else {
|
|
|
|
numSigOps += MaxPubKeysPerMultiSig
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Not a sigop.
|
|
|
|
}
|
|
|
|
|
|
|
|
prevOp = tokenizer.Opcode()
|
|
|
|
}
|
|
|
|
|
|
|
|
return numSigOps
|
|
|
|
}
|
|
|
|
|
2015-04-29 20:08:56 +02:00
|
|
|
// GetSigOpCount provides a quick count of the number of signature operations
|
|
|
|
// in a script. a CHECKSIG operations counts for 1, and a CHECK_MULTISIG for 20.
|
|
|
|
// If the script fails to parse, then the count up to the point of failure is
|
|
|
|
// returned.
|
2019-03-13 07:11:20 +01:00
|
|
|
//
|
|
|
|
// WARNING: This function always treats the passed script as version 0. Great
|
|
|
|
// care must be taken if introducing a new script version because it is used in
|
|
|
|
// consensus which, unfortunately as of the time of this writing, does not check
|
|
|
|
// script versions before counting their signature operations which means nodes
|
|
|
|
// on existing rules will count new version scripts as if they were version 0.
|
2015-04-29 20:08:56 +02:00
|
|
|
func GetSigOpCount(script []byte) int {
|
2019-03-13 07:11:20 +01:00
|
|
|
return countSigOpsV0(script, false)
|
2015-04-29 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:18 +01:00
|
|
|
// finalOpcodeData returns the data associated with the final opcode in the
|
|
|
|
// script. It will return nil if the script fails to parse.
|
|
|
|
func finalOpcodeData(scriptVersion uint16, script []byte) []byte {
|
|
|
|
// Avoid unnecessary work.
|
|
|
|
if len(script) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
data = tokenizer.Data()
|
|
|
|
}
|
|
|
|
if tokenizer.Err() != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2015-04-29 20:08:56 +02:00
|
|
|
// GetPreciseSigOpCount returns the number of signature operations in
|
|
|
|
// scriptPubKey. If bip16 is true then scriptSig may be searched for the
|
|
|
|
// Pay-To-Script-Hash script in order to find the precise number of signature
|
|
|
|
// operations in the transaction. If the script fails to parse, then the count
|
|
|
|
// up to the point of failure is returned.
|
2019-03-13 07:11:31 +01:00
|
|
|
//
|
|
|
|
// WARNING: This function always treats the passed script as version 0. Great
|
|
|
|
// care must be taken if introducing a new script version because it is used in
|
|
|
|
// consensus which, unfortunately as of the time of this writing, does not check
|
|
|
|
// script versions before counting their signature operations which means nodes
|
|
|
|
// on existing rules will count new version scripts as if they were version 0.
|
|
|
|
//
|
|
|
|
// The third parameter is DEPRECATED and is unused.
|
|
|
|
func GetPreciseSigOpCount(scriptSig, scriptPubKey []byte, _ bool) int {
|
|
|
|
const scriptVersion = 0
|
2015-04-29 20:08:56 +02:00
|
|
|
|
2019-03-13 07:11:31 +01:00
|
|
|
// Treat non P2SH transactions as normal. Note that signature operation
|
|
|
|
// counting includes all operations up to the first parse failure.
|
|
|
|
if !isScriptHashScript(scriptPubKey) {
|
|
|
|
return countSigOpsV0(scriptPubKey, true)
|
2015-04-29 20:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The signature script must only push data to the stack for P2SH to be
|
|
|
|
// a valid pair, so the signature operation count is 0 when that is not
|
|
|
|
// the case.
|
2019-03-13 07:11:31 +01:00
|
|
|
if len(scriptSig) == 0 || !IsPushOnlyScript(scriptSig) {
|
2015-04-29 20:08:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// The P2SH script is the last item the signature script pushes to the
|
|
|
|
// stack. When the script is empty, there are no signature operations.
|
2019-03-13 07:11:31 +01:00
|
|
|
//
|
|
|
|
// Notice that signature scripts that fail to fully parse count as 0
|
|
|
|
// signature operations unlike public key and redeem scripts.
|
|
|
|
redeemScript := finalOpcodeData(scriptVersion, scriptSig)
|
|
|
|
if len(redeemScript) == 0 {
|
2015-04-29 20:08:56 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:31 +01:00
|
|
|
// Return the more precise sigops count for the redeem script. Note that
|
|
|
|
// signature operation counting includes all operations up to the first
|
|
|
|
// parse failure.
|
|
|
|
return countSigOpsV0(redeemScript, true)
|
2015-04-29 20:08:56 +02:00
|
|
|
}
|
2015-08-01 16:43:06 +02:00
|
|
|
|
2016-10-19 02:47:46 +02:00
|
|
|
// GetWitnessSigOpCount returns the number of signature operations generated by
|
|
|
|
// spending the passed pkScript with the specified witness, or sigScript.
|
|
|
|
// Unlike GetPreciseSigOpCount, this function is able to accurately count the
|
|
|
|
// number of signature operations generated by spending witness programs, and
|
|
|
|
// nested p2sh witness programs. If the script fails to parse, then the count
|
|
|
|
// up to the point of failure is returned.
|
|
|
|
func GetWitnessSigOpCount(sigScript, pkScript []byte, witness wire.TxWitness) int {
|
|
|
|
// If this is a regular witness program, then we can proceed directly
|
|
|
|
// to counting its signature operations without any further processing.
|
2021-02-05 10:58:59 +01:00
|
|
|
if isWitnessProgramScript(pkScript) {
|
2016-10-19 02:47:46 +02:00
|
|
|
return getWitnessSigOps(pkScript, witness)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll check the sigScript to see if this is a nested p2sh
|
|
|
|
// witness program. This is a case wherein the sigScript is actually a
|
|
|
|
// datapush of a p2wsh witness program.
|
2021-02-05 10:58:59 +01:00
|
|
|
if isScriptHashScript(pkScript) && IsPushOnlyScript(sigScript) &&
|
|
|
|
len(sigScript) > 0 && isWitnessProgramScript(sigScript[1:]) {
|
2016-10-19 02:47:46 +02:00
|
|
|
return getWitnessSigOps(sigScript[1:], witness)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// getWitnessSigOps returns the number of signature operations generated by
|
|
|
|
// spending the passed witness program wit the passed witness. The exact
|
|
|
|
// signature counting heuristic is modified by the version of the passed
|
|
|
|
// witness program. If the version of the witness program is unable to be
|
|
|
|
// extracted, then 0 is returned for the sig op count.
|
|
|
|
func getWitnessSigOps(pkScript []byte, witness wire.TxWitness) int {
|
|
|
|
// Attempt to extract the witness program version.
|
|
|
|
witnessVersion, witnessProgram, err := ExtractWitnessProgramInfo(
|
|
|
|
pkScript,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
switch witnessVersion {
|
2022-01-07 03:27:00 +01:00
|
|
|
case BaseSegwitWitnessVersion:
|
2016-10-19 02:47:46 +02:00
|
|
|
switch {
|
|
|
|
case len(witnessProgram) == payToWitnessPubKeyHashDataSize:
|
|
|
|
return 1
|
|
|
|
case len(witnessProgram) == payToWitnessScriptHashDataSize &&
|
|
|
|
len(witness) > 0:
|
|
|
|
|
|
|
|
witnessScript := witness[len(witness)-1]
|
2021-02-04 23:14:53 +01:00
|
|
|
return countSigOpsV0(witnessScript, true)
|
2016-10-19 02:47:46 +02:00
|
|
|
}
|
2022-01-07 03:27:00 +01:00
|
|
|
|
|
|
|
// Taproot signature operations don't count towards the block-wide sig
|
|
|
|
// op limit, instead a distinct weight-based accounting method is used.
|
|
|
|
case TaprootWitnessVersion:
|
2021-11-01 19:42:22 +01:00
|
|
|
return 0
|
2016-10-19 02:47:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:07 +01:00
|
|
|
// checkScriptParses returns an error if the provided script fails to parse.
|
|
|
|
func checkScriptParses(scriptVersion uint16, script []byte) error {
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
return tokenizer.Err()
|
|
|
|
}
|
|
|
|
|
2015-08-01 16:43:06 +02:00
|
|
|
// IsUnspendable returns whether the passed public key script is unspendable, or
|
2023-02-13 09:33:00 +01:00
|
|
|
// guaranteed to fail at execution. This allows outputs to be pruned instantly
|
2015-08-01 16:43:06 +02:00
|
|
|
// when entering the UTXO set.
|
2019-03-13 07:12:21 +01:00
|
|
|
//
|
|
|
|
// NOTE: This function is only valid for version 0 scripts. Since the function
|
|
|
|
// does not accept a script version, the results are undefined for other script
|
|
|
|
// versions.
|
2015-08-01 16:43:06 +02:00
|
|
|
func IsUnspendable(pkScript []byte) bool {
|
2019-03-13 07:12:21 +01:00
|
|
|
// The script is unspendable if starts with OP_RETURN or is guaranteed
|
|
|
|
// to fail at execution due to being larger than the max allowed script
|
|
|
|
// size.
|
|
|
|
switch {
|
|
|
|
case len(pkScript) > 0 && pkScript[0] == OP_RETURN:
|
|
|
|
return true
|
|
|
|
case len(pkScript) > MaxScriptSize:
|
2015-08-01 16:43:06 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:21 +01:00
|
|
|
// The script is unspendable if it is guaranteed to fail at execution.
|
|
|
|
const scriptVersion = 0
|
|
|
|
return checkScriptParses(scriptVersion, pkScript) != nil
|
2015-08-01 16:43:06 +02:00
|
|
|
}
|
2022-01-07 02:32:35 +01:00
|
|
|
|
|
|
|
// ScriptHasOpSuccess returns true if any op codes in the script contain an
|
|
|
|
// OP_SUCCESS op code.
|
|
|
|
func ScriptHasOpSuccess(witnessScript []byte) bool {
|
|
|
|
// First, create a new script tokenizer so we can run through all the
|
|
|
|
// elements.
|
|
|
|
tokenizer := MakeScriptTokenizer(0, witnessScript)
|
|
|
|
|
|
|
|
// Run through all the op codes, returning true if we find anything
|
|
|
|
// that is marked as a new op success.
|
|
|
|
for tokenizer.Next() {
|
|
|
|
if _, ok := successOpcodes[tokenizer.Opcode()]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|