script

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LockTimeThreshold = 5e8 // Tue Nov 5 00:53:20 1985 UTC
)

constants

Variables

View Source
var (

	// script.go
	ErrScriptBound               = errors.New("Program counter out of script bound")
	ErrNoEnoughDataOPPUSHDATA1   = errors.New("OP_PUSHDATA1 has not enough data")
	ErrNoEnoughDataOPPUSHDATA2   = errors.New("OP_PUSHDATA2 has not enough data")
	ErrNoEnoughDataOPPUSHDATA4   = errors.New("OP_PUSHDATA4 has not enough data")
	ErrInvalidStackOperation     = errors.New("Invalid stack operation")
	ErrBadOpcode                 = errors.New("Bad opcode")
	ErrScriptEqualVerify         = errors.New("Equality verification failure")
	ErrScriptSignatureVerifyFail = errors.New("Signature verification failure")
	ErrInputIndexOutOfBound      = errors.New("input index out of bound")
	ErrAddressNotApplicable      = errors.New("Address not applicable")
	ErrOpReturn                  = errors.New("Encounter OP_RETURN")
	ErrInvalidSplitAddrScript    = errors.New("Invalid split address script")
	ErrScriptLockTimeVerifyFail  = errors.New("Check lock time verification failure")
	ErrInvalidContractScript     = errors.New("Invalid contract script")
	ErrInvalidContractParams     = errors.New("Invalid contract params")

	// stack.go
	ErrFinalStackEmpty       = errors.New("Final stack empty")
	ErrFinalTopStackEleFalse = errors.New("Final top stack element false")
	ErrCountNegative         = errors.New("Count is negative")
)

error

View Source
var (
	// TokenNameKey is the key for writing token name onchain
	TokenNameKey = []byte("Name")
	// TokenSymbolKey is the key for writing token symbol onchain
	TokenSymbolKey = []byte("Symbol")
	// TokenAmountKey is the key for writing token amount onchain
	TokenAmountKey = []byte("Amount")
	// TokenDecimalsKey is the key for writing token decimals onchain
	TokenDecimalsKey = []byte("Decimals")

	// TokenTxHashKey is the key for writing tx hash of token id onchain
	TokenTxHashKey = []byte("TokenTxHash")
	// TokenTxOutIdxKey is the key for writing tx output index of token id onchain
	TokenTxOutIdxKey = []byte("TokenTxOutIdx")
)
View Source
var (
	ZeroContractAddress = types.AddressHash{}
)

variables

Functions

func CalcTxHashForSig

func CalcTxHashForSig(
	scriptPubKey []byte, originalTx *types.Transaction, txInIdx int,
) (*crypto.HashType, error)

CalcTxHashForSig calculates the hash of a tx input, used for signature

func Validate

func Validate(scriptSig, scriptPubKey *Script, tx *types.Transaction, txInIdx int) error

Validate verifies the script

Types

type IssueParams

type IssueParams struct {
	Name        string
	Symbol      string
	TotalSupply uint64
	Decimals    uint8
}

IssueParams defines parameters for issuing ERC20-like tokens

type OpCode

type OpCode byte

OpCode enum

const (
	// push value
	OP0         OpCode = 0x00 // 0
	OPFALSE     OpCode = 0x00 // 0 - AKA OP0
	OPDATA20    OpCode = 0x14 // 20
	OPPUSHDATA1 OpCode = 0x4c // 76
	OPPUSHDATA2 OpCode = 0x4d // 77
	OPPUSHDATA4 OpCode = 0x4e // 78
	OP1NEGATE   OpCode = 0x4f // 79
	OPRESERVED  OpCode = 0x50 // 80
	OP1         OpCode = 0x51 // 81
	OPTRUE      OpCode = 0x51 // 81 - AKA OP1
	OP2         OpCode = 0x52 // 82
	OP3         OpCode = 0x53 // 83
	OP4         OpCode = 0x54 // 84
	OP5         OpCode = 0x55 // 85
	OP6         OpCode = 0x56 // 86
	OP7         OpCode = 0x57 // 87
	OP8         OpCode = 0x58 // 88
	OP9         OpCode = 0x59 // 89
	OP10        OpCode = 0x5a // 90
	OP11        OpCode = 0x5b // 91
	OP12        OpCode = 0x5c // 92
	OP13        OpCode = 0x5d // 93
	OP14        OpCode = 0x5e // 94
	OP15        OpCode = 0x5f // 95
	OP16        OpCode = 0x60 // 96

	// control
	OPNOP      OpCode = 0x61 // 97
	OPVER      OpCode = 0x62 // 98
	OPIF       OpCode = 0x63 // 99
	OPNOTIF    OpCode = 0x64 // 100
	OPVERIF    OpCode = 0x65 // 101
	OPVERNOTIF OpCode = 0x66 // 102
	OPELSE     OpCode = 0x67 // 103
	OPENDIF    OpCode = 0x68 // 104
	OPVERIFY   OpCode = 0x69 // 105
	OPRETURN   OpCode = 0x6a // 106

	// stack ops
	OPTOALTSTACK   OpCode = 0x6b // 107
	OPFROMALTSTACK OpCode = 0x6c // 108
	OP2DROP        OpCode = 0x6d // 109
	OP2DUP         OpCode = 0x6e // 110
	OP3DUP         OpCode = 0x6f // 111
	OP2OVER        OpCode = 0x70 // 112
	OP2ROT         OpCode = 0x71 // 113
	OP2SWAP        OpCode = 0x72 // 114
	OPIFDUP        OpCode = 0x73 // 115
	OPDEPTH        OpCode = 0x74 // 116
	OPDROP         OpCode = 0x75 // 117
	OPDUP          OpCode = 0x76 // 118
	OPNIP          OpCode = 0x77 // 119
	OPOVER         OpCode = 0x78 // 120
	OPPICK         OpCode = 0x79 // 121
	OPROLL         OpCode = 0x7a // 122
	OPROT          OpCode = 0x7b // 123
	OPSWAP         OpCode = 0x7c // 124
	OPTUCK         OpCode = 0x7d // 125

	// splice ops
	OPCAT    OpCode = 0x7e // 126
	OPSUBSTR OpCode = 0x7f // 127
	OPLEFT   OpCode = 0x80 // 128
	OPRIGHT  OpCode = 0x81 // 129
	OPSIZE   OpCode = 0x82 // 130

	// bit logic
	OPINVERT      OpCode = 0x83 // 131
	OPAND         OpCode = 0x84 // 132
	OPOR          OpCode = 0x85 // 133
	OPXOR         OpCode = 0x86 // 134
	OPEQUAL       OpCode = 0x87 // 135
	OPEQUALVERIFY OpCode = 0x88 // 136
	OPRESERVED1   OpCode = 0x89 // 137
	OPRESERVED2   OpCode = 0x8a // 138

	// numeric
	OP1ADD      OpCode = 0x8b // 139
	OP1SUB      OpCode = 0x8c // 140
	OP2MUL      OpCode = 0x8d // 141
	OP2DIV      OpCode = 0x8e // 142
	OPNEGATE    OpCode = 0x8f // 143
	OPABS       OpCode = 0x90 // 144
	OPNOT       OpCode = 0x91 // 145
	OP0NOTEQUAL OpCode = 0x92 // 146

	OPADD    OpCode = 0x93 // 147
	OPSUB    OpCode = 0x94 // 148
	OPMUL    OpCode = 0x95 // 149
	OPDIV    OpCode = 0x96 // 150
	OPMOD    OpCode = 0x97 // 151
	OPLSHIFT OpCode = 0x98 // 152
	OPRSHIFT OpCode = 0x99 // 153

	OPBOOLAND            OpCode = 0x9a // 154
	OPBOOLOR             OpCode = 0x9b // 155
	OPNUMEQUAL           OpCode = 0x9c // 156
	OPNUMEQUALVERIFY     OpCode = 0x9d // 157
	OPNUMNOTEQUAL        OpCode = 0x9e // 158
	OPLESSTHAN           OpCode = 0x9f // 159
	OPGREATERTHAN        OpCode = 0xa0 // 160
	OPLESSTHANOREQUAL    OpCode = 0xa1 // 161
	OPGREATERTHANOREQUAL OpCode = 0xa2 // 162
	OPMIN                OpCode = 0xa3 // 163
	OPMAX                OpCode = 0xa4 // 164

	OPWITHIN OpCode = 0xa5 // 165

	// crypto
	OPRIPEMD160           OpCode = 0xa6 // 166
	OPSHA1                OpCode = 0xa7 // 167
	OPSHA256              OpCode = 0xa8 // 168
	OPHASH160             OpCode = 0xa9 // 169
	OPHASH256             OpCode = 0xaa // 170
	OPCODESEPARATOR       OpCode = 0xab // 171
	OPCHECKSIG            OpCode = 0xac // 172
	OPCHECKSIGVERIFY      OpCode = 0xad // 173
	OPCHECKMULTISIG       OpCode = 0xae // 174
	OPCHECKMULTISIGVERIFY OpCode = 0xaf // 175
	OPCHECKLOCKTIMEVERIFY OpCode = 0xb0 // 176

	// vm
	OPCONTRACT OpCode = 0xb1 // 177
)

These constants are based on bitcoin official opcodes

type Operand

type Operand []byte

Operand represents stack operand when interpretting script

type Script

type Script []byte

Script represents scripts

func CreateSplitAddrScriptPrefix added in v0.3.0

func CreateSplitAddrScriptPrefix(addr types.Address) *Script

CreateSplitAddrScriptPrefix creates a script prefix for split address with a hashed address

func GasRefundSignatureScript added in v0.5.0

func GasRefundSignatureScript(nonce uint64) *Script

GasRefundSignatureScript returns a standard signature script for gas refound transaction.

func IssueTokenScript

func IssueTokenScript(pubKeyHash []byte, params *IssueParams) *Script

IssueTokenScript creates a script to issue tokens to the specified address.

func MakeContractScriptPubkey added in v0.5.0

func MakeContractScriptPubkey(
	from, to *types.AddressHash, code []byte, gasPrice, gasLimit, nonce uint64,
	version int32,
) (*Script, error)

MakeContractScriptPubkey makes a script pubkey for contract vout

func MakeContractScriptSig added in v0.5.0

func MakeContractScriptSig() *Script

MakeContractScriptSig makes a script sig for contract vin

func MakeContractUtxoScriptPubkey added in v0.5.0

func MakeContractUtxoScriptPubkey(from, to *types.AddressHash, nonce uint64, version int32) *Script

MakeContractUtxoScriptPubkey makes a script pubkey for contract addr utxo

func NewScript

func NewScript() *Script

NewScript returns an empty script

func NewScriptFromBytes

func NewScriptFromBytes(scriptBytes []byte) *Script

NewScriptFromBytes returns a script from byte slice

func NewScriptWithCap added in v0.5.0

func NewScriptWithCap(cap int) *Script

NewScriptWithCap returns an empty script

func PayToPubKeyHashCLTVScript added in v0.3.0

func PayToPubKeyHashCLTVScript(pubKeyHash []byte, blockTimeOrHeight int64) *Script

PayToPubKeyHashCLTVScript creates a script to lock a transaction output to the specified address till a specific time or block height.

func PayToPubKeyHashScript

func PayToPubKeyHashScript(pubKeyHash []byte) *Script

PayToPubKeyHashScript creates a script to lock a transaction output to the specified address.

func SignatureScript

func SignatureScript(sig *crypto.Signature, pubKey []byte) *Script

SignatureScript creates a script to unlock a utxo.

func SplitAddrScript added in v0.3.0

func SplitAddrScript(addrs []types.Address, weights []uint64) *Script

SplitAddrScript returns a script to store a split address output

func StandardCoinbaseSignatureScript

func StandardCoinbaseSignatureScript(height uint32) *Script

StandardCoinbaseSignatureScript returns a standard signature script for coinbase transaction.

func TransferTokenScript

func TransferTokenScript(pubKeyHash []byte, params *TransferParams) *Script

TransferTokenScript creates a script to transfer tokens to the specified address.

func (*Script) AddOpCode

func (s *Script) AddOpCode(opCode OpCode) *Script

AddOpCode adds an opcode to the script

func (*Script) AddOperand

func (s *Script) AddOperand(operand []byte) *Script

AddOperand adds an operand to the script

func (*Script) AddScript

func (s *Script) AddScript(script *Script) *Script

AddScript appends a script to the script

func (*Script) Disasm

func (s *Script) Disasm() string

Disasm disassembles script in human readable format. If the script fails to parse, the returned string will contain the disassembled script up to the failure point, appended by the string '[Error: error info]'

func (*Script) ExtractAddress

func (s *Script) ExtractAddress() (types.Address, error)

ExtractAddress returns address within the script

func (*Script) GetIssueParams

func (s *Script) GetIssueParams() (*IssueParams, error)

GetIssueParams returns token issue parameters embedded in the script

func (*Script) GetSplitAddrScriptPrefix added in v0.3.0

func (s *Script) GetSplitAddrScriptPrefix() *Script

GetSplitAddrScriptPrefix returns prefix of split addr script without and list of addresses and weights only called on split address script, so no need to check error

func (*Script) GetTransferParams

func (s *Script) GetTransferParams() (*TransferParams, error)

GetTransferParams returns token transfer parameters embedded in the script

func (*Script) IsContractPubkey added in v0.5.0

func (s *Script) IsContractPubkey() bool

IsContractPubkey returns true if the script pubkey contains OPCONTRACT code

func (*Script) IsContractSig added in v0.5.0

func (s *Script) IsContractSig() bool

IsContractSig returns true if the script sig contains OPCONTRACT code

func (*Script) IsPayToPubKeyHash

func (s *Script) IsPayToPubKeyHash() bool

IsPayToPubKeyHash returns if the script is p2pkh

func (*Script) IsPayToPubKeyHashCLTVScript added in v0.3.0

func (s *Script) IsPayToPubKeyHashCLTVScript() bool

IsPayToPubKeyHashCLTVScript returns if the script is p2pkhCLTV

func (*Script) IsPayToScriptHash

func (s *Script) IsPayToScriptHash() bool

IsPayToScriptHash returns if the script is p2sh

func (*Script) IsSplitAddrScript added in v0.3.0

func (s *Script) IsSplitAddrScript() bool

IsSplitAddrScript returns if the script is split address Note: assume OP_RETURN is only used for split address here. Add a magic number if OP_RETURN is used for something else

func (*Script) IsStandard added in v0.5.0

func (s *Script) IsStandard() bool

IsStandard returns if a script is standard Only certain types of transactions are allowed, i.e., regarded as standard

func (*Script) IsTokenIssue

func (s *Script) IsTokenIssue() bool

IsTokenIssue returns if the script is token issurance

func (*Script) IsTokenTransfer

func (s *Script) IsTokenTransfer() bool

IsTokenTransfer returns if the script is token issurance

func (*Script) P2PKHScriptPrefix

func (s *Script) P2PKHScriptPrefix() *Script

P2PKHScriptPrefix returns p2pkh prefix of token script

func (*Script) ParseContractAddr added in v0.5.0

func (s *Script) ParseContractAddr() (*types.AddressContract, error)

ParseContractAddr returns contract contract address within the script

func (*Script) ParseContractFrom added in v0.5.0

func (s *Script) ParseContractFrom() (*types.AddressPubKeyHash, error)

ParseContractFrom returns contract address within the script

func (*Script) ParseContractGas added in v0.5.0

func (s *Script) ParseContractGas() (uint64, error)

ParseContractGas returns address within the script

func (*Script) ParseContractGasPrice added in v0.5.0

func (s *Script) ParseContractGasPrice() (uint64, error)

ParseContractGasPrice returns address within the script

func (*Script) ParseContractNonce added in v0.5.0

func (s *Script) ParseContractNonce() (uint64, error)

ParseContractNonce returns address within the script

func (*Script) ParseContractParams added in v0.5.0

func (s *Script) ParseContractParams() (params *types.VMTxParams, typ types.ContractType, err error)

ParseContractParams parse script pubkey with OPCONTRACT to stack

func (*Script) ParseSplitAddrScript added in v0.3.0

func (s *Script) ParseSplitAddrScript() ([]types.Address, []uint64, error)

ParseSplitAddrScript returns [addr1, addr2, addr3, ...], [w1, w2, w3, ...] OP_RETURN <hash addr> [(addr1, w1), (addr2, w2), (addr3, w3), ...]

type Stack

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

Stack is used when interpretting script

type TokenID

type TokenID struct {
	// An outpoint uniquely identifies a token by its issurance tx and output index,
	// which contains all parameters of the token
	types.OutPoint
}

TokenID uniquely identifies a token, consisting of tx hash and output index

func NewTokenID

func NewTokenID(txHash crypto.HashType, txOutIdx uint32) TokenID

NewTokenID returns a new token ID

func (*TokenID) String added in v0.3.0

func (t *TokenID) String() string

type TransferParams

type TransferParams struct {
	TokenID
	Amount uint64
}

TransferParams defines parameters for transferring tokens

Jump to

Keyboard shortcuts

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