vmutil

package
v4.8.11+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2020 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadValue       = errors.New("bad value")
	ErrMultisigFormat = errors.New("bad multisig program format")
)

pre-define errors

View Source
var ErrUnresolvedJump = errors.New("unresolved jump target")

Functions

func DefaultCoinbaseProgram

func DefaultCoinbaseProgram() ([]byte, error)

DefaultCoinbaseProgram generates the script for contorl coinbase output

func IsUnspendable

func IsUnspendable(prog []byte) bool

IsUnspendable checks if a contorl program is absolute failed

func P2MCProgram added in v1.0.5

func P2MCProgram(magneticContractArgs MagneticContractArgs) ([]byte, error)

P2MCProgram generates the script for control with magnetic contract

MagneticContract source code: contract MagneticContract(requestedAsset: Asset,

                          ratioNumerator: Integer,
                          ratioDenominator: Integer,
                          sellerProgram: Program,
                          standardProgram: Program,
                          sellerKey: PublicKey) locks valueAmount of valueAsset {
 clause partialTrade(exchangeAmount: Amount) {
  define actualAmount: Integer = exchangeAmount * ratioDenominator / ratioNumerator
  verify actualAmount >= 0 && actualAmount < valueAmount
  define receiveAmount: Integer = exchangeAmount * 999 / 1000
  lock receiveAmount of requestedAsset with sellerProgram
  lock valueAmount-actualAmount of valueAsset with standardProgram
  unlock actualAmount of valueAsset
 }
 clause fullTrade() {
  define requestedAmount: Integer = valueAmount * ratioNumerator / ratioDenominator
  define requestedAmount: Integer = requestedAmount * 999 / 1000
  verify requestedAmount >= 0
  lock requestedAmount of requestedAsset with sellerProgram
  unlock valueAmount of valueAsset
 }
 clause cancel(sellerSig: Signature) {
  verify checkTxSig(sellerKey, sellerSig)
  unlock valueAmount of valueAsset
 }
}

contract stack flow: 7 [... <position> <clause selector> sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset 7] ROLL [... <clause selector> sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset <position>] TOALTSTACK [... <clause selector> sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset] 6 [... <clause selector> sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset 6] ROLL [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset <clause selector>] DUP [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset <clause selector> <clause selector>] 2 [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset <clause selector> <clause selector> 2] NUMEQUAL [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset <clause selector> (<clause selector> == 2)] JUMPIF:$cancel [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset <clause selector>] JUMPIF:$fullTrade [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset] $partialTrade [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset] 6 [... exchangeAmount sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset 6] PICK [... exchangeAmount sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset exchangeAmount] 3 [... exchangeAmount sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset exchangeAmount 3] ROLL [... exchangeAmount sellerKey standardProgram sellerProgram ratioNumerator requestedAsset exchangeAmount ratioDenominator] 3 [... exchangeAmount sellerKey standardProgram sellerProgram ratioNumerator requestedAsset exchangeAmount ratioDenominator 3] ROLL [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset exchangeAmount ratioDenominator ratioNumerator] MULFRACTION [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount] AMOUNT [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount] OVER [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount actualAmount] 0 [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount actualAmount 0] GREATERTHANOREQUAL [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount (actualAmount > 0)] 2 [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount (actualAmount > 0) 2] PICK [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount (actualAmount > 0) actualAmount] 2 [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount valueAmount (actualAmount > 0) actualAmount 2] ROLL [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount (actualAmount > 0) actualAmount valueAmount] LESSTHAN [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount (actualAmount > 0) (actualAmount < valueAmount)] BOOLAND [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount ((actualAmount > 0) && (actualAmount < valueAmount))] VERIFY [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount] FROMALTSTACK [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount <position>] DUP [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount <position> <position>] TOALTSTACK [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount <position>] 6 [... exchangeAmount sellerKey standardProgram sellerProgram requestedAsset actualAmount <position> 6] ROLL [... sellerKey standardProgram sellerProgram requestedAsset actualAmount <position> exchangeAmount] 999 [... sellerKey standardProgram sellerProgram requestedAsset actualAmount <position> exchangeAmount 999] 1000 [... sellerKey standardProgram sellerProgram requestedAsset actualAmount <position> exchangeAmount 1000] MULFRACTION [... sellerKey standardProgram sellerProgram requestedAsset actualAmount <position> receiveAmount] 3 [... sellerKey standardProgram sellerProgram requestedAsset actualAmount <position> receiveAmount 3] ROLL [... sellerKey standardProgram sellerProgram actualAmount <position> receiveAmount requestedAsset] 1 [... sellerKey standardProgram sellerProgram actualAmount <position> receiveAmount requestedAsset 1] 5 [... sellerKey standardProgram sellerProgram actualAmount <position> receiveAmount requestedAsset 1 5] ROLL [... sellerKey standardProgram actualAmount <position> receiveAmount requestedAsset 1 sellerProgram] CHECKOUTPUT [... sellerKey standardProgram actualAmount checkOutput(receiveAmount, requestedAsset, sellerProgram)] VERIFY [... sellerKey standardProgram actualAmount] FROMALTSTACK [... sellerKey standardProgram actualAmount <position>] 1 [... sellerKey standardProgram actualAmount <position> 1] ADD [... sellerKey standardProgram actualAmount (<position> + 1)] AMOUNT [... sellerKey standardProgram actualAmount (<position> + 1) valueAmount] 2 [... sellerKey standardProgram actualAmount (<position> + 1) valueAmount 2] ROLL [... sellerKey standardProgram (<position> + 1) valueAmount actualAmount] SUB [... sellerKey standardProgram (<position> + 1) (valueAmount - actualAmount)] ASSET [... sellerKey standardProgram (<position> + 1) (valueAmount - actualAmount) valueAsset] 1 [... sellerKey standardProgram (<position> + 1) (valueAmount - actualAmount) valueAsset 1] 4 [... sellerKey standardProgram (<position> + 1) (valueAmount - actualAmount) valueAsset 1 4] ROLL [... sellerKey (<position> + 1) (valueAmount - actualAmount) valueAsset 1 standardProgram] CHECKOUTPUT [... sellerKey checkOutput((valueAmount - actualAmount), valueAsset, standardProgram)] JUMP:$_end [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset] $fullTrade [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset] AMOUNT [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset valueAmount] 2 [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset valueAmount 2] ROLL [... sellerKey standardProgram sellerProgram ratioDenominator requestedAsset valueAmount ratioNumerator] 3 [... sellerKey standardProgram sellerProgram ratioDenominator requestedAsset valueAmount ratioNumerator 3] ROLL [... sellerKey standardProgram sellerProgram requestedAsset valueAmount ratioNumerator ratioDenominator] MULFRACTION [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount] 999 [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount 999] 1000 [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount 999 1000] MULFRACTION [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount] DUP [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount requestedAmount] 0 [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount requestedAmount 0] GREATERTHANOREQUAL [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount (requestedAmount > 0)] VERIFY [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount] FROMALTSTACK [... sellerKey standardProgram sellerProgram requestedAsset requestedAmount <position>] SWAP [... sellerKey standardProgram sellerProgram requestedAsset <position> requestedAmount] 2 [... sellerKey standardProgram sellerProgram requestedAsset <position> requestedAmount 2] ROLL [... sellerKey standardProgram sellerProgram <position> requestedAmount requestedAsset] 1 [... sellerKey standardProgram sellerProgram <position> requestedAmount requestedAsset 1] 4 [... sellerKey standardProgram sellerProgram <position> requestedAmount requestedAsset 1 4] ROLL [... sellerKey standardProgram <position> requestedAmount requestedAsset 1 sellerProgram] CHECKOUTPUT [... sellerKey standardProgram checkOutput(requestedAmount, requestedAsset, sellerProgram)] JUMP:$_end [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset] $cancel [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset <clause selector>] DROP [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset] 6 [... sellerSig sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset 6] ROLL [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset sellerSig] 6 [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset sellerSig 6] ROLL [... standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset sellerSig sellerKey] TXSIGHASH SWAP CHECKSIG [... standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset checkTxSig(sellerKey, sellerSig)] $_end [... sellerKey standardProgram sellerProgram ratioDenominator ratioNumerator requestedAsset]

func P2PKHSigProgram

func P2PKHSigProgram(pubkeyHash []byte) ([]byte, error)

P2PKHSigProgram generates the script for control with pubkey hash

func P2SHProgram

func P2SHProgram(scriptHash []byte) ([]byte, error)

P2SHProgram generates the script for control with script hash

func P2SPMultiSigProgram

func P2SPMultiSigProgram(pubkeys []ed25519.PublicKey, nrequired int) ([]byte, error)

P2SPMultiSigProgram generates the script for control transaction output

func P2SPMultiSigProgramWithHeight

func P2SPMultiSigProgramWithHeight(pubkeys []ed25519.PublicKey, nrequired int, blockHeight int64) ([]byte, error)

P2SPMultiSigProgramWithHeight generates the script with block height for control transaction output

func P2WMCProgram added in v1.0.5

func P2WMCProgram(magneticContractArgs MagneticContractArgs) ([]byte, error)

P2WMCProgram return the segwit pay to magnetic contract

func P2WPKHProgram

func P2WPKHProgram(hash []byte) ([]byte, error)

P2WPKHProgram return the segwit pay to public key hash

func P2WSHProgram

func P2WSHProgram(hash []byte) ([]byte, error)

P2WSHProgram return the segwit pay to script hash

func RetireProgram

func RetireProgram(comment []byte) ([]byte, error)

RetireProgram generates the script for retire output

Types

type Builder

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

func NewBuilder

func NewBuilder() *Builder

func (*Builder) AddData

func (b *Builder) AddData(data []byte) *Builder

AddData adds a pushdata instruction for a given byte string.

func (*Builder) AddInt64

func (b *Builder) AddInt64(n int64) *Builder

AddInt64 adds a pushdata instruction for an integer value.

func (*Builder) AddJump

func (b *Builder) AddJump(target int) *Builder

AddJump adds a JUMP opcode whose target is the given target number. The actual program location of the target does not need to be known yet, as long as SetJumpTarget is called before Build.

func (*Builder) AddJumpIf

func (b *Builder) AddJumpIf(target int) *Builder

AddJump adds a JUMPIF opcode whose target is the given target number. The actual program location of the target does not need to be known yet, as long as SetJumpTarget is called before Build.

func (*Builder) AddOp

func (b *Builder) AddOp(op vm.Op) *Builder

AddOp adds the given opcode to the program.

func (*Builder) AddRawBytes

func (b *Builder) AddRawBytes(data []byte) *Builder

AddRawBytes simply appends the given bytes to the program. (It does not introduce a pushdata opcode.)

func (*Builder) Build

func (b *Builder) Build() ([]byte, error)

Build produces the bytecode of the program. It first resolves any jumps in the program by filling in the addresses of their targets. This requires SetJumpTarget to be called prior to Build for each jump target used (in a call to AddJump or AddJumpIf). If any target's address hasn't been set in this way, this function produces ErrUnresolvedJump. There are no other error conditions.

func (*Builder) NewJumpTarget

func (b *Builder) NewJumpTarget() int

NewJumpTarget allocates a number that can be used as a jump target in AddJump and AddJumpIf. Call SetJumpTarget to associate the number with a program location.

func (*Builder) SetJumpTarget

func (b *Builder) SetJumpTarget(target int) *Builder

SetJumpTarget associates the given jump-target number with the current position in the program - namely, the program's length, such that the first instruction executed by a jump using this target will be whatever instruction is added next. It is legal for SetJumpTarget to be called at the end of the program, causing jumps using that target to fall off the end. There must be a call to SetJumpTarget for every jump target used before any call to Build.

type MagneticContractArgs added in v1.0.5

type MagneticContractArgs struct {
	RequestedAsset   bc.AssetID
	RatioNumerator   int64
	RatioDenominator int64
	SellerProgram    []byte
	SellerKey        []byte
}

MagneticContractArgs is a struct for magnetic contract arguments

Jump to

Keyboard shortcuts

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