2020-09-15 23:30:59 +02:00
|
|
|
// Copyright (c) 2013-2020 The btcsuite developers
|
2015-05-01 22:20:07 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package txscript
|
|
|
|
|
|
|
|
import (
|
2017-01-07 18:31:03 +01:00
|
|
|
"fmt"
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2016-10-19 02:45:40 +02:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-05-01 22:20:07 +02:00
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-07-20 08:26:05 +02:00
|
|
|
// MaxDataCarrierSize is the maximum number of bytes allowed in pushed
|
2015-05-01 22:20:07 +02:00
|
|
|
// data to be considered a nulldata transaction
|
2015-07-20 08:26:05 +02:00
|
|
|
MaxDataCarrierSize = 80
|
2015-05-01 22:20:07 +02:00
|
|
|
|
|
|
|
// StandardVerifyFlags are the script flags which are used when
|
|
|
|
// executing transaction scripts to enforce additional checks which
|
|
|
|
// are required for the script to be considered standard. These checks
|
|
|
|
// help reduce issues related to transaction malleability as well as
|
|
|
|
// allow pay-to-script hash transactions. Note these flags are
|
|
|
|
// different than what is required for the consensus rules in that they
|
|
|
|
// are more strict.
|
|
|
|
//
|
|
|
|
// TODO: This definition does not belong here. It belongs in a policy
|
|
|
|
// package.
|
|
|
|
StandardVerifyFlags = ScriptBip16 |
|
|
|
|
ScriptVerifyDERSignatures |
|
|
|
|
ScriptVerifyStrictEncoding |
|
|
|
|
ScriptVerifyMinimalData |
|
|
|
|
ScriptStrictMultiSig |
|
|
|
|
ScriptDiscourageUpgradableNops |
|
2015-10-09 21:30:12 +02:00
|
|
|
ScriptVerifyCleanStack |
|
2016-11-18 04:08:06 +01:00
|
|
|
ScriptVerifyNullFail |
|
2015-10-05 18:11:56 +02:00
|
|
|
ScriptVerifyCheckLockTimeVerify |
|
2015-10-23 23:05:24 +02:00
|
|
|
ScriptVerifyCheckSequenceVerify |
|
2016-10-19 03:02:00 +02:00
|
|
|
ScriptVerifyLowS |
|
2016-10-19 03:03:51 +02:00
|
|
|
ScriptStrictMultiSig |
|
2016-10-19 03:02:00 +02:00
|
|
|
ScriptVerifyWitness |
|
2017-04-27 01:32:47 +02:00
|
|
|
ScriptVerifyDiscourageUpgradeableWitnessProgram |
|
|
|
|
ScriptVerifyMinimalIf |
|
|
|
|
ScriptVerifyWitnessPubKeyType
|
2015-05-01 22:20:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ScriptClass is an enumeration for the list of standard types of script.
|
|
|
|
type ScriptClass byte
|
|
|
|
|
|
|
|
// Classes of script payment known about in the blockchain.
|
|
|
|
const (
|
2016-10-19 02:45:40 +02:00
|
|
|
NonStandardTy ScriptClass = iota // None of the recognized forms.
|
|
|
|
PubKeyTy // Pay pubkey.
|
|
|
|
PubKeyHashTy // Pay pubkey hash.
|
|
|
|
WitnessV0PubKeyHashTy // Pay witness pubkey hash.
|
|
|
|
ScriptHashTy // Pay to script hash.
|
|
|
|
WitnessV0ScriptHashTy // Pay to witness script hash.
|
|
|
|
MultiSigTy // Multi signature.
|
|
|
|
NullDataTy // Empty data-only (provably prunable).
|
2020-09-15 23:30:59 +02:00
|
|
|
WitnessUnknownTy // Witness unknown
|
2015-05-01 22:20:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// scriptClassToName houses the human-readable strings which describe each
|
|
|
|
// script class.
|
|
|
|
var scriptClassToName = []string{
|
2016-10-19 02:45:40 +02:00
|
|
|
NonStandardTy: "nonstandard",
|
|
|
|
PubKeyTy: "pubkey",
|
|
|
|
PubKeyHashTy: "pubkeyhash",
|
|
|
|
WitnessV0PubKeyHashTy: "witness_v0_keyhash",
|
|
|
|
ScriptHashTy: "scripthash",
|
|
|
|
WitnessV0ScriptHashTy: "witness_v0_scripthash",
|
|
|
|
MultiSigTy: "multisig",
|
|
|
|
NullDataTy: "nulldata",
|
2020-09-15 23:30:59 +02:00
|
|
|
WitnessUnknownTy: "witness_unknown",
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the Stringer interface by returning the name of
|
|
|
|
// the enum script class. If the enum is invalid then "Invalid" will be
|
|
|
|
// returned.
|
|
|
|
func (t ScriptClass) String() string {
|
|
|
|
if int(t) > len(scriptClassToName) || int(t) < 0 {
|
|
|
|
return "Invalid"
|
|
|
|
}
|
|
|
|
return scriptClassToName[t]
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:06:56 +01:00
|
|
|
// extractCompressedPubKey extracts a compressed public key from the passed
|
|
|
|
// script if it is a standard pay-to-compressed-secp256k1-pubkey script. It
|
|
|
|
// will return nil otherwise.
|
|
|
|
func extractCompressedPubKey(script []byte) []byte {
|
|
|
|
// A pay-to-compressed-pubkey script is of the form:
|
|
|
|
// OP_DATA_33 <33-byte compressed pubkey> OP_CHECKSIG
|
|
|
|
|
|
|
|
// All compressed secp256k1 public keys must start with 0x02 or 0x03.
|
|
|
|
if len(script) == 35 &&
|
|
|
|
script[34] == OP_CHECKSIG &&
|
|
|
|
script[0] == OP_DATA_33 &&
|
|
|
|
(script[1] == 0x02 || script[1] == 0x03) {
|
|
|
|
|
|
|
|
return script[1:34]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractUncompressedPubKey extracts an uncompressed public key from the
|
|
|
|
// passed script if it is a standard pay-to-uncompressed-secp256k1-pubkey
|
|
|
|
// script. It will return nil otherwise.
|
|
|
|
func extractUncompressedPubKey(script []byte) []byte {
|
|
|
|
// A pay-to-uncompressed-pubkey script is of the form:
|
|
|
|
// OP_DATA_65 <65-byte uncompressed pubkey> OP_CHECKSIG
|
|
|
|
//
|
|
|
|
// All non-hybrid uncompressed secp256k1 public keys must start with 0x04.
|
|
|
|
// Hybrid uncompressed secp256k1 public keys start with 0x06 or 0x07:
|
|
|
|
// - 0x06 => hybrid format for even Y coords
|
|
|
|
// - 0x07 => hybrid format for odd Y coords
|
|
|
|
if len(script) == 67 &&
|
|
|
|
script[66] == OP_CHECKSIG &&
|
|
|
|
script[0] == OP_DATA_65 &&
|
|
|
|
(script[1] == 0x04 || script[1] == 0x06 || script[1] == 0x07) {
|
|
|
|
|
|
|
|
return script[1:66]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractPubKey extracts either compressed or uncompressed public key from the
|
|
|
|
// passed script if it is a either a standard pay-to-compressed-secp256k1-pubkey
|
|
|
|
// or pay-to-uncompressed-secp256k1-pubkey script, respectively. It will return
|
|
|
|
// nil otherwise.
|
|
|
|
func extractPubKey(script []byte) []byte {
|
|
|
|
if pubKey := extractCompressedPubKey(script); pubKey != nil {
|
|
|
|
return pubKey
|
|
|
|
}
|
|
|
|
return extractUncompressedPubKey(script)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isPubKeyScript returns whether or not the passed script is either a standard
|
|
|
|
// pay-to-compressed-secp256k1-pubkey or pay-to-uncompressed-secp256k1-pubkey
|
|
|
|
// script.
|
|
|
|
func isPubKeyScript(script []byte) bool {
|
|
|
|
return extractPubKey(script) != nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:09:28 +01:00
|
|
|
// extractPubKeyHash extracts the public key hash from the passed script if it
|
|
|
|
// is a standard pay-to-pubkey-hash script. It will return nil otherwise.
|
|
|
|
func extractPubKeyHash(script []byte) []byte {
|
|
|
|
// A pay-to-pubkey-hash script is of the form:
|
|
|
|
// OP_DUP OP_HASH160 <20-byte hash> OP_EQUALVERIFY OP_CHECKSIG
|
|
|
|
if len(script) == 25 &&
|
|
|
|
script[0] == OP_DUP &&
|
|
|
|
script[1] == OP_HASH160 &&
|
|
|
|
script[2] == OP_DATA_20 &&
|
|
|
|
script[23] == OP_EQUALVERIFY &&
|
|
|
|
script[24] == OP_CHECKSIG {
|
|
|
|
|
|
|
|
return script[3:23]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isPubKeyHashScript returns whether or not the passed script is a standard
|
|
|
|
// pay-to-pubkey-hash script.
|
|
|
|
func isPubKeyHashScript(script []byte) bool {
|
|
|
|
return extractPubKeyHash(script) != nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:14 +01:00
|
|
|
// extractScriptHash extracts the script hash from the passed script if it is a
|
|
|
|
// standard pay-to-script-hash script. It will return nil otherwise.
|
|
|
|
//
|
|
|
|
// 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 extractScriptHash(script []byte) []byte {
|
|
|
|
// A pay-to-script-hash script is of the form:
|
|
|
|
// OP_HASH160 <20-byte scripthash> OP_EQUAL
|
|
|
|
if len(script) == 23 &&
|
|
|
|
script[0] == OP_HASH160 &&
|
|
|
|
script[1] == OP_DATA_20 &&
|
|
|
|
script[22] == OP_EQUAL {
|
|
|
|
|
|
|
|
return script[2:22]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isScriptHashScript returns whether or not the passed script is a standard
|
|
|
|
// pay-to-script-hash script.
|
|
|
|
func isScriptHashScript(script []byte) bool {
|
|
|
|
return extractScriptHash(script) != nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:16 +01:00
|
|
|
// multiSigDetails houses details extracted from a standard multisig script.
|
|
|
|
type multiSigDetails struct {
|
|
|
|
requiredSigs int
|
|
|
|
numPubKeys int
|
|
|
|
pubKeys [][]byte
|
|
|
|
valid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractMultisigScriptDetails attempts to extract details from the passed
|
|
|
|
// script if it is a standard multisig script. The returned details struct will
|
|
|
|
// have the valid flag set to false otherwise.
|
|
|
|
//
|
|
|
|
// The extract pubkeys flag indicates whether or not the pubkeys themselves
|
|
|
|
// should also be extracted and is provided because extracting them results in
|
|
|
|
// an allocation that the caller might wish to avoid. The pubKeys member of
|
|
|
|
// the returned details struct will be nil when the flag is false.
|
|
|
|
//
|
|
|
|
// NOTE: This function is only valid for version 0 scripts. The returned
|
|
|
|
// details struct will always be empty and have the valid flag set to false for
|
|
|
|
// other script versions.
|
|
|
|
func extractMultisigScriptDetails(scriptVersion uint16, script []byte, extractPubKeys bool) multiSigDetails {
|
|
|
|
// The only currently supported script version is 0.
|
|
|
|
if scriptVersion != 0 {
|
|
|
|
return multiSigDetails{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A multi-signature script is of the form:
|
|
|
|
// NUM_SIGS PUBKEY PUBKEY PUBKEY ... NUM_PUBKEYS OP_CHECKMULTISIG
|
|
|
|
|
|
|
|
// The script can't possibly be a multisig script if it doesn't end with
|
|
|
|
// OP_CHECKMULTISIG or have at least two small integer pushes preceding it.
|
|
|
|
// Fail fast to avoid more work below.
|
|
|
|
if len(script) < 3 || script[len(script)-1] != OP_CHECKMULTISIG {
|
|
|
|
return multiSigDetails{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first opcode must be a small integer specifying the number of
|
|
|
|
// signatures required.
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
if !tokenizer.Next() || !isSmallInt(tokenizer.Opcode()) {
|
|
|
|
return multiSigDetails{}
|
|
|
|
}
|
|
|
|
requiredSigs := asSmallInt(tokenizer.Opcode())
|
|
|
|
|
|
|
|
// The next series of opcodes must either push public keys or be a small
|
|
|
|
// integer specifying the number of public keys.
|
|
|
|
var numPubKeys int
|
|
|
|
var pubKeys [][]byte
|
|
|
|
if extractPubKeys {
|
|
|
|
pubKeys = make([][]byte, 0, MaxPubKeysPerMultiSig)
|
|
|
|
}
|
|
|
|
for tokenizer.Next() {
|
|
|
|
if isSmallInt(tokenizer.Opcode()) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
data := tokenizer.Data()
|
|
|
|
numPubKeys++
|
|
|
|
if !isStrictPubKeyEncoding(data) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if extractPubKeys {
|
|
|
|
pubKeys = append(pubKeys, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tokenizer.Done() {
|
|
|
|
return multiSigDetails{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The next opcode must be a small integer specifying the number of public
|
|
|
|
// keys required.
|
|
|
|
op := tokenizer.Opcode()
|
|
|
|
if !isSmallInt(op) || asSmallInt(op) != numPubKeys {
|
|
|
|
return multiSigDetails{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There must only be a single opcode left unparsed which will be
|
|
|
|
// OP_CHECKMULTISIG per the check above.
|
|
|
|
if int32(len(tokenizer.Script()))-tokenizer.ByteIndex() != 1 {
|
|
|
|
return multiSigDetails{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multiSigDetails{
|
|
|
|
requiredSigs: requiredSigs,
|
|
|
|
numPubKeys: numPubKeys,
|
|
|
|
pubKeys: pubKeys,
|
|
|
|
valid: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isMultisigScript returns whether or not the passed script is a standard
|
|
|
|
// multisig script.
|
|
|
|
//
|
|
|
|
// NOTE: This function is only valid for version 0 scripts. It will always
|
|
|
|
// return false for other script versions.
|
|
|
|
func isMultisigScript(scriptVersion uint16, script []byte) bool {
|
|
|
|
// Since this is only checking the form of the script, don't extract the
|
|
|
|
// public keys to avoid the allocation.
|
|
|
|
details := extractMultisigScriptDetails(scriptVersion, script, false)
|
|
|
|
return details.valid
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:15 +01:00
|
|
|
// IsMultisigScript returns whether or not the passed script is a standard
|
|
|
|
// multisignature script.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The error is DEPRECATED and will be removed in the major version bump.
|
|
|
|
func IsMultisigScript(script []byte) (bool, error) {
|
2019-03-13 07:11:16 +01:00
|
|
|
const scriptVersion = 0
|
|
|
|
return isMultisigScript(scriptVersion, script), nil
|
2019-03-13 07:11:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:18 +01:00
|
|
|
// IsMultisigSigScript returns whether or not the passed script appears to be a
|
|
|
|
// signature script which consists of a pay-to-script-hash multi-signature
|
|
|
|
// redeem script. Determining if a signature script is actually a redemption of
|
|
|
|
// pay-to-script-hash requires the associated public key script which is often
|
|
|
|
// expensive to obtain. Therefore, this makes a fast best effort guess that has
|
|
|
|
// a high probability of being correct by checking if the signature script ends
|
|
|
|
// with a data push and treating that data push as if it were a p2sh redeem
|
|
|
|
// script
|
|
|
|
//
|
|
|
|
// 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:11:17 +01:00
|
|
|
func IsMultisigSigScript(script []byte) bool {
|
2019-03-13 07:11:18 +01:00
|
|
|
const scriptVersion = 0
|
|
|
|
|
|
|
|
// The script can't possibly be a multisig signature script if it doesn't
|
|
|
|
// end with OP_CHECKMULTISIG in the redeem script or have at least two small
|
|
|
|
// integers preceding it, and the redeem script itself must be preceded by
|
|
|
|
// at least a data push opcode. Fail fast to avoid more work below.
|
|
|
|
if len(script) < 4 || script[len(script)-1] != OP_CHECKMULTISIG {
|
2019-03-13 07:11:17 +01:00
|
|
|
return false
|
|
|
|
}
|
2019-03-13 07:11:18 +01:00
|
|
|
|
|
|
|
// Parse through the script to find the last opcode and any data it might
|
|
|
|
// push and treat it as a p2sh redeem script even though it might not
|
|
|
|
// actually be one.
|
|
|
|
possibleRedeemScript := finalOpcodeData(scriptVersion, script)
|
|
|
|
if possibleRedeemScript == nil {
|
2019-03-13 07:11:17 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:18 +01:00
|
|
|
// Finally, return if that possible redeem script is a multisig script.
|
|
|
|
return isMultisigScript(scriptVersion, possibleRedeemScript)
|
2019-03-13 07:11:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 22:22:40 +01:00
|
|
|
// extractWitnessPubKeyHash extracts the witness public key hash from the passed
|
|
|
|
// script if it is a standard pay-to-witness-pubkey-hash script. It will return
|
|
|
|
// nil otherwise.
|
|
|
|
func extractWitnessPubKeyHash(script []byte) []byte {
|
|
|
|
// A pay-to-witness-pubkey-hash script is of the form:
|
|
|
|
// OP_0 OP_DATA_20 <20-byte-hash>
|
|
|
|
if len(script) == 22 &&
|
|
|
|
script[0] == OP_0 &&
|
|
|
|
script[1] == OP_DATA_20 {
|
|
|
|
|
|
|
|
return script[2:22]
|
|
|
|
}
|
2019-04-19 10:46:52 +02:00
|
|
|
|
2021-02-04 22:22:40 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWitnessPubKeyHashScript returns whether or not the passed script is a
|
|
|
|
// standard pay-to-witness-pubkey-hash script.
|
|
|
|
func isWitnessPubKeyHashScript(script []byte) bool {
|
|
|
|
return extractWitnessPubKeyHash(script) != nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 22:39:10 +01:00
|
|
|
// extractWitnessScriptHash extracts the witness script hash from the passed
|
|
|
|
// script if it is standard pay-to-witness-script-hash script. It will return
|
|
|
|
// nil otherwise.
|
|
|
|
func extractWitnessScriptHash(script []byte) []byte {
|
|
|
|
// A pay-to-witness-script-hash script is of the form:
|
|
|
|
// OP_0 OP_DATA_32 <32-byte-hash>
|
|
|
|
if len(script) == 34 &&
|
|
|
|
script[0] == OP_0 &&
|
|
|
|
script[1] == OP_DATA_32 {
|
|
|
|
|
|
|
|
return script[2:34]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWitnessScriptHashScript returns whether or not the passed script is a
|
|
|
|
// standard pay-to-witness-script-hash script.
|
|
|
|
func isWitnessScriptHashScript(script []byte) bool {
|
|
|
|
return extractWitnessScriptHash(script) != nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:39:28 +02:00
|
|
|
// extractWitnessProgramInfo returns the version and program if the passed
|
2019-04-20 04:43:43 +02:00
|
|
|
// script constitutes a valid witness program. The last return value indicates
|
2019-04-20 04:39:28 +02:00
|
|
|
// whether or not the script is a valid witness program.
|
|
|
|
func extractWitnessProgramInfo(script []byte) (int, []byte, bool) {
|
2019-04-20 03:58:28 +02:00
|
|
|
// Skip parsing if we know the program is invalid based on size.
|
|
|
|
if len(script) < 4 || len(script) > 42 {
|
2019-04-20 04:39:28 +02:00
|
|
|
return 0, nil, false
|
2019-04-20 03:58:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const scriptVersion = 0
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
|
|
|
|
// The first opcode must be a small int.
|
|
|
|
if !tokenizer.Next() ||
|
|
|
|
!isSmallInt(tokenizer.Opcode()) {
|
|
|
|
|
2019-04-20 04:39:28 +02:00
|
|
|
return 0, nil, false
|
2019-04-20 03:58:28 +02:00
|
|
|
}
|
2019-04-20 04:39:28 +02:00
|
|
|
version := asSmallInt(tokenizer.Opcode())
|
2019-04-20 03:58:28 +02:00
|
|
|
|
|
|
|
// The second opcode must be a canonical data push, the length of the
|
|
|
|
// data push is bounded to 40 by the initial check on overall script
|
|
|
|
// length.
|
|
|
|
if !tokenizer.Next() ||
|
|
|
|
!isCanonicalPush(tokenizer.Opcode(), tokenizer.Data()) {
|
|
|
|
|
2019-04-20 04:39:28 +02:00
|
|
|
return 0, nil, false
|
2019-04-20 03:58:28 +02:00
|
|
|
}
|
2019-04-20 04:39:28 +02:00
|
|
|
program := tokenizer.Data()
|
2019-04-20 03:58:28 +02:00
|
|
|
|
|
|
|
// The witness program is valid if there are no more opcodes, and we
|
|
|
|
// terminated without a parsing error.
|
2019-04-20 04:39:28 +02:00
|
|
|
valid := tokenizer.Done() && tokenizer.Err() == nil
|
|
|
|
|
|
|
|
return version, program, valid
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWitnessProgramScript returns true if the passed script is a witness
|
|
|
|
// program, and false otherwise. A witness program MUST adhere to the following
|
|
|
|
// constraints: there must be exactly two pops (program version and the program
|
|
|
|
// itself), the first opcode MUST be a small integer (0-16), the push data MUST
|
|
|
|
// be canonical, and finally the size of the push data must be between 2 and 40
|
|
|
|
// bytes.
|
|
|
|
//
|
|
|
|
// The length of the script must be between 4 and 42 bytes. The
|
|
|
|
// smallest program is the witness version, followed by a data push of
|
|
|
|
// 2 bytes. The largest allowed witness program has a data push of
|
|
|
|
// 40-bytes.
|
|
|
|
//
|
|
|
|
// 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 isWitnessProgramScript(script []byte) bool {
|
|
|
|
_, _, valid := extractWitnessProgramInfo(script)
|
|
|
|
return valid
|
2019-04-20 03:58:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-05 00:15:34 +01:00
|
|
|
// isNullDataScript returns whether or not the passed script is a standard
|
|
|
|
// null data script.
|
|
|
|
//
|
|
|
|
// NOTE: This function is only valid for version 0 scripts. It will always
|
|
|
|
// return false for other script versions.
|
|
|
|
func isNullDataScript(scriptVersion uint16, script []byte) bool {
|
|
|
|
// The only currently supported script version is 0.
|
|
|
|
if scriptVersion != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// A null script is of the form:
|
|
|
|
// OP_RETURN <optional data>
|
|
|
|
//
|
|
|
|
// Thus, it can either be a single OP_RETURN or an OP_RETURN followed by a
|
|
|
|
// data push up to MaxDataCarrierSize bytes.
|
|
|
|
|
|
|
|
// The script can't possibly be a a null data script if it doesn't start
|
|
|
|
// with OP_RETURN. Fail fast to avoid more work below.
|
|
|
|
if len(script) < 1 || script[0] != OP_RETURN {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Single OP_RETURN.
|
|
|
|
if len(script) == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// OP_RETURN followed by data push up to MaxDataCarrierSize bytes.
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script[1:])
|
|
|
|
return tokenizer.Next() && tokenizer.Done() &&
|
|
|
|
(isSmallInt(tokenizer.Opcode()) || tokenizer.Opcode() <= OP_PUSHDATA4) &&
|
|
|
|
len(tokenizer.Data()) <= MaxDataCarrierSize
|
|
|
|
}
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
// scriptType returns the type of the script being inspected from the known
|
|
|
|
// standard types.
|
2019-03-13 07:11:33 +01:00
|
|
|
//
|
|
|
|
// NOTE: All scripts that are not version 0 are currently considered non
|
|
|
|
// standard.
|
|
|
|
func typeOfScript(scriptVersion uint16, script []byte) ScriptClass {
|
|
|
|
if scriptVersion != 0 {
|
|
|
|
return NonStandardTy
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:11:34 +01:00
|
|
|
switch {
|
2021-02-04 23:07:19 +01:00
|
|
|
case isPubKeyScript(script):
|
|
|
|
return PubKeyTy
|
2019-03-13 07:11:45 +01:00
|
|
|
case isPubKeyHashScript(script):
|
|
|
|
return PubKeyHashTy
|
2019-03-13 07:11:34 +01:00
|
|
|
case isScriptHashScript(script):
|
|
|
|
return ScriptHashTy
|
2019-04-19 10:46:52 +02:00
|
|
|
case isWitnessPubKeyHashScript(script):
|
|
|
|
return WitnessV0PubKeyHashTy
|
2019-04-19 11:04:01 +02:00
|
|
|
case isWitnessScriptHashScript(script):
|
|
|
|
return WitnessV0ScriptHashTy
|
2019-03-13 07:11:36 +01:00
|
|
|
case isMultisigScript(scriptVersion, script):
|
|
|
|
return MultiSigTy
|
2021-02-05 00:15:45 +01:00
|
|
|
case isNullDataScript(scriptVersion, script):
|
|
|
|
return NullDataTy
|
2019-04-19 11:04:01 +02:00
|
|
|
default:
|
2019-03-13 07:11:33 +01:00
|
|
|
return NonStandardTy
|
|
|
|
}
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetScriptClass returns the class of the script passed.
|
|
|
|
//
|
|
|
|
// NonStandardTy will be returned when the script does not parse.
|
|
|
|
func GetScriptClass(script []byte) ScriptClass {
|
2019-03-13 07:11:33 +01:00
|
|
|
const scriptVersion = 0
|
|
|
|
return typeOfScript(scriptVersion, script)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 23:30:59 +02:00
|
|
|
// NewScriptClass returns the ScriptClass corresponding to the string name
|
|
|
|
// provided as argument. ErrUnsupportedScriptType error is returned if the
|
|
|
|
// name doesn't correspond to any known ScriptClass.
|
|
|
|
//
|
|
|
|
// Not to be confused with GetScriptClass.
|
|
|
|
func NewScriptClass(name string) (*ScriptClass, error) {
|
|
|
|
for i, n := range scriptClassToName {
|
|
|
|
if n == name {
|
|
|
|
value := ScriptClass(i)
|
|
|
|
return &value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("%w: %s", ErrUnsupportedScriptType, name)
|
|
|
|
}
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
// expectedInputs returns the number of arguments required by a script.
|
|
|
|
// If the script is of unknown type such that the number can not be determined
|
|
|
|
// then -1 is returned. We are an internal function and thus assume that class
|
|
|
|
// is the real class of pops (and we can thus assume things that were determined
|
|
|
|
// while finding out the type).
|
2019-03-13 07:12:09 +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 expectedInputs(script []byte, class ScriptClass) int {
|
2015-05-01 22:20:07 +02:00
|
|
|
switch class {
|
|
|
|
case PubKeyTy:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
case PubKeyHashTy:
|
|
|
|
return 2
|
|
|
|
|
2016-10-19 02:45:40 +02:00
|
|
|
case WitnessV0PubKeyHashTy:
|
|
|
|
return 2
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
case ScriptHashTy:
|
|
|
|
// Not including script. That is handled by the caller.
|
|
|
|
return 1
|
|
|
|
|
2016-10-19 02:45:40 +02:00
|
|
|
case WitnessV0ScriptHashTy:
|
|
|
|
// Not including script. That is handled by the caller.
|
|
|
|
return 1
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
case MultiSigTy:
|
|
|
|
// Standard multisig has a push a small number for the number
|
|
|
|
// of sigs and number of keys. Check the first push instruction
|
|
|
|
// to see how many arguments are expected. typeOfScript already
|
|
|
|
// checked this so we know it'll be a small int. Also, due to
|
|
|
|
// 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.
|
2019-03-13 07:12:09 +01:00
|
|
|
return asSmallInt(script[0]) + 1
|
2015-05-01 22:20:07 +02:00
|
|
|
|
|
|
|
case NullDataTy:
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScriptInfo houses information about a script pair that is determined by
|
|
|
|
// CalcScriptInfo.
|
|
|
|
type ScriptInfo struct {
|
|
|
|
// PkScriptClass is the class of the public key script and is equivalent
|
|
|
|
// to calling GetScriptClass on it.
|
|
|
|
PkScriptClass ScriptClass
|
|
|
|
|
|
|
|
// NumInputs is the number of inputs provided by the public key script.
|
|
|
|
NumInputs int
|
|
|
|
|
|
|
|
// ExpectedInputs is the number of outputs required by the signature
|
|
|
|
// script and any pay-to-script-hash scripts. The number will be -1 if
|
|
|
|
// unknown.
|
|
|
|
ExpectedInputs int
|
|
|
|
|
|
|
|
// SigOps is the number of signature operations in the script pair.
|
|
|
|
SigOps int
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalcScriptInfo returns a structure providing data about the provided script
|
|
|
|
// pair. It will error if the pair is in someway invalid such that they can not
|
|
|
|
// be analysed, i.e. if they do not parse or the pkScript is not a push-only
|
|
|
|
// script
|
2019-03-13 07:11:33 +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.
|
|
|
|
//
|
|
|
|
// DEPRECATED. This will be removed in the next major version bump.
|
2016-10-19 02:45:40 +02:00
|
|
|
func CalcScriptInfo(sigScript, pkScript []byte, witness wire.TxWitness,
|
|
|
|
bip16, segwit bool) (*ScriptInfo, error) {
|
|
|
|
|
2019-03-13 07:12:09 +01:00
|
|
|
// Count the number of opcodes in the signature script while also ensuring
|
|
|
|
// that successfully parses. Since there is a check below to ensure the
|
|
|
|
// script is push only, this equates to the number of inputs to the public
|
|
|
|
// key script.
|
2019-03-13 07:11:33 +01:00
|
|
|
const scriptVersion = 0
|
2019-03-13 07:12:09 +01:00
|
|
|
var numInputs int
|
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, sigScript)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
numInputs++
|
|
|
|
}
|
|
|
|
if err := tokenizer.Err(); err != nil {
|
2015-05-01 22:20:07 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:09 +01:00
|
|
|
if err := checkScriptParses(scriptVersion, pkScript); err != nil {
|
2015-05-01 22:20:07 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-07 18:31:03 +01:00
|
|
|
// Can't have a signature script that doesn't just push data.
|
2019-03-13 07:12:09 +01:00
|
|
|
if !IsPushOnlyScript(sigScript) {
|
2017-01-07 18:31:03 +01:00
|
|
|
return nil, scriptError(ErrNotPushOnly,
|
|
|
|
"signature script is not push only")
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:09 +01:00
|
|
|
si := new(ScriptInfo)
|
|
|
|
si.PkScriptClass = typeOfScript(scriptVersion, pkScript)
|
|
|
|
|
|
|
|
si.ExpectedInputs = expectedInputs(pkScript, si.PkScriptClass)
|
2015-05-01 22:20:07 +02:00
|
|
|
|
2016-10-19 02:45:40 +02:00
|
|
|
switch {
|
2015-05-01 22:20:07 +02:00
|
|
|
// Count sigops taking into account pay-to-script-hash.
|
2016-10-19 02:45:40 +02:00
|
|
|
case si.PkScriptClass == ScriptHashTy && bip16 && !segwit:
|
2019-03-13 07:12:09 +01:00
|
|
|
// The redeem script is the final data push of the signature script.
|
|
|
|
redeemScript := finalOpcodeData(scriptVersion, sigScript)
|
|
|
|
reedeemClass := typeOfScript(scriptVersion, redeemScript)
|
|
|
|
rsInputs := expectedInputs(redeemScript, reedeemClass)
|
|
|
|
if rsInputs == -1 {
|
2015-05-01 22:20:07 +02:00
|
|
|
si.ExpectedInputs = -1
|
|
|
|
} else {
|
2019-03-13 07:12:09 +01:00
|
|
|
si.ExpectedInputs += rsInputs
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
2019-03-13 07:12:09 +01:00
|
|
|
si.SigOps = countSigOpsV0(redeemScript, true)
|
2016-10-19 02:45:40 +02:00
|
|
|
|
|
|
|
// All entries pushed to stack (or are OP_RESERVED and exec
|
|
|
|
// will fail).
|
2019-03-13 07:12:09 +01:00
|
|
|
si.NumInputs = numInputs
|
2016-10-19 02:45:40 +02:00
|
|
|
|
|
|
|
// If segwit is active, and this is a regular p2wkh output, then we'll
|
|
|
|
// treat the script as a p2pkh output in essence.
|
|
|
|
case si.PkScriptClass == WitnessV0PubKeyHashTy && segwit:
|
|
|
|
|
|
|
|
si.SigOps = GetWitnessSigOpCount(sigScript, pkScript, witness)
|
|
|
|
si.NumInputs = len(witness)
|
|
|
|
|
|
|
|
// We'll attempt to detect the nested p2sh case so we can accurately
|
|
|
|
// count the signature operations involved.
|
|
|
|
case si.PkScriptClass == ScriptHashTy &&
|
|
|
|
IsWitnessProgram(sigScript[1:]) && bip16 && segwit:
|
|
|
|
|
|
|
|
// Extract the pushed witness program from the sigScript so we
|
|
|
|
// can determine the number of expected inputs.
|
2019-03-13 07:11:33 +01:00
|
|
|
redeemClass := typeOfScript(scriptVersion, sigScript[1:])
|
2019-03-13 07:12:09 +01:00
|
|
|
shInputs := expectedInputs(sigScript[1:], redeemClass)
|
2016-10-19 02:45:40 +02:00
|
|
|
if shInputs == -1 {
|
|
|
|
si.ExpectedInputs = -1
|
|
|
|
} else {
|
|
|
|
si.ExpectedInputs += shInputs
|
|
|
|
}
|
|
|
|
|
|
|
|
si.SigOps = GetWitnessSigOpCount(sigScript, pkScript, witness)
|
|
|
|
|
|
|
|
si.NumInputs = len(witness)
|
2019-03-13 07:12:09 +01:00
|
|
|
si.NumInputs += numInputs
|
2016-10-19 02:45:40 +02:00
|
|
|
|
|
|
|
// If segwit is active, and this is a p2wsh output, then we'll need to
|
|
|
|
// examine the witness script to generate accurate script info.
|
|
|
|
case si.PkScriptClass == WitnessV0ScriptHashTy && segwit:
|
|
|
|
witnessScript := witness[len(witness)-1]
|
2019-03-13 07:11:33 +01:00
|
|
|
redeemClass := typeOfScript(scriptVersion, witnessScript)
|
2019-03-13 07:12:09 +01:00
|
|
|
shInputs := expectedInputs(witnessScript, redeemClass)
|
2016-10-19 02:45:40 +02:00
|
|
|
if shInputs == -1 {
|
|
|
|
si.ExpectedInputs = -1
|
|
|
|
} else {
|
|
|
|
si.ExpectedInputs += shInputs
|
|
|
|
}
|
|
|
|
|
|
|
|
si.SigOps = GetWitnessSigOpCount(sigScript, pkScript, witness)
|
|
|
|
si.NumInputs = len(witness)
|
|
|
|
|
|
|
|
default:
|
2019-03-13 07:12:09 +01:00
|
|
|
si.SigOps = countSigOpsV0(pkScript, true)
|
2016-10-19 02:45:40 +02:00
|
|
|
|
|
|
|
// All entries pushed to stack (or are OP_RESERVED and exec
|
|
|
|
// will fail).
|
2019-03-13 07:12:09 +01:00
|
|
|
si.NumInputs = numInputs
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return si, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalcMultiSigStats returns the number of public keys and signatures from
|
|
|
|
// a multi-signature transaction script. The passed script MUST already be
|
|
|
|
// known to be a multi-signature script.
|
2019-03-13 07:12:13 +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-05-01 22:20:07 +02:00
|
|
|
func CalcMultiSigStats(script []byte) (int, int, error) {
|
2019-03-13 07:12:13 +01:00
|
|
|
// The public keys are not needed here, so pass false to avoid the extra
|
|
|
|
// allocation.
|
|
|
|
const scriptVersion = 0
|
|
|
|
details := extractMultisigScriptDetails(scriptVersion, script, false)
|
|
|
|
if !details.valid {
|
2017-01-07 18:31:03 +01:00
|
|
|
str := fmt.Sprintf("script %x is not a multisig script", script)
|
|
|
|
return 0, 0, scriptError(ErrNotMultisigScript, str)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:13 +01:00
|
|
|
return details.numPubKeys, details.requiredSigs, nil
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// payToPubKeyHashScript creates a new script to pay a transaction
|
|
|
|
// output to a 20-byte pubkey hash. It is expected that the input is a valid
|
|
|
|
// hash.
|
|
|
|
func payToPubKeyHashScript(pubKeyHash []byte) ([]byte, error) {
|
|
|
|
return NewScriptBuilder().AddOp(OP_DUP).AddOp(OP_HASH160).
|
|
|
|
AddData(pubKeyHash).AddOp(OP_EQUALVERIFY).AddOp(OP_CHECKSIG).
|
|
|
|
Script()
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:45:40 +02:00
|
|
|
// payToWitnessPubKeyHashScript creates a new script to pay to a version 0
|
|
|
|
// pubkey hash witness program. The passed hash is expected to be valid.
|
|
|
|
func payToWitnessPubKeyHashScript(pubKeyHash []byte) ([]byte, error) {
|
|
|
|
return NewScriptBuilder().AddOp(OP_0).AddData(pubKeyHash).Script()
|
|
|
|
}
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
// payToScriptHashScript creates a new script to pay a transaction output to a
|
|
|
|
// script hash. It is expected that the input is a valid hash.
|
|
|
|
func payToScriptHashScript(scriptHash []byte) ([]byte, error) {
|
|
|
|
return NewScriptBuilder().AddOp(OP_HASH160).AddData(scriptHash).
|
|
|
|
AddOp(OP_EQUAL).Script()
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:45:40 +02:00
|
|
|
// payToWitnessPubKeyHashScript creates a new script to pay to a version 0
|
|
|
|
// script hash witness program. The passed hash is expected to be valid.
|
|
|
|
func payToWitnessScriptHashScript(scriptHash []byte) ([]byte, error) {
|
|
|
|
return NewScriptBuilder().AddOp(OP_0).AddData(scriptHash).Script()
|
|
|
|
}
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
// payToPubkeyScript creates a new script to pay a transaction output to a
|
|
|
|
// public key. It is expected that the input is a valid pubkey.
|
|
|
|
func payToPubKeyScript(serializedPubKey []byte) ([]byte, error) {
|
|
|
|
return NewScriptBuilder().AddData(serializedPubKey).
|
|
|
|
AddOp(OP_CHECKSIG).Script()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PayToAddrScript creates a new script to pay a transaction output to a the
|
|
|
|
// specified address.
|
|
|
|
func PayToAddrScript(addr btcutil.Address) ([]byte, error) {
|
2017-01-07 18:31:03 +01:00
|
|
|
const nilAddrErrStr = "unable to generate payment script for nil address"
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
switch addr := addr.(type) {
|
|
|
|
case *btcutil.AddressPubKeyHash:
|
|
|
|
if addr == nil {
|
2017-01-07 18:31:03 +01:00
|
|
|
return nil, scriptError(ErrUnsupportedAddress,
|
|
|
|
nilAddrErrStr)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
return payToPubKeyHashScript(addr.ScriptAddress())
|
|
|
|
|
|
|
|
case *btcutil.AddressScriptHash:
|
|
|
|
if addr == nil {
|
2017-01-07 18:31:03 +01:00
|
|
|
return nil, scriptError(ErrUnsupportedAddress,
|
|
|
|
nilAddrErrStr)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
return payToScriptHashScript(addr.ScriptAddress())
|
|
|
|
|
|
|
|
case *btcutil.AddressPubKey:
|
|
|
|
if addr == nil {
|
2017-01-07 18:31:03 +01:00
|
|
|
return nil, scriptError(ErrUnsupportedAddress,
|
|
|
|
nilAddrErrStr)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
return payToPubKeyScript(addr.ScriptAddress())
|
2016-10-19 02:45:40 +02:00
|
|
|
|
|
|
|
case *btcutil.AddressWitnessPubKeyHash:
|
|
|
|
if addr == nil {
|
2017-04-27 01:33:28 +02:00
|
|
|
return nil, scriptError(ErrUnsupportedAddress,
|
|
|
|
nilAddrErrStr)
|
2016-10-19 02:45:40 +02:00
|
|
|
}
|
|
|
|
return payToWitnessPubKeyHashScript(addr.ScriptAddress())
|
|
|
|
case *btcutil.AddressWitnessScriptHash:
|
|
|
|
if addr == nil {
|
2017-04-27 01:33:28 +02:00
|
|
|
return nil, scriptError(ErrUnsupportedAddress,
|
|
|
|
nilAddrErrStr)
|
2016-10-19 02:45:40 +02:00
|
|
|
}
|
|
|
|
return payToWitnessScriptHashScript(addr.ScriptAddress())
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 18:31:03 +01:00
|
|
|
str := fmt.Sprintf("unable to generate payment script for unsupported "+
|
|
|
|
"address type %T", addr)
|
|
|
|
return nil, scriptError(ErrUnsupportedAddress, str)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:03:14 +02:00
|
|
|
// NullDataScript creates a provably-prunable script containing OP_RETURN
|
2017-01-07 18:31:03 +01:00
|
|
|
// followed by the passed data. An Error with the error code ErrTooMuchNullData
|
|
|
|
// will be returned if the length of the passed data exceeds MaxDataCarrierSize.
|
2016-10-21 16:37:48 +02:00
|
|
|
func NullDataScript(data []byte) ([]byte, error) {
|
|
|
|
if len(data) > MaxDataCarrierSize {
|
2017-01-07 18:31:03 +01:00
|
|
|
str := fmt.Sprintf("data size %d is larger than max "+
|
|
|
|
"allowed size %d", len(data), MaxDataCarrierSize)
|
|
|
|
return nil, scriptError(ErrTooMuchNullData, str)
|
2016-10-21 16:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NewScriptBuilder().AddOp(OP_RETURN).AddData(data).Script()
|
|
|
|
}
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
// MultiSigScript returns a valid script for a multisignature redemption where
|
|
|
|
// nrequired of the keys in pubkeys are required to have signed the transaction
|
2017-01-07 18:31:03 +01:00
|
|
|
// for success. An Error with the error code ErrTooManyRequiredSigs will be
|
|
|
|
// returned if nrequired is larger than the number of keys provided.
|
2015-05-01 22:20:07 +02:00
|
|
|
func MultiSigScript(pubkeys []*btcutil.AddressPubKey, nrequired int) ([]byte, error) {
|
|
|
|
if len(pubkeys) < nrequired {
|
2017-01-07 18:31:03 +01:00
|
|
|
str := fmt.Sprintf("unable to generate multisig script with "+
|
|
|
|
"%d required signatures when there are only %d public "+
|
|
|
|
"keys available", nrequired, len(pubkeys))
|
|
|
|
return nil, scriptError(ErrTooManyRequiredSigs, str)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
builder := NewScriptBuilder().AddInt64(int64(nrequired))
|
|
|
|
for _, key := range pubkeys {
|
|
|
|
builder.AddData(key.ScriptAddress())
|
|
|
|
}
|
|
|
|
builder.AddInt64(int64(len(pubkeys)))
|
|
|
|
builder.AddOp(OP_CHECKMULTISIG)
|
|
|
|
|
|
|
|
return builder.Script()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushedData returns an array of byte slices containing any pushed data found
|
|
|
|
// in the passed script. This includes OP_0, but not OP_1 - OP_16.
|
2019-03-13 07:12:19 +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-05-01 22:20:07 +02:00
|
|
|
func PushedData(script []byte) ([][]byte, error) {
|
2019-03-13 07:12:19 +01:00
|
|
|
const scriptVersion = 0
|
2015-05-01 22:20:07 +02:00
|
|
|
|
|
|
|
var data [][]byte
|
2019-03-13 07:12:19 +01:00
|
|
|
tokenizer := MakeScriptTokenizer(scriptVersion, script)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
if tokenizer.Data() != nil {
|
|
|
|
data = append(data, tokenizer.Data())
|
|
|
|
} else if tokenizer.Opcode() == OP_0 {
|
2015-05-02 21:56:55 +02:00
|
|
|
data = append(data, nil)
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 07:12:19 +01:00
|
|
|
if err := tokenizer.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-01 22:20:07 +02:00
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:28 +01:00
|
|
|
// pubKeyHashToAddrs is a convenience function to attempt to convert the
|
|
|
|
// passed hash to a pay-to-pubkey-hash address housed within an address
|
|
|
|
// slice. It is used to consolidate common code.
|
|
|
|
func pubKeyHashToAddrs(hash []byte, params *chaincfg.Params) []btcutil.Address {
|
|
|
|
// Skip the pubkey hash if it's invalid for some reason.
|
|
|
|
var addrs []btcutil.Address
|
|
|
|
addr, err := btcutil.NewAddressPubKeyHash(hash, params)
|
|
|
|
if err == nil {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:27 +01:00
|
|
|
// scriptHashToAddrs is a convenience function to attempt to convert the passed
|
|
|
|
// hash to a pay-to-script-hash address housed within an address slice. It is
|
|
|
|
// used to consolidate common code.
|
|
|
|
func scriptHashToAddrs(hash []byte, params *chaincfg.Params) []btcutil.Address {
|
|
|
|
// Skip the hash if it's invalid for some reason.
|
|
|
|
var addrs []btcutil.Address
|
|
|
|
addr, err := btcutil.NewAddressScriptHashFromHash(hash, params)
|
|
|
|
if err == nil {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2015-05-01 22:20:07 +02:00
|
|
|
// ExtractPkScriptAddrs returns the type of script, addresses and required
|
|
|
|
// signatures associated with the passed PkScript. Note that it only works for
|
|
|
|
// 'standard' transaction script types. Any data such as public keys which are
|
|
|
|
// invalid are omitted from the results.
|
2019-03-13 07:12:38 +01:00
|
|
|
//
|
|
|
|
// NOTE: This function only attempts to identify version 0 scripts. The return
|
|
|
|
// value will indicate a nonstandard script type for other script versions along
|
|
|
|
// with an invalid script version error.
|
2015-05-01 22:20:07 +02:00
|
|
|
func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (ScriptClass, []btcutil.Address, int, error) {
|
2019-03-13 07:12:27 +01:00
|
|
|
|
2019-03-13 07:12:28 +01:00
|
|
|
// Check for pay-to-pubkey-hash script.
|
|
|
|
if hash := extractPubKeyHash(pkScript); hash != nil {
|
|
|
|
return PubKeyHashTy, pubKeyHashToAddrs(hash, chainParams), 1, nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:27 +01:00
|
|
|
// Check for pay-to-script-hash.
|
|
|
|
if hash := extractScriptHash(pkScript); hash != nil {
|
|
|
|
return ScriptHashTy, scriptHashToAddrs(hash, chainParams), 1, nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:31 +01:00
|
|
|
// Check for pay-to-pubkey script.
|
|
|
|
if data := extractPubKey(pkScript); data != nil {
|
|
|
|
var addrs []btcutil.Address
|
|
|
|
addr, err := btcutil.NewAddressPubKey(data, chainParams)
|
|
|
|
if err == nil {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return PubKeyTy, addrs, 1, nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:33 +01:00
|
|
|
// Check for multi-signature script.
|
|
|
|
const scriptVersion = 0
|
|
|
|
details := extractMultisigScriptDetails(scriptVersion, pkScript, true)
|
|
|
|
if details.valid {
|
|
|
|
// Convert the public keys while skipping any that are invalid.
|
|
|
|
addrs := make([]btcutil.Address, 0, len(details.pubKeys))
|
|
|
|
for _, pubkey := range details.pubKeys {
|
|
|
|
addr, err := btcutil.NewAddressPubKey(pubkey, chainParams)
|
|
|
|
if err == nil {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MultiSigTy, addrs, details.requiredSigs, nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:38 +01:00
|
|
|
// Check for null data script.
|
|
|
|
if isNullDataScript(scriptVersion, pkScript) {
|
|
|
|
// Null data transactions have no addresses or required signatures.
|
|
|
|
return NullDataTy, nil, 0, nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:18:51 +02:00
|
|
|
if hash := extractWitnessPubKeyHash(pkScript); hash != nil {
|
|
|
|
var addrs []btcutil.Address
|
|
|
|
addr, err := btcutil.NewAddressWitnessPubKeyHash(hash, chainParams)
|
|
|
|
if err == nil {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return WitnessV0PubKeyHashTy, addrs, 1, nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:22:52 +02:00
|
|
|
if hash := extractWitnessScriptHash(pkScript); hash != nil {
|
|
|
|
var addrs []btcutil.Address
|
|
|
|
addr, err := btcutil.NewAddressWitnessScriptHash(hash, chainParams)
|
|
|
|
if err == nil {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return WitnessV0ScriptHashTy, addrs, 1, nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:32:37 +02:00
|
|
|
// If none of the above passed, then the address must be non-standard.
|
|
|
|
return NonStandardTy, nil, 0, nil
|
2015-05-01 22:20:07 +02:00
|
|
|
}
|
2017-09-20 19:44:35 +02:00
|
|
|
|
|
|
|
// AtomicSwapDataPushes houses the data pushes found in atomic swap contracts.
|
|
|
|
type AtomicSwapDataPushes struct {
|
|
|
|
RecipientHash160 [20]byte
|
|
|
|
RefundHash160 [20]byte
|
2017-11-28 16:02:46 +01:00
|
|
|
SecretHash [32]byte
|
2018-02-16 22:18:43 +01:00
|
|
|
SecretSize int64
|
2017-09-20 19:44:35 +02:00
|
|
|
LockTime int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractAtomicSwapDataPushes returns the data pushes from an atomic swap
|
|
|
|
// contract. If the script is not an atomic swap contract,
|
|
|
|
// ExtractAtomicSwapDataPushes returns (nil, nil). Non-nil errors are returned
|
|
|
|
// for unparsable scripts.
|
|
|
|
//
|
|
|
|
// NOTE: Atomic swaps are not considered standard script types by the dcrd
|
|
|
|
// mempool policy and should be used with P2SH. The atomic swap format is also
|
|
|
|
// expected to change to use a more secure hash function in the future.
|
|
|
|
//
|
|
|
|
// This function is only defined in the txscript package due to API limitations
|
|
|
|
// which prevent callers using txscript to parse nonstandard scripts.
|
2019-03-13 07:12:25 +01:00
|
|
|
//
|
|
|
|
// DEPRECATED. This will be removed in the next major version bump. The error
|
|
|
|
// should also likely be removed if the code is reimplemented by any callers
|
|
|
|
// since any errors result in a nil result anyway.
|
2018-02-16 22:18:43 +01:00
|
|
|
func ExtractAtomicSwapDataPushes(version uint16, pkScript []byte) (*AtomicSwapDataPushes, error) {
|
2019-03-13 07:12:25 +01:00
|
|
|
// An atomic swap is of the form:
|
|
|
|
// IF
|
|
|
|
// SIZE <secret size> EQUALVERIFY SHA256 <32-byte secret> EQUALVERIFY DUP
|
|
|
|
// HASH160 <20-byte recipient hash>
|
|
|
|
// ELSE
|
|
|
|
// <locktime> CHECKLOCKTIMEVERIFY DROP DUP HASH160 <20-byte refund hash>
|
|
|
|
// ENDIF
|
|
|
|
// EQUALVERIFY CHECKSIG
|
|
|
|
type templateMatch struct {
|
|
|
|
expectCanonicalInt bool
|
|
|
|
maxIntBytes int
|
|
|
|
opcode byte
|
|
|
|
extractedInt int64
|
|
|
|
extractedData []byte
|
2017-09-20 19:44:35 +02:00
|
|
|
}
|
2019-03-13 07:12:25 +01:00
|
|
|
var template = [20]templateMatch{
|
|
|
|
{opcode: OP_IF},
|
|
|
|
{opcode: OP_SIZE},
|
|
|
|
{expectCanonicalInt: true, maxIntBytes: maxScriptNumLen},
|
|
|
|
{opcode: OP_EQUALVERIFY},
|
|
|
|
{opcode: OP_SHA256},
|
|
|
|
{opcode: OP_DATA_32},
|
|
|
|
{opcode: OP_EQUALVERIFY},
|
|
|
|
{opcode: OP_DUP},
|
|
|
|
{opcode: OP_HASH160},
|
|
|
|
{opcode: OP_DATA_20},
|
|
|
|
{opcode: OP_ELSE},
|
|
|
|
{expectCanonicalInt: true, maxIntBytes: cltvMaxScriptNumLen},
|
|
|
|
{opcode: OP_CHECKLOCKTIMEVERIFY},
|
|
|
|
{opcode: OP_DROP},
|
|
|
|
{opcode: OP_DUP},
|
|
|
|
{opcode: OP_HASH160},
|
|
|
|
{opcode: OP_DATA_20},
|
|
|
|
{opcode: OP_ENDIF},
|
|
|
|
{opcode: OP_EQUALVERIFY},
|
|
|
|
{opcode: OP_CHECKSIG},
|
2017-09-20 19:44:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 07:12:25 +01:00
|
|
|
var templateOffset int
|
|
|
|
tokenizer := MakeScriptTokenizer(version, pkScript)
|
|
|
|
for tokenizer.Next() {
|
|
|
|
// Not an atomic swap script if it has more opcodes than expected in the
|
|
|
|
// template.
|
|
|
|
if templateOffset >= len(template) {
|
2018-02-16 22:18:43 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-03-13 07:12:25 +01:00
|
|
|
|
|
|
|
op := tokenizer.Opcode()
|
|
|
|
data := tokenizer.Data()
|
|
|
|
tplEntry := &template[templateOffset]
|
|
|
|
if tplEntry.expectCanonicalInt {
|
|
|
|
switch {
|
|
|
|
case data != nil:
|
|
|
|
val, err := makeScriptNum(data, true, tplEntry.maxIntBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tplEntry.extractedInt = int64(val)
|
|
|
|
|
|
|
|
case isSmallInt(op):
|
|
|
|
tplEntry.extractedInt = int64(asSmallInt(op))
|
|
|
|
|
|
|
|
// Not an atomic swap script if the opcode does not push an int.
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if op != tplEntry.opcode {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tplEntry.extractedData = data
|
2017-09-20 19:44:35 +02:00
|
|
|
}
|
2019-03-13 07:12:25 +01:00
|
|
|
|
|
|
|
templateOffset++
|
|
|
|
}
|
|
|
|
if err := tokenizer.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !tokenizer.Done() || templateOffset != len(template) {
|
2017-09-20 19:44:35 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-03-13 07:12:25 +01:00
|
|
|
|
|
|
|
// At this point, the script appears to be an atomic swap, so populate and
|
|
|
|
// return the extacted data.
|
|
|
|
pushes := AtomicSwapDataPushes{
|
|
|
|
SecretSize: template[2].extractedInt,
|
|
|
|
LockTime: template[11].extractedInt,
|
|
|
|
}
|
|
|
|
copy(pushes.SecretHash[:], template[5].extractedData)
|
|
|
|
copy(pushes.RecipientHash160[:], template[9].extractedData)
|
|
|
|
copy(pushes.RefundHash160[:], template[16].extractedData)
|
|
|
|
return &pushes, nil
|
2017-09-20 19:44:35 +02:00
|
|
|
}
|