interpreter

package
v2.0.0-beta.10 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: ISC Imports: 16 Imported by: 3

README

interpreter

ISC License GoDoc

Package interpreter implements the an interpreter for the bitcoin transaction language. There is a comprehensive test suite.

This package has intentionally been designed so it can be used as a standalone package for any projects needing to use or validate bitcoin transaction scripts.

Bitcoin Scripts

Bitcoin provides a stack-based, FORTH-like language for the scripts in the bitcoin transactions. This language is not turing complete although it is still fairly powerful. A description of the language can be found at https://wiki.bitcoinsv.io/index.php/Script

Installation and Updating

$ go get -u github.com/libsv/go-bt/bscript/interpreter

Examples

  • Standard Pay-to-pubkey-hash Script
    Demonstrates creating a script which pays to a bitcoin address. It also prints the created script hex and uses the DisasmString function to display the disassembled script.

License

Package interpreter is licensed under the copyfree ISC License.

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

View Source
const (
	MaxOpsBeforeGenesis                = 500
	MaxStackSizeBeforeGenesis          = 1000
	MaxScriptSizeBeforeGenesis         = 10000
	MaxScriptElementSizeBeforeGenesis  = 520
	MaxScriptNumberLengthBeforeGenesis = 4
	MaxPubKeysPerMultiSigBeforeGenesis = 20
)

Limits applied to transactions before genesis

View Source
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

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.

func NewEngine

func NewEngine() Engine

NewEngine returns a new script engine for the provided locking script (of a previous transaction out), transaction, and input index. The flags modify the behaviour of the script engine according to the description provided by each flag.

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.

func WithTx

func WithTx(tx *bt.Tx, inputIdx int, prevOutput *bt.Output) ExecutionOptionFunc

WithTx configure the execution to run again a tx.

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

func (o *ParsedOp) AlwaysIllegal() bool

AlwaysIllegal returns true if the op is always illegal

func (*ParsedOp) EnforceMinimumDataPush

func (o *ParsedOp) EnforceMinimumDataPush() error

EnforceMinimumDataPush checks that the op is pushing only the needed amount of data. Errs if not the case.

func (*ParsedOp) IsConditional

func (o *ParsedOp) IsConditional() bool

IsConditional returns true if the op is a conditional

func (*ParsedOp) IsDisabled

func (o *ParsedOp) IsDisabled() bool

IsDisabled returns true if the op is disabled

func (*ParsedOp) Name

func (o *ParsedOp) Name() string

Name returns the human readable name for the current opcode

func (*ParsedOp) RequiresTx

func (o *ParsedOp) RequiresTx() bool

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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