Documentation ¶
Overview ¶
Package interpreter implements the bitcoin transaction script language.
A complete description of the script language used by bitcoin can be found at https://en.bitcoin.it/wiki/Script. The following only serves as a quick overview to provide information on how to use the package.
This package provides data structures and functions to parse and execute bitcoin transaction scripts.
Script Overview ¶
Bitcoin transaction scripts are written in a stack-base, FORTH-like language.
The bitcoin script language consists of a number of opcodes which fall into several categories such pushing and popping data to and from the stack, performing basic and bitwise arithmetic, conditional branching, comparing hashes, and checking cryptographic signatures. Scripts are processed from left to right and intentionally do not provide loops.
The vast majority of Bitcoin scripts at the time of this writing are of several standard forms which consist of a spender providing a public key and a signature which proves the spender owns the associated private key. This information is used to prove the the spender is authorized to perform the transaction.
One benefit of using a scripting language is added flexibility in specifying what conditions must be met in order to spend bitcoins.
Errors ¶
Errors returned by this package are of type interpreter.Error. This allows the caller to programmatically determine the specific error by examining the ErrorCode field of the type asserted interpreter.Error while still providing rich error messages with contextual information. A convenience function named IsErrorCode is also provided to allow callers to easily check for a specific error code. See ErrorCode in the package documentation for a full list.
Index ¶
Constants ¶
const ( MaxOpsBeforeGenesis = 500 MaxStackSizeBeforeGenesis = 1000 MaxScriptSizeBeforeGenesis = 10000 MaxScriptElementSizeBeforeGenesis = 520 MaxPubKeysPerMultiSigBeforeGenesis = 20 )
Limits applied to transactions before genesis
const ( OpCondFalse = 0 OpCondTrue = 1 OpCondSkip = 2 )
Conditional execution constants.
const ( // LockTimeThreshold is the number below which a lock time is // interpreted to be a block number. Since an average of one block // is generated per 10 minutes, this allows blocks for about 9,512 // years. LockTimeThreshold = 5e8 // Tue Nov 5 00:53:20 1985 UTC )
Variables ¶
This section is empty.
Functions ¶
func IsErrorCode ¶
IsErrorCode returns whether or not the provided error is a script error with the provided error code.
Types ¶
type Engine ¶
type Engine interface {
Execute(ExecutionParams) error
}
Engine is the virtual machine that executes scripts.
type Error ¶
Error identifies a script-related error. It is used to indicate three classes of errors:
- Script execution failures due to violating one of the many requirements imposed by the script engine or evaluating to false
- Improper API usage by callers
- Internal consistency check failures
The caller can use type assertions on the returned errors to access the ErrorCode field to ascertain the specific reason for the error. As an additional convenience, the caller may make use of the IsErrorCode function to check for a specific error code.
func NewErrMinimalDataPush ¶
NewErrMinimalDataPush returns an Error with code ErrMinimalData
type ErrorCode ¶
type ErrorCode int
ErrorCode identifies a kind of script error.
const ( // ErrInternal is returned if internal consistency checks fail. In // practice this error should never be seen as it would mean there is an // error in the engine logic. ErrInternal ErrorCode = iota // ErrOK represents successful execution. It should be treated similar to that // ok io.EOF ErrOK // ErrInvalidFlags is returned when the passed flags to NewEngine // contain an invalid combination. ErrInvalidFlags // ErrInvalidIndex is returned when an out-of-bounds index is passed to // a function. ErrInvalidIndex // ErrUnsupportedAddress is returned when a concrete type that // implements a bsvutil.Address is not a supported type. ErrUnsupportedAddress // ErrNotMultisigScript is returned from CalcMultiSigStats when the // provided script is not a multisig script. ErrNotMultisigScript // ErrTooManyRequiredSigs is returned from MultiSigScript when the // specified number of required signatures is larger than the number of // provided public keys. ErrTooManyRequiredSigs // ErrTooMuchNullData is returned from NullDataScript when the length of // the provided data exceeds MaxDataCarrierSize. ErrTooMuchNullData // ErrEarlyReturn is returned when OP_RETURN is executed in the script. ErrEarlyReturn // ErrEmptyStack is returned when the script evaluated without error, // but terminated with an empty top stack element. ErrEmptyStack // ErrEvalFalse is returned when the script evaluated without error but // terminated with a false top stack element. ErrEvalFalse // ErrScriptUnfinished is returned when CheckErrorCondition is called on // a script that has not finished executing. ErrScriptUnfinished // ErrScriptDone is returned when an attempt to execute an opcode is // made once all of them have already been executed. This can happen // due to things such as a second call to Execute or calling Step after // all opcodes have already been executed. ErrInvalidProgramCounter // ErrScriptTooBig is returned if a script is larger than MaxScriptSize. ErrScriptTooBig // ErrElementTooBig is returned if the size of an element to be pushed // to the stack is over MaxScriptElementSize. ErrElementTooBig // ErrTooManyOperations is returned if a script has more than // MaxOpsPerScript opcodes that do not push data. ErrTooManyOperations // ErrStackOverflow is returned when stack and altstack combined depth // is over the limit. ErrStackOverflow // ErrInvalidPubKeyCount is returned when the number of public keys // specified for a multsig is either negative or greater than // MaxPubKeysPerMultiSig. ErrInvalidPubKeyCount // ErrInvalidSignatureCount is returned when the number of signatures // specified for a multisig is either negative or greater than the // number of public keys. ErrInvalidSignatureCount // ErrNumberTooBig is returned when the argument for an opcode that // expects numeric input is larger than the expected maximum number of // bytes. For the most part, opcodes that deal with stack manipulation // via offsets, arithmetic, numeric comparison, and boolean logic are // those that this applies to. However, any opcode that expects numeric // input may fail with this code. ErrNumberTooBig // ErrNumberTooSmall is returned when the argument for an opcode that // expects numeric input is smaller than the expected maximum number of // bytes. For the most part, opcodes that deal with stack manipulation // via offsets, arithmetic, numeric comparison, and boolean logic are // those that this applies to. However, any opcode that expects numeric // input may fail with this code. ErrNumberTooSmall // ErrDivideByZero is returned when OP_DIV is invoked to divide a number // by zero ErrDivideByZero // ErrVerify is returned when OP_VERIFY is encountered in a script and // the top item on the data stack does not evaluate to true. ErrVerify // ErrEqualVerify is returned when OP_EQUALVERIFY is encountered in a // script and the top item on the data stack does not evaluate to true. ErrEqualVerify // ErrNumEqualVerify is returned when OP_NUMEQUALVERIFY is encountered // in a script and the top item on the data stack does not evaluate to // true. ErrNumEqualVerify // ErrCheckSigVerify is returned when OP_CHECKSIGVERIFY is encountered // in a script and the top item on the data stack does not evaluate to // true. ErrCheckSigVerify // ErrCheckSigVerify is returned when OP_CHECKMULTISIGVERIFY is // encountered in a script and the top item on the data stack does not // evaluate to true. ErrCheckMultiSigVerify // ErrDisabledOpcode is returned when a disabled opcode is encountered // in a script. ErrDisabledOpcode // ErrReservedOpcode is returned when an opcode marked as reserved // is encountered in a script. ErrReservedOpcode // ErrMalformedPush is returned when a data push opcode tries to push // more bytes than are left in the script. ErrMalformedPush // ErrInvalidStackOperation is returned when a stack operation is // attempted with a number that is invalid for the current stack size. ErrInvalidStackOperation // ErrUnbalancedConditional is returned when an OP_ELSE or OP_ENDIF is // encountered in a script without first having an OP_IF or OP_NOTIF or // the end of script is reached without encountering an OP_ENDIF when // an OP_IF or OP_NOTIF was previously encountered. ErrUnbalancedConditional // ErrInvalidInputLength is returned when an input to an opcode is not // the correct length as required by that opcode. ErrInvalidInputLength // ErrMinimalData is returned when the ScriptVerifyMinimalData flag // is set and the script contains push operations that do not use // the minimal opcode required. ErrMinimalData // ErrMinimalIf is returned when the ScriptVerifyMinimalIf flag // is set and the script contains if operations that do not use // the minimal opcode required. ErrMinimalIf // ErrInvalidSigHashType is returned when a signature hash type is not // one of the supported types. ErrInvalidSigHashType // ErrSigTooShort is returned when a signature that should be a // canonically-encoded DER signature is too short. ErrSigTooShort // ErrSigTooLong is returned when a signature that should be a // canonically-encoded DER signature is too long. ErrSigTooLong // ErrSigInvalidSeqID is returned when a signature that should be a // canonically-encoded DER signature does not have the expected ASN.1 // sequence ID. ErrSigInvalidSeqID // ErrSigInvalidDataLen is returned a signature that should be a // canonically-encoded DER signature does not specify the correct number // of remaining bytes for the R and S portions. ErrSigInvalidDataLen // ErrSigMissingSTypeID is returned a signature that should be a // canonically-encoded DER signature does not provide the ASN.1 type ID // for S. ErrSigMissingSTypeID // ErrSigMissingSLen is returned when a signature that should be a // canonically-encoded DER signature does not provide the length of S. ErrSigMissingSLen // ErrSigInvalidSLen is returned a signature that should be a // canonically-encoded DER signature does not specify the correct number // of bytes for the S portion. ErrSigInvalidSLen // ErrSigInvalidRIntID is returned when a signature that should be a // canonically-encoded DER signature does not have the expected ASN.1 // integer ID for R. ErrSigInvalidRIntID // ErrSigZeroRLen is returned when a signature that should be a // canonically-encoded DER signature has an R length of zero. ErrSigZeroRLen // ErrSigNegativeR is returned when a signature that should be a // canonically-encoded DER signature has a negative value for R. ErrSigNegativeR // ErrSigTooMuchRPadding is returned when a signature that should be a // canonically-encoded DER signature has too much padding for R. ErrSigTooMuchRPadding // ErrSigInvalidSIntID is returned when a signature that should be a // canonically-encoded DER signature does not have the expected ASN.1 // integer ID for S. ErrSigInvalidSIntID // ErrSigZeroSLen is returned when a signature that should be a // canonically-encoded DER signature has an S length of zero. ErrSigZeroSLen // ErrSigNegativeS is returned when a signature that should be a // canonically-encoded DER signature has a negative value for S. ErrSigNegativeS // ErrSigTooMuchSPadding is returned when a signature that should be a // canonically-encoded DER signature has too much padding for S. ErrSigTooMuchSPadding // ErrSigHighS is returned when the ScriptVerifyLowS flag is set and the // script contains any signatures whose S values are higher than the // half order. ErrSigHighS // ErrNotPushOnly is returned when a script that is required to only // push data to the stack performs other operations. A couple of cases // where this applies is for a pay-to-script-hash signature script when // bip16 is active and when the ScriptVerifySigPushOnly flag is set. ErrNotPushOnly // ErrSigNullDummy is returned when the ScriptStrictMultiSig flag is set // and a multisig script has anything other than 0 for the extra dummy // argument. ErrSigNullDummy // ErrPubKeyType is returned when the ScriptVerifyStrictEncoding // flag is set and the script contains invalid public keys. ErrPubKeyType // ErrCleanStack is returned when the ScriptVerifyCleanStack flag // is set, and after evalution, the stack does not contain only a // single element. ErrCleanStack // ErrNullFail is returned when the ScriptVerifyNullFail flag is // set and signatures are not empty on failed checksig or checkmultisig // operations. ErrNullFail // ErrDiscourageUpgradableNOPs is returned when the // ScriptDiscourageUpgradableNops flag is set and a NOP opcode is // encountered in a script. ErrDiscourageUpgradableNOPs // ErrNegativeLockTime is returned when a script contains an opcode that // interprets a negative lock time. ErrNegativeLockTime // ErrUnsatisfiedLockTime is returned when a script contains an opcode // that involves a lock time and the required lock time has not been // reached. ErrUnsatisfiedLockTime // ErrIllegalForkID is returned when either the ScriptEnableSighashForkID flag is set, but // the transaction doesn't have a ForkID sighash flag, or when the transaction does have the ForkID // set, but the ScriptEnableSighashForkID flag is not set. ErrIllegalForkID )
These constants are used to identify a specific Error.
type ExecutionParams ¶
type ExecutionParams struct { PreviousTxOut *bt.Output Tx *bt.Tx InputIdx int Flags ScriptFlags }
ExecutionParams are the params required for building an Engine
type OpcodeParser ¶
type OpcodeParser interface { Parse(*bscript.Script) (ParsedScript, error) Unparse(ParsedScript) (*bscript.Script, error) }
OpcodeParser parses *bscript.Script into a ParsedScript, and unparsing back
type ParsedOp ¶
type ParsedOp struct { Op opcode Data []byte }
ParsedOp is a parsed opcode
func (*ParsedOp) AlwaysIllegal ¶
AlwaysIllegal returns true if the op is always illegal
func (*ParsedOp) EnforceMinimumDataPush ¶
EnforceMinimumDataPush checks that the op is pushing only the needed amount of data. Errs if not the case.
func (*ParsedOp) IsConditional ¶
IsConditional returns true if the op is a conditional
func (*ParsedOp) IsDisabled ¶
IsDisabled returns true if the op is disabled
type ParsedScript ¶
type ParsedScript []ParsedOp
ParsedScript is a slice of ParsedOp
func (ParsedScript) IsPushOnly ¶
func (p ParsedScript) IsPushOnly() bool
IsPushOnly returns true if the ParsedScript only contains push commands
type ScriptFlags ¶
type ScriptFlags uint32
ScriptFlags is a bitmask defining additional operations or tests that will be done when executing a script pair.
const ( // ScriptBip16 defines whether the bip16 threshold has passed and thus // pay-to-script hash transactions will be fully validated. ScriptBip16 ScriptFlags = 1 << iota // ScriptStrictMultiSig defines whether to verify the stack item // used by CHECKMULTISIG is zero length. ScriptStrictMultiSig // ScriptDiscourageUpgradableNops defines whether to verify that // NOP1 through NOP10 are reserved for future soft-fork upgrades. This // flag must not be used for consensus critical code nor applied to // blocks as this flag is only for stricter standard transaction // checks. This flag is only applied when the above opcodes are // executed. ScriptDiscourageUpgradableNops // ScriptVerifyCheckLockTimeVerify defines whether to verify that // a transaction output is spendable based on the locktime. // This is BIP0065. ScriptVerifyCheckLockTimeVerify // ScriptVerifyCheckSequenceVerify defines whether to allow execution // pathways of a script to be restricted based on the age of the output // being spent. This is BIP0112. ScriptVerifyCheckSequenceVerify // ScriptVerifyCleanStack defines that the stack must contain only // one stack element after evaluation and that the element must be // true if interpreted as a boolean. This is rule 6 of BIP0062. // This flag should never be used without the ScriptBip16 flag. ScriptVerifyCleanStack // ScriptVerifyDERSignatures defines that signatures are required // to compily with the DER format. ScriptVerifyDERSignatures // ScriptVerifyLowS defines that signtures are required to comply with // the DER format and whose S value is <= order / 2. This is rule 5 // of BIP0062. ScriptVerifyLowS // ScriptVerifyMinimalData defines that signatures must use the smallest // push operator. This is both rules 3 and 4 of BIP0062. ScriptVerifyMinimalData // ScriptVerifyNullFail defines that signatures must be empty if // a CHECKSIG or CHECKMULTISIG operation fails. ScriptVerifyNullFail // ScriptVerifySigPushOnly defines that signature scripts must contain // only pushed data. This is rule 2 of BIP0062. ScriptVerifySigPushOnly // ScriptEnableSighashForkID defined that signature scripts have forkid // enabled. ScriptEnableSighashForkID // ScriptVerifyStrictEncoding defines that signature scripts and // public keys must follow the strict encoding requirements. ScriptVerifyStrictEncoding // ScriptVerifyBip143SigHash defines that signature hashes should // be calculated using the bip0143 signature hashing algorithm. ScriptVerifyBip143SigHash // ScriptUTXOAfterGenesis defines that the utxo was created after // genesis. ScriptUTXOAfterGenesis // ScriptVerifyMinimalIf defines the enforcement of any conditional statement using the // minimum required data. ScriptVerifyMinimalIf )
func (*ScriptFlags) AddFlag ¶
func (s *ScriptFlags) AddFlag(flag ScriptFlags)
AddFlag adds the passed flag to ScriptFlags
func (ScriptFlags) HasFlag ¶
func (s ScriptFlags) HasFlag(flag ScriptFlags) bool
HasFlag returns whether the ScriptFlags has the passed flag set.