2020-02-03 14:24:57 +00:00
|
|
|
package emit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-04-21 13:45:48 +00:00
|
|
|
"math/bits"
|
2020-02-03 14:24:57 +00:00
|
|
|
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-12-29 10:44:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-03 18:16:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-02-03 14:24:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Instruction emits a VM Instruction with data to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Instruction(w *io.BinWriter, op opcode.Opcode, b []byte) {
|
|
|
|
w.WriteB(byte(op))
|
|
|
|
w.WriteBytes(b)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
// Opcodes emits a single VM Instruction without arguments to the given buffer.
|
|
|
|
func Opcodes(w *io.BinWriter, ops ...opcode.Opcode) {
|
|
|
|
for _, op := range ops {
|
|
|
|
w.WriteB(byte(op))
|
|
|
|
}
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Bool emits a bool type to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Bool(w *io.BinWriter, ok bool) {
|
2022-10-28 09:12:32 +00:00
|
|
|
var opVal = opcode.PUSHT
|
2022-10-25 10:08:33 +00:00
|
|
|
if !ok {
|
2022-10-28 09:12:32 +00:00
|
|
|
opVal = opcode.PUSHF
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2022-10-28 09:12:32 +00:00
|
|
|
Opcodes(w, opVal)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 13:45:48 +00:00
|
|
|
func padRight(s int, buf []byte) []byte {
|
|
|
|
l := len(buf)
|
|
|
|
buf = buf[:s]
|
|
|
|
if buf[l-1]&0x80 != 0 {
|
|
|
|
for i := l; i < s; i++ {
|
|
|
|
buf[i] = 0xFF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Int emits an int type to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Int(w *io.BinWriter, i int64) {
|
2022-02-09 12:13:15 +00:00
|
|
|
if smallInt(w, i) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bigInt(w, big.NewInt(i), false)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// BigInt emits a big-integer to the given buffer.
|
2022-02-09 12:13:15 +00:00
|
|
|
func BigInt(w *io.BinWriter, n *big.Int) {
|
|
|
|
bigInt(w, n, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func smallInt(w *io.BinWriter, i int64) bool {
|
2020-02-03 14:46:51 +00:00
|
|
|
switch {
|
|
|
|
case i == -1:
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, opcode.PUSHM1)
|
2020-04-21 13:45:48 +00:00
|
|
|
case i >= 0 && i < 16:
|
2022-02-09 12:13:15 +00:00
|
|
|
val := opcode.Opcode(int(opcode.PUSH0) + int(i))
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, val)
|
2020-02-03 14:46:51 +00:00
|
|
|
default:
|
2022-02-09 12:13:15 +00:00
|
|
|
return false
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2022-02-09 12:13:15 +00:00
|
|
|
return true
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 12:13:15 +00:00
|
|
|
func bigInt(w *io.BinWriter, n *big.Int, trySmall bool) {
|
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if trySmall && n.IsInt64() && smallInt(w, n.Int64()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-09 12:30:26 +00:00
|
|
|
if err := stackitem.CheckIntegerSize(n); err != nil {
|
|
|
|
w.Err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:16:34 +00:00
|
|
|
buf := bigint.ToPreallocatedBytes(n, make([]byte, 0, 32))
|
|
|
|
if len(buf) == 0 {
|
|
|
|
Opcodes(w, opcode.PUSH0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
padSize := byte(8 - bits.LeadingZeros8(byte(len(buf)-1)))
|
|
|
|
Opcodes(w, opcode.PUSHINT8+opcode.Opcode(padSize))
|
|
|
|
w.WriteBytes(padRight(1<<padSize, buf))
|
|
|
|
}
|
|
|
|
|
2023-05-15 15:04:32 +00:00
|
|
|
// Array emits an array of elements to the given buffer. It accepts elements of the following types:
|
|
|
|
// - int8, int16, int32, int64, int
|
|
|
|
// - uint8, uint16, uint32
|
|
|
|
// - *big.Int
|
|
|
|
// - string, []byte
|
|
|
|
// - util.Uint160, *util.Uint160, util.Uint256, *util.Uint256
|
|
|
|
// - bool
|
|
|
|
// - stackitem.Convertible, stackitem.Item
|
|
|
|
// - nil
|
|
|
|
// - []any
|
2023-04-03 10:34:24 +00:00
|
|
|
func Array(w *io.BinWriter, es ...any) {
|
2022-01-14 14:21:14 +00:00
|
|
|
if len(es) == 0 {
|
|
|
|
Opcodes(w, opcode.NEWARRAY0)
|
|
|
|
return
|
|
|
|
}
|
2020-03-27 08:00:06 +00:00
|
|
|
for i := len(es) - 1; i >= 0; i-- {
|
|
|
|
switch e := es[i].(type) {
|
2023-04-03 10:34:24 +00:00
|
|
|
case []any:
|
2020-11-24 08:08:52 +00:00
|
|
|
Array(w, e...)
|
2020-03-27 08:00:06 +00:00
|
|
|
case int64:
|
|
|
|
Int(w, e)
|
2021-12-13 12:28:08 +00:00
|
|
|
case int32:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case uint32:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case int16:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case uint16:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case int8:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case uint8:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case int:
|
|
|
|
Int(w, int64(e))
|
2021-03-05 11:16:34 +00:00
|
|
|
case *big.Int:
|
2022-02-09 12:13:15 +00:00
|
|
|
BigInt(w, e)
|
2020-03-27 08:00:06 +00:00
|
|
|
case string:
|
|
|
|
String(w, e)
|
|
|
|
case util.Uint160:
|
|
|
|
Bytes(w, e.BytesBE())
|
2021-04-16 13:19:37 +00:00
|
|
|
case util.Uint256:
|
|
|
|
Bytes(w, e.BytesBE())
|
2022-09-07 19:34:46 +00:00
|
|
|
case *util.Uint160:
|
|
|
|
if e == nil {
|
|
|
|
Opcodes(w, opcode.PUSHNULL)
|
|
|
|
} else {
|
|
|
|
Bytes(w, e.BytesBE())
|
|
|
|
}
|
|
|
|
case *util.Uint256:
|
|
|
|
if e == nil {
|
|
|
|
Opcodes(w, opcode.PUSHNULL)
|
|
|
|
} else {
|
|
|
|
Bytes(w, e.BytesBE())
|
|
|
|
}
|
2020-03-27 08:00:06 +00:00
|
|
|
case []byte:
|
|
|
|
Bytes(w, e)
|
|
|
|
case bool:
|
|
|
|
Bool(w, e)
|
2023-05-15 15:04:32 +00:00
|
|
|
case stackitem.Convertible:
|
|
|
|
Convertible(w, e)
|
|
|
|
case stackitem.Item:
|
|
|
|
StackItem(w, e)
|
2020-03-27 08:00:06 +00:00
|
|
|
default:
|
2020-08-04 09:03:31 +00:00
|
|
|
if es[i] != nil {
|
2021-07-22 11:23:31 +00:00
|
|
|
w.Err = fmt.Errorf("unsupported type: %T", e)
|
2020-08-04 09:03:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, opcode.PUSHNULL)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Int(w, int64(len(es)))
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, opcode.PACK)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:04:32 +00:00
|
|
|
// Convertible converts provided stackitem.Convertible to the stackitem.Item and
|
|
|
|
// emits the item to the given buffer.
|
|
|
|
func Convertible(w *io.BinWriter, c stackitem.Convertible) {
|
|
|
|
si, err := c.ToStackItem()
|
|
|
|
if err != nil {
|
|
|
|
w.Err = fmt.Errorf("failed to convert stackitem.Convertible to stackitem: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
StackItem(w, si)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StackItem emits provided stackitem.Item to the given buffer.
|
|
|
|
func StackItem(w *io.BinWriter, si stackitem.Item) {
|
|
|
|
switch t := si.Type(); t {
|
|
|
|
case stackitem.AnyT:
|
|
|
|
if si.Value() == nil {
|
|
|
|
Opcodes(w, opcode.PUSHNULL)
|
|
|
|
} else {
|
|
|
|
w.Err = fmt.Errorf("only nil value supported for %s", t)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case stackitem.BooleanT:
|
|
|
|
Bool(w, si.Value().(bool))
|
|
|
|
case stackitem.IntegerT:
|
|
|
|
BigInt(w, si.Value().(*big.Int))
|
|
|
|
case stackitem.ByteArrayT, stackitem.BufferT:
|
|
|
|
Bytes(w, si.Value().([]byte))
|
|
|
|
case stackitem.ArrayT:
|
|
|
|
arr := si.Value().([]stackitem.Item)
|
|
|
|
arrAny := make([]any, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
arrAny[i] = arr[i]
|
|
|
|
}
|
|
|
|
Array(w, arrAny...)
|
|
|
|
case stackitem.StructT:
|
|
|
|
arr := si.Value().([]stackitem.Item)
|
|
|
|
for i := len(arr) - 1; i >= 0; i-- {
|
|
|
|
StackItem(w, arr[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
Int(w, int64(len(arr)))
|
|
|
|
Opcodes(w, opcode.PACKSTRUCT)
|
|
|
|
case stackitem.MapT:
|
|
|
|
arr := si.Value().([]stackitem.MapElement)
|
|
|
|
for i := len(arr) - 1; i >= 0; i-- {
|
|
|
|
StackItem(w, arr[i].Value)
|
|
|
|
StackItem(w, arr[i].Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
Int(w, int64(len(arr)))
|
|
|
|
Opcodes(w, opcode.PACKMAP)
|
|
|
|
default:
|
|
|
|
w.Err = fmt.Errorf("%s is unsuppoted", t)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:24:57 +00:00
|
|
|
// String emits a string to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func String(w *io.BinWriter, s string) {
|
|
|
|
Bytes(w, []byte(s))
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes emits a byte array to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Bytes(w *io.BinWriter, b []byte) {
|
|
|
|
var n = len(b)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case n < 0x100:
|
|
|
|
Instruction(w, opcode.PUSHDATA1, []byte{byte(n)})
|
|
|
|
case n < 0x10000:
|
2020-02-03 14:24:57 +00:00
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(n))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.PUSHDATA2, buf)
|
|
|
|
default:
|
2020-02-03 14:24:57 +00:00
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, uint32(n))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.PUSHDATA4, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
w.WriteBytes(b)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Syscall emits the syscall API to the given buffer.
|
|
|
|
// Syscall API string cannot be 0.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Syscall(w *io.BinWriter, api string) {
|
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
} else if len(api) == 0 {
|
|
|
|
w.Err = errors.New("syscall api cannot be of length 0")
|
|
|
|
return
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-04-15 14:40:05 +00:00
|
|
|
buf := make([]byte, 4)
|
2020-08-13 07:41:33 +00:00
|
|
|
binary.LittleEndian.PutUint32(buf, interopnames.ToID([]byte(api)))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.SYSCALL, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Call emits a call Instruction with the label to the given buffer.
|
2020-02-21 08:50:03 +00:00
|
|
|
func Call(w *io.BinWriter, op opcode.Opcode, label uint16) {
|
2020-02-03 14:46:51 +00:00
|
|
|
Jmp(w, op, label)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Jmp emits a jump Instruction along with the label to the given buffer.
|
2020-02-21 08:50:03 +00:00
|
|
|
func Jmp(w *io.BinWriter, op opcode.Opcode, label uint16) {
|
2020-02-03 14:46:51 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
} else if !isInstructionJmp(op) {
|
|
|
|
w.Err = fmt.Errorf("opcode %s is not a jump or call type", op.String())
|
|
|
|
return
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-04-23 08:56:36 +00:00
|
|
|
buf := make([]byte, 4)
|
2020-02-21 08:50:03 +00:00
|
|
|
binary.LittleEndian.PutUint16(buf, label)
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, op, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// AppCallNoArgs emits a call to the provided contract.
|
2020-12-29 10:44:07 +00:00
|
|
|
func AppCallNoArgs(w *io.BinWriter, scriptHash util.Uint160, operation string, f callflag.CallFlag) {
|
|
|
|
Int(w, int64(f))
|
|
|
|
String(w, operation)
|
2020-05-07 11:38:19 +00:00
|
|
|
Bytes(w, scriptHash.BytesBE())
|
2020-08-14 10:50:52 +00:00
|
|
|
Syscall(w, interopnames.SystemContractCall)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 14:25:43 +00:00
|
|
|
// AppCall emits SYSCALL with System.Contract.Call parameter for given contract, operation, call flag and arguments.
|
2023-04-03 10:34:24 +00:00
|
|
|
func AppCall(w *io.BinWriter, scriptHash util.Uint160, operation string, f callflag.CallFlag, args ...any) {
|
2020-03-27 08:00:06 +00:00
|
|
|
Array(w, args...)
|
2020-12-29 10:44:07 +00:00
|
|
|
AppCallNoArgs(w, scriptHash, operation, f)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 19:57:10 +00:00
|
|
|
// CheckSig emits a single-key verification script using given []bytes as a key.
|
|
|
|
// It does not check for key correctness, so you can get an invalid script if the
|
|
|
|
// data passed is not really a public key.
|
|
|
|
func CheckSig(w *io.BinWriter, key []byte) {
|
|
|
|
Bytes(w, key)
|
|
|
|
Syscall(w, interopnames.SystemCryptoCheckSig)
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:24:57 +00:00
|
|
|
func isInstructionJmp(op opcode.Opcode) bool {
|
2020-08-20 05:34:14 +00:00
|
|
|
return opcode.JMP <= op && op <= opcode.CALLL || op == opcode.ENDTRYL
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|