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 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
- type DefaultOpcodeParser
- type Engine
- type ExecutionOptionFunc
- func WithAfterGenesis() ExecutionOptionFunc
- func WithFlags(flags scriptflag.Flag) ExecutionOptionFunc
- func WithForkID() ExecutionOptionFunc
- func WithP2SH() ExecutionOptionFunc
- func WithScripts(lockingScript *bscript.Script, unlockingScript *bscript.Script) ExecutionOptionFunc
- func WithTx(tx *bt.Tx, inputIdx int, prevOutput *bt.Output) ExecutionOptionFunc
- type OpcodeParser
- type ParsedOp
- type ParsedScript
Constants ¶
const ( MaxOpsBeforeGenesis = 500 MaxStackSizeBeforeGenesis = 1000 MaxScriptSizeBeforeGenesis = 10000 MaxScriptElementSizeBeforeGenesis = 520 MaxScriptNumberLengthBeforeGenesis = 4 MaxPubKeysPerMultiSigBeforeGenesis = 20 )
Limits applied to transactions before genesis
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 ¶
This section is empty.
Types ¶
type DefaultOpcodeParser ¶
type DefaultOpcodeParser struct {
ErrorOnCheckSig bool
}
DefaultOpcodeParser is a standard parser which can be used from zero value.
func (*DefaultOpcodeParser) Parse ¶
func (p *DefaultOpcodeParser) Parse(s *bscript.Script) (ParsedScript, error)
Parse takes a *bscript.Script and returns a []interpreter.ParsedOp
func (*DefaultOpcodeParser) Unparse ¶
func (p *DefaultOpcodeParser) Unparse(pscr ParsedScript) (*bscript.Script, error)
Unparse reverses the action of Parse and returns the ParsedScript as a *bscript.Script
type Engine ¶
type Engine interface {
Execute(opts ...ExecutionOptionFunc) error
}
Engine is the virtual machine that executes scripts.
type ExecutionOptionFunc ¶
type ExecutionOptionFunc func(p *execOpts)
ExecutionOptionFunc for setting execution options.
func WithAfterGenesis ¶
func WithAfterGenesis() ExecutionOptionFunc
WithAfterGenesis configure the execution to operate in an after-genesis context.
func WithFlags ¶
func WithFlags(flags scriptflag.Flag) ExecutionOptionFunc
WithFlags configure the execution with the provided flags.
func WithForkID ¶
func WithForkID() ExecutionOptionFunc
WithForkID configure the execution to allow a tx with a fork id.
func WithP2SH ¶
func WithP2SH() ExecutionOptionFunc
WithP2SH configure the execution to allow a P2SH output.
func WithScripts ¶
func WithScripts(lockingScript *bscript.Script, unlockingScript *bscript.Script) ExecutionOptionFunc
WithScripts configure the execution to run again a set of *bscript.Script.
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
func (*ParsedOp) RequiresTx ¶
RequiresTx returns true if the op is checksig
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