script

package
v1.2.28 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpFALSE               = 0
	OpPUSHDATA1           = 76
	OpPUSHDATA2           = 77
	OpPUSHDATA4           = 78
	Op1NEGATE             = 79
	OpRESERVED            = 80
	OpTRUE                = 81
	Op2                   = 82
	Op3                   = 83
	Op4                   = 84
	Op5                   = 85
	Op6                   = 86
	Op7                   = 87
	Op8                   = 88
	Op9                   = 89
	Op10                  = 90
	Op11                  = 91
	Op12                  = 92
	Op13                  = 93
	Op14                  = 94
	Op15                  = 95
	Op16                  = 96
	OpNOP                 = 97
	OpVER                 = 98
	OpIF                  = 99
	OpNOTIF               = 100
	OpVERIF               = 101
	OpVERNOTIF            = 102
	OpELSE                = 103
	OpENDIF               = 104
	OpVERIFY              = 105
	OpRETURN              = 106
	OpTOALTSTACK          = 107
	OpFROMALTSTACK        = 108
	Op2DROP               = 109
	Op2DUP                = 110
	Op3DUP                = 111
	Op2OVER               = 112
	Op2ROT                = 113
	Op2SWAP               = 114
	OpIFDUP               = 115
	OpDEPTH               = 116
	OpDROP                = 117
	OpDUP                 = 118
	OpNIP                 = 119
	OpOVER                = 120
	OpPICK                = 121
	OpROLL                = 122
	OpROT                 = 123
	OpSWAP                = 124
	OpTUCK                = 125
	OpCAT                 = 126
	OpSUBSTR              = 127
	OpLEFT                = 128
	OpRIGHT               = 129
	OpSIZE                = 130
	OpINVERT              = 131
	OpAND                 = 132
	OpOR                  = 133
	OpXOR                 = 134
	OpEQUAL               = 135
	OpEQUALVERIFY         = 136
	OpRESERVED1           = 137
	OpRESERVED2           = 138
	Op1ADD                = 139
	Op1SUB                = 140
	Op2MUL                = 141
	Op2DIV                = 142
	OpNEGATE              = 143
	OpABS                 = 144
	OpNOT                 = 145
	Op0NOTEQUAL           = 146
	OpADD                 = 147
	OpSUB                 = 148
	OpMUL                 = 149
	OpDIV                 = 150
	OpMOD                 = 151
	OpLSHIFT              = 152
	OpRSHIFT              = 153
	OpBOOLAND             = 154
	OpBOOLOR              = 155
	OpNUMEQUAL            = 156
	OpNUMEQUALVERIFY      = 157
	OpNUMNOTEQUAL         = 158
	OpLESSTHAN            = 159
	OpGREATERTHAN         = 160
	OpLESSTHANOREQUAL     = 161
	OpGREATERTHANOREQUAL  = 162
	OpMIN                 = 163
	OpMAX                 = 164
	OpWITHIN              = 165
	OpRIPEMD160           = 166
	OpSHA1                = 167
	OpSHA256              = 168
	OpHASH160             = 169
	OpHASH256             = 170
	OpCODESEPARATOR       = 171
	OpCHECKSIG            = 172
	OpCHECKSIGVERIFY      = 173
	OpCHECKMULTISIG       = 174
	OpCHECKMULTISIGVERIFY = 175
	OpNOP1                = 176
	OpCHECKLOCKTIMEVERIFY = 177
	OpCHECKSEQUENCEVERIFY = 178
	OpNOP4                = 179
	OpNOP5                = 180
	OpNOP6                = 181
	OpNOP7                = 182
	OpNOP8                = 183
	OpNOP9                = 184
	OpNOP10               = 185
	OpPUBKEYHASH          = 253
	OpPUBKEY              = 254
	OpINVALIDOPCODE       = 255
)

Standard Bitcoin script opcodes

View Source
const (
	RcOK = iota
	RcErr
	RcExceeds
	RcParseError
	RcScriptError
	RcLengthMismatch
	RcEmptyStack
	RcInvalidFinalStack
	RcNotImplemented
	RcInvalidOpcode
	RcReservedOpcode
	RcTxInvalid
	RcTypeMismatch
	RcInvalidStackType
	RcExceedsStack
	RcNoTransaction
	RcUnclosedIf
	RcDoubleElse
	RcInvalidTransaction
	RcInvalidPubkey
	RcInvalidUint
	RcInvalidSignature
	RcInvalidTransfer
	RcNotVerified
	RcDisabledOpcode
	RcTxNotSignable
	RcEmptyScript
	RcDone
)

Result codes returned by script functions.

Variables

View Source
var (
	// OpCodes is a list of all valid opcodes in a Bitcoin script.
	OpCodes = []*OpCode{}/* 114 elements not displayed */

)
View Source
var (
	RcString = []string{
		"OK",
		"Generic error",
		"Operation exceeds available data",
		"Parse error",
		"Script error",
		"Length mismatch",
		"Empty stack",
		"Invalid final stack",
		"Not implemented yet",
		"Invalid opcode",
		"Reserved opcode",
		"Invalid transaction",
		"Type mismatch",
		"Invalid stack type",
		"Operation exceeds stack",
		"No transaction available",
		"Unclosed IF",
		"Double ELSE",
		"Invalid transaction",
		"Invalid pubkey",
		"Invalid Uint",
		"Invalid signature",
		"Invalid transfer",
		"Not verified",
		"Disabled opcode",
		"Transaction not signable",
		"Empty script",
		"Script done",
	}
)

Human-readable result codes

Functions

This section is empty.

Types

type OpCode

type OpCode struct {
	// Name is the mnemonic name of the opcode.
	Name string
	// Short name
	Short string
	// Value is the byte code of the opcode.
	Value byte
	// Exec is function that performs the stack operations for the opcode.
	Exec func(r *R) int
}

OpCode describes a Bitcoin script opcode with a symbolic name and a value.

func GetOpcode

func GetOpcode(v byte) *OpCode

GetOpcode returns a opcode for a given byte value.

func GetOpcodeTR added in v1.2.23

func GetOpcodeTR(v byte) *OpCode

GetOpcodeTR returns a opcode for a given byte value in a tapscript.

type R

type R struct {
	CbStep func(stack *Stack, stmt *Statement, rc int)
	// contains filtered or unexported fields
}

R is the Bitcoin script runtime environment

func NewRuntime

func NewRuntime(tx *Tx) *R

NewRuntime creates a new script parser and execution runtime.

func (*R) CheckMultiSig

func (r *R) CheckMultiSig() (bool, int)

CheckMultiSig performs a OpCHECKMULTISIG operation on the stack (without pushing a result onto the stack).

func (*R) CheckSig

func (r *R) CheckSig() (bool, int)

CheckSig performs a OpCHECKSIG operation on the stack (without pushing a result onto the stack)

func (*R) ExecScript

func (r *R) ExecScript(script *Script) (bool, int)

ExecScript executes a script belonging to a transaction. If no transaction is specified, some script opcodes like OpCHECKSIG could not be executed. N.B.: To successfully execute 'script' that involves OpCHECKSIG it needs to be assembled (concatenated) and cleaned up from the prev.sigScript and curr.pkScript (see https://en.bitcoin.it/wiki/OpCHECKSIG); 'tx' is the current transaction in dissected format already prepared for signature.

type Script

type Script struct {
	Stmts []*Statement
}

Script is an ordered list of statements.

func Compile

func Compile(src string) (*Script, error)

Compile compiles a source code into a Bitcoin script.

func NewScript

func NewScript() *Script

NewScript creates a new (empty) script.

func Parse

func Parse(hexScript string) (scr *Script, rc int)

Parse dissects binary scripts into a sequence of statements that constitutes a script.

func ParseBin

func ParseBin(code []byte) (scr *Script, rc int)

ParseBin dissects binary scripts into a sequence of statements that constitutes a script.

func (*Script) Add

func (s *Script) Add(stmt *Statement)

Add a statement at the end of the script.

func (*Script) Bytes

func (s *Script) Bytes() []byte

Bytes returns a (flat) binary representation of the script

func (*Script) Decompile

func (s *Script) Decompile() string

Decompile returns a human-readable Bitcoin script source from a binary script representation.

func (*Script) GetTemplate

func (s *Script) GetTemplate() (tpl []byte, rc int)

GetTemplate returns a template derived from a script. A template only contains a sequence of opcodes; it is used to find structural equivalent scripts (but with varying data).

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack represents the FIFO stack used during the processing of a script. Objects on the stack are of type math.Int; byte arrays and intrinsic integers are converted in both way when necessary.

func NewStack

func NewStack() *Stack

NewStack creates a new empty stack.

func (*Stack) Compare

func (s *Stack) Compare() (a, b *math.Int, cmp int, rc int)

Compare compares the two top-level elements of the stack as integers.

func (*Stack) Dup

func (s *Stack) Dup(n int) int

Dup duplicates the top n.th elements of the stack.

func (*Stack) Len

func (s *Stack) Len() int

Len returns the length of the stack

func (*Stack) Peek

func (s *Stack) Peek() (*math.Int, int)

Peek looks up the the top-level object on the stack without removing it.

func (*Stack) PeekAt

func (s *Stack) PeekAt(i int) (*math.Int, int)

PeekAt looks up the object at depth 'i' of the stack (top-level is depth 0) without removing it.

func (*Stack) Pop

func (s *Stack) Pop() (*math.Int, int)

Pop removes the top-level element from the stack and returns it.

func (*Stack) Push

func (s *Stack) Push(v interface{}) int

Push an object onto the stack. Objects can be of type int, []byte, *math.Int or bool; other types return a result code 'RcInvalidStackType'.

func (*Stack) RemoveAt

func (s *Stack) RemoveAt(i int) (*math.Int, int)

RemoveAt removes the element at depth 'i' from the stack (top-level is depth 0) and returns it.

func (*Stack) Values

func (s *Stack) Values() []*math.Int

Values returns the stack content

type Statement

type Statement struct {
	Opcode byte
	Data   []byte
}

Statement is a single script statement.

func NewDataStatement

func NewDataStatement(data []byte) *Statement

NewDataStatement creates a data statement.

func NewStatement

func NewStatement(op byte) *Statement

NewStatement creates a statement with an opcode.

func (*Statement) String

func (s *Statement) String() string

String returns the string representation of a statement.

type Tx added in v1.2.23

type Tx struct {
	SignedData []byte
	LockTime   uint64
	Sequence   uint64
	Version    int
}

Jump to

Keyboard shortcuts

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