stdscript

package
v8.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2022 License: ISC Imports: 5 Imported by: 0

README

stdscript

Build Status ISC License Doc

Overview

This package provides facilities for identifying and extracting data from transaction scripts that are considered standard by the default policy of most nodes.

Understanding Standardness Versus Consensus

Under the hood, all regular transactions make use of transaction scripts with consensus-enforced semantics determined by an associated scripting language version in order to create smart contracts that impose constraints required to redeem funds.

The consensus rules consist of executing these transaction scripts in a virtual machine and further dictate that the result of the execution must evaluate to true. In particular, this means that scripts may be composed of any sequence of supported opcodes so long as they evaluate to true.

Recall that the full transaction script that is executed is the concatenation of two parts:

  1. The output script of a transaction (also known as the public key script)
  2. The input script of a transaction spending the aforementioned output (also known as the signature script)

Standard transaction scripts are public key scripts which are only composed of a well-defined sequence of opcodes that are widely used to achieve a specific goal such as requiring signatures to spend funds or anchoring data into the chain. In other words, standard scripts are a restricted subset of all possible public key scripts such that they conform to well-defined templates of a recognized form.

In addition, in order to improve overall efficiency of the network and limit potential denial of service attacks, the default policy of most nodes is to only accept and relay scripts which are considered standard, however, that in no way implies a transaction with non-standard scripts (any script that is not one of the recognized forms) is invalid per the consensus rules.

This distinction between standardness and consensus is extremely important for developers working on the consensus rules since what is considered a standard script can change at any time, while script execution for a given scripting language version must remain valid forever! To be explicit, the consensus rules must NOT reference or interact with code dealing with standard scripts directly in any way as that would lead to breaking perfectly valid scripts that may no longer be considered standard by policy.

This package is named stdscript to clearly convey that these scripts are particular recognized forms that most software considers standard by policy and therefore, as previously stated, must NOT be referenced by consensus code.

Pitfall with Special Script Types Enforced by Consensus

One important point of potential confusion to be cognizant of is that some of the script types, such as pay-to-script-hash, as well as all of the stake-related scripts, additionally have special consensus enforcement which means the consensus rules must also recognize them. In other words, they are both consensus scripts as well as recognized standard scripts.

This is particularly relevant to the Decred staking system since a distinguishing aspect of it is that its scripts are tightly controlled by consensus to only permit very specific scripts, unlike regular transactions which allow scripts composed of any sequence of opcodes that evaluate to true.

Consequently, it might be tempting to make use of the code in this package for identifying them for the purposes of enforcement since it is conveniently available, however, as previously stated, developers must NOT reference any code in this package from code that enforces consensus rules as this package will change with policy over time and therefore would lead to breaking the consensus rules.

Recognized Version 0 Standard Scripts

The following table lists the version 0 script types this package recognizes along with whether the type only applies to the staking system, and the type of the raw data that can be extracted:

Version 0 Script Type Stake? Data Type
p2pk-ecdsa-secp256k1 N []byte
p2pk-ed25519 N []byte
p2pk-schnorr-secp256k1 N []byte
p2pkh-ecdsa-secp256k1 N []byte
p2pkh-ed25519 N []byte
p2pkh-schnorr-secp256k1 N []byte
p2sh N []byte
ecdsa-multisig N MultiSigDetailsV0
nulldata N []byte
stake submission p2pkh Y []byte
stake submission p2sh Y []byte
stake generation p2pkh Y []byte
stake generation p2sh Y []byte
stake revocation p2pkh Y []byte
stake revocation p2sh Y []byte
stake change p2pkh Y []byte
stake change p2sh Y []byte
treasury add Y -
treasury generation p2pkh Y []byte
treasury generation p2sh Y []byte

Abbreviations:

  • p2pk = Pay-to-public-key
  • p2pkh = Pay-to-public-key-hash
  • p2sh = Pay-to-script-hash

Package Usage Primer

Interacting with existing standard scripts typically falls into the following categories:

  • Analyzing them to determine their type
  • Extracting data
Determining Script Type

In order to provide a more ergonomic and efficient API depending on the specific needs of callers, this package offers three approaches to determining script types:

  1. Determining the script type as a ScriptType enum. For example, DetermineScriptType and DetermineScriptTypeV0.
  2. Determining if a script is a specific type directly. For example, IsPubKeyHashScript, IsPubKeyHashScriptV0, and IsScriptHashScript.
  3. Determining if a script is a specific type by checking if the relevant version-specific data was extracted. For example, ExtractPubKeyHashV0 and ExtractScriptHashV0.

The first approach is most suitable for callers that want to work with and display information about a wide variety of script types, such as block explorers.

The second approach is better suited to callers that are looking for a very specific script type as it is more efficient to check for a single type instead of checking all types to produce an enum as in the first approach.

The third approach is aimed more at callers that need access to the relevant data from the script in addition to merely determining the type. Unless noted otherwise in the documentation for a specific method, the data extraction methods only return data if the provided script is actually of the associated type. For example, when a wallet needs to determine if a transaction script is a pay-to-pubkey-hash script that involves a particular public key hash, instead of calling IsPubKeyHashScriptV0 (or worse calling DetermineScriptType and checking the returned enum type) followed by ExtractPubKeyHashV0, it is more efficient to simply call ExtractPubKeyHashV0 and check if the data returned is not nil followed by using said data.

It is also worth noting that the first two approaches offer API methods that accept the scripting language version as a parameter as well as their version-specific variants that make it clear from the API exactly which script types are supported for a given version at compile time. The third method, on the other hand, only offers version-specific methods. This is discussed further in the Extracting Data section.

Extracting Data

Callers that work with standard scripts often need to obtain the type and version-specific data. For example, a caller will likely need to be able to determine the number of required signatures and individual public keys from a version 0 ECDSA multisignature script for further processing.

This package provides several version-specific data extraction methods, such as ExtractPubKeyHashV0 and ExtractMultiSigScriptDetailsV0, for this purpose.

It should be noted that unlike the script type determination methods, this package does not offer version-agnostic variants of the data extraction methods that accept a dynamic version. This is a deliberate design choice that was made because the associated data is highly dependent on both the specific type of script and the scripting language version, so any version-agnostic methods would need to return interfaces that callers would have to type assert based on the version thereby defeating the original intention of using a version-agnostic method to begin with.

Provably Pruneable Scripts

A provably pruneable script is a public key script that is of a specific form that is provably unspendable and therefore is safe to prune from the set of unspent transaction outputs. They are primarily useful for anchoring commitments into the blockchain and are the preferred method to achieve that goal.

This package provides the version-specific ProvablyPruneableScriptV0 method for this purpose.

Note that no version-agnostic variant of the method that accepts a dynamic version is provided since the exact details of what is considered standard is likely to change between scripting language versions, so callers will necessarily have to ensure appropriate data is provided based on the version.

Additional Convenience Methods

As mentioned in the overview, standardness only applies to public key scripts. However, despite perhaps not fitting well with the name of the package, some additional convenience methods are provided for detecting and extracting data from common redeem scripts (the target script of a standard pay-to-script-hash script) as follows:

  • Version 0 ECDSA multisignature redeem scripts
  • Version 0 atomic swap redeem scripts

Installation and Updating

This package is part of the github.com/Decred-Next/dcrnd/txscript/version4/v8 module. Use the standard go tooling for working with modules to incorporate it.

Examples

  • DetermineScriptType

    Demonstrates determining the type of a script for a given scripting language version.

  • ExtractPubKeyHashV0

    Demonstrates extracting a public key hash from a standard pay-to-pubkey-hash script for scripting language version 0.

  • ExtractScriptHashV0

    Demonstrates extracting a script hash from a standard pay-to-script-hash script for scripting language version 0.

License

Package stdscript is licensed under the copyfree ISC License.

Documentation

Overview

Package stdscript provides facilities for working with standard scripts.

Package stdscript provides facilities for working with standard scripts.

Index

Examples

Constants

View Source
const (
	// ErrUnsupportedScriptVersion indicates that a given script version is not
	// supported.
	ErrUnsupportedScriptVersion = ErrorKind("ErrUnsupportedScriptVersion")

	// ErrTooManyRequiredSigs is returned from MultiSigScript when the
	// specified number of required signatures is larger than the number of
	// provided public keys.
	ErrTooManyRequiredSigs = ErrorKind("ErrTooManyRequiredSigs")

	// ErrPubKeyType is returned when a script contains invalid public keys.
	ErrPubKeyType = ErrorKind("ErrPubKeyType")

	// ErrTooMuchNullData is returned when attempting to generate a
	// provably-pruneable script with data that exceeds the maximum allowed
	// length.
	ErrTooMuchNullData = ErrorKind("ErrTooMuchNullData")
)

These constants are used to identify a specific ErrorKind.

View Source
const (
	// MaxDataCarrierSizeV0 is the maximum number of bytes allowed in pushed
	// data to be considered a standard version 0 provably pruneable nulldata
	// script.
	MaxDataCarrierSizeV0 = 256
)

Variables

This section is empty.

Functions

func DetermineRequiredSigs

func DetermineRequiredSigs(scriptVersion uint16, script []byte) uint16

DetermineRequiredSigs attempts to identify the number of signatures required by the passed script for the known standard types.

NOTE: Version 0 scripts are the only currently supported version. It will always return 0 for other script versions.

Similarly, 0 is returned when the script does not parse or is not one of the known standard types.

func DetermineRequiredSigsV0

func DetermineRequiredSigsV0(script []byte) uint16

DetermineRequiredSigsV0 attempts to identify the number of signatures required by the passed version 0 script for the known standard types.

It will return 0 when the script does not parse or is not one of the known standard types.

func ExtractCompressedPubKeyV0

func ExtractCompressedPubKeyV0(script []byte) []byte

ExtractCompressedPubKeyV0 extracts a compressed public key from the passed script if it is a standard version 0 pay-to-compressed-secp256k1-pubkey script. It will return nil otherwise.

func ExtractPubKeyAltDetailsV0

func ExtractPubKeyAltDetailsV0(script []byte) ([]byte, dcrec.SignatureType)

ExtractPubKeyAltDetailsV0 extracts the public key and signature type from the passed script if it is a standard version 0 pay-to-alt-pubkey script. It will return nil otherwise.

func ExtractPubKeyEd25519V0

func ExtractPubKeyEd25519V0(script []byte) []byte

ExtractPubKeyEd25519V0 extracts a public key from the passed script if it is a standard version 0 pay-to-ed25519-pubkey script. It will return nil otherwise.

func ExtractPubKeyHashAltDetailsV0

func ExtractPubKeyHashAltDetailsV0(script []byte) ([]byte, dcrec.SignatureType)

ExtractPubKeyHashAltDetailsV0 extracts the public key hash and signature type from the passed script if it is a standard version 0 pay-to-alt-pubkey-hash script. It will return nil otherwise.

func ExtractPubKeyHashEd25519V0

func ExtractPubKeyHashEd25519V0(script []byte) []byte

ExtractPubKeyHashEd25519V0 extracts the public key hash from the passed script if it is a standard version 0 pay-to-pubkey-hash-ed25519 script. It will return nil otherwise.

func ExtractPubKeyHashSchnorrSecp256k1V0

func ExtractPubKeyHashSchnorrSecp256k1V0(script []byte) []byte

ExtractPubKeyHashSchnorrSecp256k1V0 extracts the public key hash from the passed script if it is a standard version 0 pay-to-pubkey-hash-schnorr-secp256k1 script. It will return nil otherwise.

func ExtractPubKeyHashV0

func ExtractPubKeyHashV0(script []byte) []byte

ExtractPubKeyHashV0 extracts the public key hash from the passed script if it is a standard version 0 pay-to-pubkey-hash-ecdsa-secp256k1 script. It will return nil otherwise.

Example

This example demonstrates extracting a public key hash from a standard pay-to-pubkey-hash script for scripting language version 0.

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/Decred-Next/dcrnd/txscript/version4/v8/stdscript"
)

// hexToBytes converts the passed hex string into bytes and will panic if there
// is an error.  This is only provided for the hard-coded constants so errors in
// the source code can be detected. It will only (and must only) be called with
// hard-coded values.
func hexToBytes(s string) []byte {
	b, err := hex.DecodeString(s)
	if err != nil {
		panic("invalid hex in source file: " + s)
	}
	return b
}

func main() {
	// Ordinarily the script version and script would be obtained from the
	// output of a transaction, but the version is assumed to be zero and the
	// script is hard coded here for the purposes of this example.
	script := hexToBytes("76a914e280cb6e66b96679aec288b1fbdbd4db08077a1b88ac")
	pkHash := stdscript.ExtractPubKeyHashV0(script)
	fmt.Printf("public key hash: %x\n", pkHash)

}
Output:

public key hash: e280cb6e66b96679aec288b1fbdbd4db08077a1b

func ExtractPubKeySchnorrSecp256k1V0

func ExtractPubKeySchnorrSecp256k1V0(script []byte) []byte

ExtractPubKeySchnorrSecp256k1V0 extracts a public key from the passed script if it is a standard version 0 pay-to-schnorr-secp256k1-pubkey script. It will return nil otherwise.

func ExtractPubKeyV0

func ExtractPubKeyV0(script []byte) []byte

ExtractPubKeyV0 extracts either a compressed or uncompressed public key from the passed script if it is either a standard version 0 pay-to-compressed-secp256k1-pubkey or pay-to-uncompressed-secp256k1-pubkey script, respectively. It will return nil otherwise.

func ExtractScriptHashV0

func ExtractScriptHashV0(script []byte) []byte

ExtractScriptHashV0 extracts the script hash from the passed script if it is a standard version 0 pay-to-script-hash script. It will return nil otherwise.

Example

This example demonstrates extracting a script hash from a standard pay-to-script-hash script for script language version 0.

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/Decred-Next/dcrnd/txscript/version4/v8/stdscript"
)

// hexToBytes converts the passed hex string into bytes and will panic if there
// is an error.  This is only provided for the hard-coded constants so errors in
// the source code can be detected. It will only (and must only) be called with
// hard-coded values.
func hexToBytes(s string) []byte {
	b, err := hex.DecodeString(s)
	if err != nil {
		panic("invalid hex in source file: " + s)
	}
	return b
}

func main() {
	// Ordinarily the script version and script would be obtained from the
	// output of a transaction, but the version is assumed to be zero and the
	// script is hard coded here for the purposes of this example.
	script := hexToBytes("a914433ec2ac1ffa1b7b7d027f564529c57197f9ae8887")
	scriptHash := stdscript.ExtractScriptHashV0(script)
	fmt.Printf("script hash: %x\n", scriptHash)

}
Output:

script hash: 433ec2ac1ffa1b7b7d027f564529c57197f9ae88

func ExtractStakeChangePubKeyHashV0

func ExtractStakeChangePubKeyHashV0(script []byte) []byte

ExtractStakeChangePubKeyHashV0 extracts the public key hash from the passed script if it is a standard version 0 stake change pay-to-pubkey-hash script. It will return nil otherwise.

func ExtractStakeChangeScriptHashV0

func ExtractStakeChangeScriptHashV0(script []byte) []byte

ExtractStakeChangeScriptHashV0 extracts the script hash from the passed script if it is a standard version 0 stake change pay-to-script-hash script. It will return nil otherwise.

func ExtractStakeGenPubKeyHashV0

func ExtractStakeGenPubKeyHashV0(script []byte) []byte

ExtractStakeGenPubKeyHashV0 extracts the public key hash from the passed script if it is a standard version 0 stake generation pay-to-pubkey-hash script. It will return nil otherwise.

func ExtractStakeGenScriptHashV0

func ExtractStakeGenScriptHashV0(script []byte) []byte

ExtractStakeGenScriptHashV0 extracts the script hash from the passed script if it is a standard version 0 stake generation pay-to-script-hash script. It will return nil otherwise.

func ExtractStakePubKeyHashV0

func ExtractStakePubKeyHashV0(script []byte) []byte

ExtractStakePubKeyHashV0 extracts the public key hash from the passed script if it is any one of the supported standard version 0 stake-tagged pay-to-pubkey-hash scripts. It will return nil otherwise.

func ExtractStakeRevocationPubKeyHashV0

func ExtractStakeRevocationPubKeyHashV0(script []byte) []byte

ExtractStakeRevocationPubKeyHashV0 extracts the public key hash from the passed script if it is a standard version 0 stake revocation pay-to-pubkey-hash script. It will return nil otherwise.

func ExtractStakeRevocationScriptHashV0

func ExtractStakeRevocationScriptHashV0(script []byte) []byte

ExtractStakeRevocationScriptHashV0 extracts the script hash from the passed script if it is a standard version 0 stake revocation pay-to-script-hash script. It will return nil otherwise.

func ExtractStakeScriptHashV0

func ExtractStakeScriptHashV0(script []byte) []byte

ExtractStakeScriptHashV0 extracts the script hash from the passed script if it is any one of the supported standard version 0 stake-tagged pay-to-script-hash scripts. It will return nil otherwise.

func ExtractStakeSubmissionPubKeyHashV0

func ExtractStakeSubmissionPubKeyHashV0(script []byte) []byte

ExtractStakeSubmissionPubKeyHashV0 extracts the public key hash from the passed script if it is a standard version 0 stake submission pay-to-pubkey-hash script. It will return nil otherwise.

func ExtractStakeSubmissionScriptHashV0

func ExtractStakeSubmissionScriptHashV0(script []byte) []byte

ExtractStakeSubmissionScriptHashV0 extracts the script hash from the passed script if it is a standard version 0 stake submission pay-to-script-hash script. It will return nil otherwise.

func ExtractTreasuryGenPubKeyHashV0

func ExtractTreasuryGenPubKeyHashV0(script []byte) []byte

ExtractTreasuryGenPubKeyHashV0 extracts the public key hash from the passed script if it is a standard version 0 treasury generation pay-to-pubkey-hash script. It will return nil otherwise.

func ExtractTreasuryGenScriptHashV0

func ExtractTreasuryGenScriptHashV0(script []byte) []byte

ExtractTreasuryGenScriptHashV0 extracts the script hash from the passed script if it is a standard version 0 treasury generation pay-to-script-hash script. It will return nil otherwise.

func ExtractUncompressedPubKeyV0

func ExtractUncompressedPubKeyV0(script []byte) []byte

ExtractUncompressedPubKeyV0 extracts an uncompressed public key from the passed script if it is a standard version 0 pay-to-uncompressed-secp256k1-pubkey script. It will return nil otherwise.

func IsMultiSigScript

func IsMultiSigScript(scriptVersion uint16, script []byte) bool

IsMultiSigScript returns whether or not the passed script is a standard ECDSA multisig script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsMultiSigScriptV0

func IsMultiSigScriptV0(script []byte) bool

IsMultiSigScriptV0 returns whether or not the passed script is a standard version 0 ECDSA multisig script.

NOTE: This function is only valid for version 0 scripts. It will always return false for other script versions.

func IsMultiSigSigScript

func IsMultiSigSigScript(scriptVersion uint16, script []byte) bool

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: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsMultiSigSigScriptV0

func IsMultiSigSigScriptV0(script []byte) bool

IsMultiSigSigScriptV0 returns whether or not the passed script appears to be a version 0 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.

func IsNullDataScript

func IsNullDataScript(scriptVersion uint16, script []byte) bool

IsNullDataScript returns whether or not the passed script is a standard null data script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsNullDataScriptV0

func IsNullDataScriptV0(script []byte) bool

IsNullDataScriptV0 returns whether or not the passed script is a standard version 0 null data script.

func IsPubKeyEd25519Script

func IsPubKeyEd25519Script(scriptVersion uint16, script []byte) bool

IsPubKeyEd25519Script returns whether or not the passed script is a standard pay-to-ed25519-pubkey script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsPubKeyEd25519ScriptV0

func IsPubKeyEd25519ScriptV0(script []byte) bool

IsPubKeyEd25519ScriptV0 returns whether or not the passed script is a standard version 0 pay-to-ed25519-pubkey script.

func IsPubKeyHashEd25519Script

func IsPubKeyHashEd25519Script(scriptVersion uint16, script []byte) bool

IsPubKeyHashEd25519Script returns whether or not the passed script is a standard pay-to-pubkey-hash-ed25519 script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsPubKeyHashEd25519ScriptV0

func IsPubKeyHashEd25519ScriptV0(script []byte) bool

IsPubKeyHashEd25519ScriptV0 returns whether or not the passed script is a standard version 0 pay-to-pubkey-hash-ed25519 script.

func IsPubKeyHashSchnorrSecp256k1Script

func IsPubKeyHashSchnorrSecp256k1Script(scriptVersion uint16, script []byte) bool

IsPubKeyHashSchnorrSecp256k1Script returns whether or not the passed script is a standard pay-to-pubkey-hash-schnorr-secp256k1 script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsPubKeyHashSchnorrSecp256k1ScriptV0

func IsPubKeyHashSchnorrSecp256k1ScriptV0(script []byte) bool

IsPubKeyHashSchnorrSecp256k1ScriptV0 returns whether or not the passed script is a standard version 0 pay-to-pubkey-hash-schnorr-secp256k1 script.

func IsPubKeyHashScript

func IsPubKeyHashScript(scriptVersion uint16, script []byte) bool

IsPubKeyHashScript returns whether or not the passed script is a standard pay-to-pubkey-hash-ecdsa-secp256k1 script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsPubKeyHashScriptV0

func IsPubKeyHashScriptV0(script []byte) bool

IsPubKeyHashScriptV0 returns whether or not the passed script is a standard version 0 pay-to-pubkey-hash-ecdsa-secp256k1 script.

func IsPubKeySchnorrSecp256k1Script

func IsPubKeySchnorrSecp256k1Script(scriptVersion uint16, script []byte) bool

IsPubKeySchnorrSecp256k1Script returns whether or not the passed script is a standard pay-to-schnorr-secp256k1-pubkey script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsPubKeySchnorrSecp256k1ScriptV0

func IsPubKeySchnorrSecp256k1ScriptV0(script []byte) bool

IsPubKeySchnorrSecp256k1ScriptV0 returns whether or not the passed script is a standard version 0 pay-to-schnorr-secp256k1-pubkey script.

func IsPubKeyScript

func IsPubKeyScript(scriptVersion uint16, script []byte) bool

IsPubKeyScript returns whether or not the passed script is either a standard pay-to-compressed-secp256k1-pubkey or pay-to-uncompressed-secp256k1-pubkey script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsPubKeyScriptV0

func IsPubKeyScriptV0(script []byte) bool

IsPubKeyScriptV0 returns whether or not the passed script is either a standard version 0 pay-to-compressed-secp256k1-pubkey or pay-to-uncompressed-secp256k1-pubkey script.

func IsScriptHashScript

func IsScriptHashScript(scriptVersion uint16, script []byte) bool

IsScriptHashScript returns whether or not the passed script is a standard pay-to-script-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsScriptHashScriptV0

func IsScriptHashScriptV0(script []byte) bool

IsScriptHashScriptV0 returns whether or not the passed script is a standard version 0 pay-to-script-hash script.

func IsStakeChangePubKeyHashScript

func IsStakeChangePubKeyHashScript(scriptVersion uint16, script []byte) bool

IsStakeChangePubKeyHashScript returns whether or not the passed script is a standard stake change pay-to-pubkey-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeChangePubKeyHashScriptV0

func IsStakeChangePubKeyHashScriptV0(script []byte) bool

IsStakeChangePubKeyHashScriptV0 returns whether or not the passed script is a standard version 0 stake change pay-to-pubkey-hash script.

func IsStakeChangeScriptHashScript

func IsStakeChangeScriptHashScript(scriptVersion uint16, script []byte) bool

IsStakeChangeScriptHashScript returns whether or not the passed script is a standard stake change pay-to-script-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeChangeScriptHashScriptV0

func IsStakeChangeScriptHashScriptV0(script []byte) bool

IsStakeChangeScriptHashScriptV0 returns whether or not the passed script is a standard version 0 stake change pay-to-script-hash script.

func IsStakeGenPubKeyHashScript

func IsStakeGenPubKeyHashScript(scriptVersion uint16, script []byte) bool

IsStakeGenPubKeyHashScript returns whether or not the passed script is a standard stake generation pay-to-pubkey-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeGenPubKeyHashScriptV0

func IsStakeGenPubKeyHashScriptV0(script []byte) bool

IsStakeGenPubKeyHashScriptV0 returns whether or not the passed script is a standard version 0 stake generation pay-to-pubkey-hash script.

func IsStakeGenScriptHashScript

func IsStakeGenScriptHashScript(scriptVersion uint16, script []byte) bool

IsStakeGenScriptHashScript returns whether or not the passed script is a standard stake generation pay-to-script-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeGenScriptHashScriptV0

func IsStakeGenScriptHashScriptV0(script []byte) bool

IsStakeGenScriptHashScriptV0 returns whether or not the passed script is a standard version 0 stake generation pay-to-script-hash script.

func IsStakeRevocationPubKeyHashScript

func IsStakeRevocationPubKeyHashScript(scriptVersion uint16, script []byte) bool

IsStakeRevocationPubKeyHashScript returns whether or not the passed script is a standard stake revocation pay-to-pubkey-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeRevocationPubKeyHashScriptV0

func IsStakeRevocationPubKeyHashScriptV0(script []byte) bool

IsStakeRevocationPubKeyHashScriptV0 returns whether or not the passed script is a standard version 0 stake revocation pay-to-pubkey-hash script.

func IsStakeRevocationScriptHashScript

func IsStakeRevocationScriptHashScript(scriptVersion uint16, script []byte) bool

IsStakeRevocationScriptHashScript returns whether or not the passed script is a standard stake revocation pay-to-script-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeRevocationScriptHashScriptV0

func IsStakeRevocationScriptHashScriptV0(script []byte) bool

IsStakeRevocationScriptHashScriptV0 returns whether or not the passed script is a standard version 0 stake revocation pay-to-script-hash script.

func IsStakeSubmissionPubKeyHashScript

func IsStakeSubmissionPubKeyHashScript(scriptVersion uint16, script []byte) bool

IsStakeSubmissionPubKeyHashScript returns whether or not the passed script is a standard stake submission pay-to-pubkey-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeSubmissionPubKeyHashScriptV0

func IsStakeSubmissionPubKeyHashScriptV0(script []byte) bool

IsStakeSubmissionPubKeyHashScriptV0 returns whether or not the passed script is a standard version 0 stake submission pay-to-pubkey-hash script.

func IsStakeSubmissionScriptHashScript

func IsStakeSubmissionScriptHashScript(scriptVersion uint16, script []byte) bool

IsStakeSubmissionScriptHashScript returns whether or not the passed script is a standard stake submission pay-to-script-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsStakeSubmissionScriptHashScriptV0

func IsStakeSubmissionScriptHashScriptV0(script []byte) bool

IsStakeSubmissionScriptHashScriptV0 returns whether or not the passed script is a standard version 0 stake submission pay-to-script-hash script.

func IsStandardAltSignatureTypeV0

func IsStandardAltSignatureTypeV0(op byte) bool

IsStandardAltSignatureTypeV0 returns whether or not the provided version 0 script opcode represents a push of a standard alt signature type.

func IsTreasuryAddScript

func IsTreasuryAddScript(scriptVersion uint16, script []byte) bool

IsTreasuryAddScript returns whether or not the passed script is a supported treasury add script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsTreasuryAddScriptV0

func IsTreasuryAddScriptV0(script []byte) bool

IsTreasuryAddScriptV0 returns whether or not the passed script is a supported version 0 treasury add script.

func IsTreasuryGenPubKeyHashScript

func IsTreasuryGenPubKeyHashScript(scriptVersion uint16, script []byte) bool

IsTreasuryGenPubKeyHashScript returns whether or not the passed script is a standard treasury generation pay-to-pubkey-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsTreasuryGenPubKeyHashScriptV0

func IsTreasuryGenPubKeyHashScriptV0(script []byte) bool

IsTreasuryGenPubKeyHashScriptV0 returns whether or not the passed script is a standard version 0 treasury generation pay-to-pubkey-hash script.

func IsTreasuryGenScriptHashScript

func IsTreasuryGenScriptHashScript(scriptVersion uint16, script []byte) bool

IsTreasuryGenScriptHashScript returns whether or not the passed script is a standard treasury generation pay-to-script-hash script.

NOTE: Version 0 scripts are the only currently supported version. It will always return false for other script versions.

func IsTreasuryGenScriptHashScriptV0

func IsTreasuryGenScriptHashScriptV0(script []byte) bool

IsTreasuryGenScriptHashScriptV0 returns whether or not the passed script is a standard version 0 treasury generation pay-to-script-hash script.

func MultiSigRedeemScriptFromScriptSigV0

func MultiSigRedeemScriptFromScriptSigV0(script []byte) []byte

MultiSigRedeemScriptFromScriptSigV0 attempts to extract a multi-signature redeem script from a version 0 P2SH-redeeming input. The script is expected to already have been checked to be a version 0 multisignature script prior to calling this function. The results are undefined for other script types.

func MultiSigScriptV0

func MultiSigScriptV0(threshold int, pubKeys ...[]byte) ([]byte, error)

MultiSigScriptV0 returns a valid version 0 script for a multisignature redemption where the specified threshold number of the keys in the given public keys are required to have signed the transaction for success.

The provided public keys must be serialized in the compressed format or an error with kind ErrPubKeyType will be returned.

An Error with kind ErrTooManyRequiredSigs will be returned if the threshold is larger than the number of keys provided.

func ProvablyPruneableScriptV0

func ProvablyPruneableScriptV0(data []byte) ([]byte, error)

ProvablyPruneableScriptV0 returns a valid version 0 provably-pruneable script which consists of an OP_RETURN followed by the passed data. An Error with kind ErrTooMuchNullData will be returned if the length of the passed data exceeds MaxDataCarrierSizeV0.

Types

type AtomicSwapDataPushesV0

type AtomicSwapDataPushesV0 struct {
	RecipientHash160 [20]byte
	RefundHash160    [20]byte
	SecretHash       [32]byte
	SecretSize       int64
	LockTime         int64
}

AtomicSwapDataPushesV0 houses the data pushes found in hash-based atomic swap contracts using version 0 scripts.

func ExtractAtomicSwapDataPushesV0

func ExtractAtomicSwapDataPushesV0(redeemScript []byte) *AtomicSwapDataPushesV0

ExtractAtomicSwapDataPushesV0 returns the data pushes from an atomic swap contract using version 0 scripts if it is one. It will return nil otherwise.

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.

type Error

type Error struct {
	Err         error
	Description string
}

Error identifies an script-related error.

It has full support for errors.Is and errors.As, so the caller can ascertain the specific reason for the error by checking the underlying error.

func (Error) Error

func (e Error) Error() string

Error satisfies the error interface and prints human-readable errors.

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap returns the underlying wrapped error.

type ErrorKind

type ErrorKind string

ErrorKind identifies a kind of error.

func (ErrorKind) Error

func (e ErrorKind) Error() string

Error satisfies the error interface and prints human-readable errors.

type MultiSigDetailsV0

type MultiSigDetailsV0 struct {
	RequiredSigs uint16
	NumPubKeys   uint16
	PubKeys      [][]byte
	Valid        bool
}

MultiSigDetailsV0 houses details extracted from a standard version 0 ECDSA multisig script.

func ExtractMultiSigScriptDetailsV0

func ExtractMultiSigScriptDetailsV0(script []byte, extractPubKeys bool) MultiSigDetailsV0

ExtractMultiSigScriptDetailsV0 attempts to extract details from the passed version 0 script if it is a standard ECDSA 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.

type ScriptType

type ScriptType byte

ScriptType identifies the type of known scripts in the blockchain that are typically considered standard by the default policy of most nodes. All other scripts are considered non-standard.

const (
	// STNonStandard indicates a script is none of the recognized standard
	// forms.
	STNonStandard ScriptType = iota

	// STPubKeyEcdsaSecp256k1 identifies a standard script that imposes an
	// encumbrance that requires a valid ECDSA signature for a specific
	// secp256k1 public key.
	//
	// This is commonly referred to as either a pay-to-pubkey (P2PK) script or
	// the more specific pay-to-pubkey-ecdsa-secp256k1 script.
	STPubKeyEcdsaSecp256k1

	// STPubKeyEd25519 identifies a standard script that imposes an encumbrance
	// that requires a valid Ed25519 signature for a specific Ed25519 public
	// key.
	//
	// This is commonly referred to as a pay-to-pubkey-ed25519 script.
	STPubKeyEd25519

	// STPubKeySchnorrSecp256k1 identifies a standard script that imposes an
	// encumbrance that requires a valid EC-Schnorr-DCRv0 signature for a
	// specific secp256k1 public key.
	//
	// This is commonly referred to as a pay-to-pubkey-schnorr-secp256k1 script.
	STPubKeySchnorrSecp256k1

	// STPubKeyHashEcdsaSecp256k1 identifies a standard script that imposes an
	// encumbrance that requires a secp256k1 public key that hashes to a
	// specific value along with a valid ECDSA signature for that public key.
	//
	// This is commonly referred to as either a pay-to-pubkey-hash (P2PKH)
	// script or the more specific pay-to-pubkey-hash-ecdsa-secp256k1 script.
	STPubKeyHashEcdsaSecp256k1

	// STPubKeyHashEd25519 identifies a standard script that imposes an
	// encumbrance that requires an Ed25519 public key that hashes to a specific
	// value along with a valid Ed25519 signature for that public key.
	//
	// This is commonly referred to as a pay-to-pubkey-hash-ed25519 script.
	STPubKeyHashEd25519

	// STPubKeyHashSchnorrSecp256k1 identifies a standard script that imposes an
	// encumbrance that requires a secp256k1 public key that hashes to a
	// specific value along with a valid EC-Schnorr-DCRv0 signature for that
	// public key.
	//
	// This is commonly referred to as a pay-to-pubkey-hash-schnorr-secp256k1
	// script.
	STPubKeyHashSchnorrSecp256k1

	// STScriptHash identifies a standard script that imposes an encumbrance
	// that requires a script that hashes to a specific value along with all of
	// the encumbrances that script itself imposes.  The script is commonly
	// referred to as a redeem script.
	//
	// This is commonly referred to as pay-to-script-hash (P2SH).
	STScriptHash

	// STMultiSig identifies a standard script that imposes an encumbrance that
	// requires a given number of valid ECDSA signatures which correspond to
	// given secp256k1 public keys.
	//
	// This is commonly referred to as a standard ECDSA n-of-m multi-signature
	// script.
	STMultiSig

	// STNullData identifies a standard null data script that is provably
	// prunable.
	STNullData

	// STStakeSubmissionPubKeyHash identifies a script that is only valid when
	// used as part of a ticket purchase transaction in the staking system and
	// is used for imposing voting rights.
	//
	// It imposes an encumbrance that requires a secp256k1 public key that
	// hashes to a specific value along with a valid ECDSA signature for that
	// public key.
	STStakeSubmissionPubKeyHash

	// STStakeSubmissionScriptHash identifies a script that is only valid when
	// used as part of a ticket purchase transaction in the staking system and
	// is used for imposing voting rights.
	//
	// It imposes an encumbrance that requires a script that hashes to a
	// specific value along with all of the encumbrances that script itself
	// imposes.  The script is commonly referred to as a redeem script.
	STStakeSubmissionScriptHash

	// STStakeGenPubKeyHash identifies a script that is only valid when used as
	// part of a vote transaction in the staking system.
	//
	// It imposes an encumbrance that requires a secp256k1 public key that
	// hashes to a specific value along with a valid ECDSA signature for that
	// public key.
	STStakeGenPubKeyHash

	// STStakeGenScriptHash identifies a script that is only valid when used as
	// part of a vote transaction in the staking system.
	//
	// It imposes an encumbrance that requires a script that hashes to a
	// specific value along with all of the encumbrances that script itself
	// imposes.  The script is commonly referred to as a redeem script.
	STStakeGenScriptHash

	// STStakeRevocationPubKeyHash identifies a script that is only valid when
	// used as part of a revocation transaction in the staking system.
	//
	// It imposes an encumbrance that requires a secp256k1 public key that
	// hashes to a specific value along with a valid ECDSA signature for that
	// public key.
	STStakeRevocationPubKeyHash

	// STStakeRevocationScriptHash identifies a script that is only valid when
	// used as part of a revocation transaction in the staking system.
	//
	// It imposes an encumbrance that requires a script that hashes to a
	// specific value along with all of the encumbrances that script itself
	// imposes.  The script is commonly referred to as a redeem script.
	STStakeRevocationScriptHash

	// STStakeChangePubKeyHash identifies a script that is only valid when used
	// as part of supported transactions in the staking system.
	//
	// It imposes an encumbrance that requires a secp256k1 public key that
	// hashes to a specific value along with a valid ECDSA signature for that
	// public key.
	STStakeChangePubKeyHash

	// STStakeChangeScriptHash identifies a script that is only valid when used
	// as part of supported transactions in the staking system.
	//
	// It imposes an encumbrance that requires a script that hashes to a
	// specific value along with all of the encumbrances that script itself
	// imposes.  The script is commonly referred to as a redeem script.
	STStakeChangeScriptHash

	// STTreasuryAdd identifies a script that is only valid when used as part
	// supported transactions in the staking system and adds value to the
	// treasury account.
	STTreasuryAdd

	// STTreasuryGenPubKeyHash identifies a script that is only valid when used
	// as part supported transactions in the staking system and generates utxos
	// from the treasury account.
	//
	// It imposes an encumbrance that requires a secp256k1 public key that
	// hashes to a specific value along with a valid ECDSA signature for that
	// public key.
	STTreasuryGenPubKeyHash

	// STTreasuryGenScriptHash identifies a script that is only valid when used
	// as part supported transactions in the staking system and generates utxos
	// from the treasury account.
	//
	// It imposes an encumbrance that requires a script that hashes to a
	// specific value along with all of the encumbrances that script itself
	// imposes.  The script is commonly referred to as a redeem script.
	STTreasuryGenScriptHash
)

func DetermineScriptType

func DetermineScriptType(scriptVersion uint16, script []byte) ScriptType

DetermineScriptType returns the type of the script passed.

NOTE: Version 0 scripts are the only currently supported version. It will always return STNonStandard for other script versions.

Similarly, STNonStandard is returned when the script does not parse.

Example

This example demonstrates determining the type of a script for a given scripting language version.

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/Decred-Next/dcrnd/txscript/version4/v8/stdscript"
)

// hexToBytes converts the passed hex string into bytes and will panic if there
// is an error.  This is only provided for the hard-coded constants so errors in
// the source code can be detected. It will only (and must only) be called with
// hard-coded values.
func hexToBytes(s string) []byte {
	b, err := hex.DecodeString(s)
	if err != nil {
		panic("invalid hex in source file: " + s)
	}
	return b
}

func main() {
	// Ordinarily the script version and script would be obtained from the
	// output of a transaction, but they are hard coded here for the purposes of
	// this example.
	const scriptVersion = 0
	script := hexToBytes("76a914e280cb6e66b96679aec288b1fbdbd4db08077a1b88ac")
	scriptType := stdscript.DetermineScriptType(scriptVersion, script)
	switch scriptType {
	case stdscript.STPubKeyHashEcdsaSecp256k1:
		fmt.Printf("standard version %d %v script\n", scriptVersion, scriptType)

	default:
		fmt.Printf("other script type: %v\n", scriptType)
	}

}
Output:

standard version 0 pubkeyhash script

func DetermineScriptTypeV0

func DetermineScriptTypeV0(script []byte) ScriptType

DetermineScriptTypeV0 returns the type of the passed version 0 script for the known standard types. This includes both types that are required by consensus as well as those which are not.

STNonStandard will be returned when the script does not parse.

func ExtractAddrs

func ExtractAddrs(scriptVersion uint16, pkScript []byte, params stdaddr.AddressParamsV0) (ScriptType, []stdaddr.Address)

ExtractAddrs analyzes the passed public key script and returns the associated script type along with any addresses associated with it when possible.

This function only works for standard script types and any data that fails to produce a valid address is omitted from the results. This means callers must not blindly assume the slice will be of a particular length for a given returned script type and should always check the length prior to access in case the addresses were not able to be created.

NOTE: Version 0 scripts are the only currently supported version. It will always return a nonstandard script type and no addresses for other script versions.

func ExtractAddrsV0

func ExtractAddrsV0(pkScript []byte, params stdaddr.AddressParamsV0) (ScriptType, []stdaddr.Address)

ExtractAddrsV0 analyzes the passed version 0 public key script and returns the associated script type along with any addresses associated with it when possible.

This function only works for standard script types and any data that fails to produce a valid address is omitted from the results. This means callers must not blindly assume the slice will be of a particular length for a given returned script type and should always check the length prior to access in case the addresses were not able to be created.

func (ScriptType) String

func (t ScriptType) String() string

String returns the ScriptType as a human-readable name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL