2019-10-10 16:52:10 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2021-02-09 18:42:39 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-12-03 14:05:06 +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"
|
2021-02-09 18:42:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/bitfield"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-11 13:31:31 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2019-10-10 16:52:10 +00:00
|
|
|
)
|
|
|
|
|
2021-07-19 10:35:48 +00:00
|
|
|
// MaxMultisigKeys is the maximum number of used keys for correct multisig contract.
|
|
|
|
const MaxMultisigKeys = 1024
|
|
|
|
|
2020-04-14 14:24:21 +00:00
|
|
|
var (
|
2021-05-11 13:32:09 +00:00
|
|
|
verifyInteropID = interopnames.ToID([]byte(interopnames.SystemCryptoCheckSig))
|
2021-05-11 14:13:33 +00:00
|
|
|
multisigInteropID = interopnames.ToID([]byte(interopnames.SystemCryptoCheckMultisig))
|
2020-04-14 14:24:21 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 14:05:06 +00:00
|
|
|
func getNumOfThingsFromInstr(instr opcode.Opcode, param []byte) (int, bool) {
|
2019-10-10 16:52:10 +00:00
|
|
|
var nthings int
|
|
|
|
|
2020-04-21 13:45:48 +00:00
|
|
|
switch {
|
|
|
|
case opcode.PUSH1 <= instr && instr <= opcode.PUSH16:
|
2019-12-03 14:05:06 +00:00
|
|
|
nthings = int(instr-opcode.PUSH1) + 1
|
2020-04-21 13:45:48 +00:00
|
|
|
case instr <= opcode.PUSHINT256:
|
2020-06-04 18:11:27 +00:00
|
|
|
n := bigint.FromBytes(param)
|
2021-07-19 10:35:48 +00:00
|
|
|
if !n.IsInt64() || n.Sign() < 0 || n.Int64() > MaxMultisigKeys {
|
2020-04-21 13:45:48 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
nthings = int(n.Int64())
|
2019-10-10 16:52:10 +00:00
|
|
|
default:
|
|
|
|
return 0, false
|
|
|
|
}
|
2021-07-19 10:35:48 +00:00
|
|
|
if nthings < 1 || nthings > MaxMultisigKeys {
|
2019-10-10 16:52:10 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return nthings, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMultiSigContract checks whether the passed script is a multi-signature
|
|
|
|
// contract.
|
|
|
|
func IsMultiSigContract(script []byte) bool {
|
2020-05-08 17:54:24 +00:00
|
|
|
_, _, ok := ParseMultiSigContract(script)
|
2020-03-05 06:41:35 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
// ParseMultiSigContract returns number of signatures and list of public keys
|
|
|
|
// from the verification script of the contract.
|
|
|
|
func ParseMultiSigContract(script []byte) (int, [][]byte, bool) {
|
2019-10-10 16:52:10 +00:00
|
|
|
var nsigs, nkeys int
|
2021-03-09 15:11:21 +00:00
|
|
|
if len(script) < 42 {
|
|
|
|
return nsigs, nil, false
|
|
|
|
}
|
2019-10-10 16:52:10 +00:00
|
|
|
|
|
|
|
ctx := NewContext(script)
|
|
|
|
instr, param, err := ctx.Next()
|
|
|
|
if err != nil {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
|
|
|
nsigs, ok := getNumOfThingsFromInstr(instr, param)
|
|
|
|
if !ok {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
2020-03-05 06:41:35 +00:00
|
|
|
var pubs [][]byte
|
2019-10-10 16:52:10 +00:00
|
|
|
for {
|
|
|
|
instr, param, err = ctx.Next()
|
|
|
|
if err != nil {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
2020-04-14 14:24:21 +00:00
|
|
|
if instr != opcode.PUSHDATA1 {
|
2019-10-10 16:52:10 +00:00
|
|
|
break
|
|
|
|
}
|
2020-04-14 14:24:21 +00:00
|
|
|
if len(param) < 33 {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2020-04-14 14:24:21 +00:00
|
|
|
}
|
2020-03-05 06:41:35 +00:00
|
|
|
pubs = append(pubs, param)
|
2019-10-10 16:52:10 +00:00
|
|
|
nkeys++
|
2021-07-19 10:35:48 +00:00
|
|
|
if nkeys > MaxMultisigKeys {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if nkeys < nsigs {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
|
|
|
nkeys2, ok := getNumOfThingsFromInstr(instr, param)
|
|
|
|
if !ok {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
|
|
|
if nkeys2 != nkeys {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
2020-04-14 14:24:21 +00:00
|
|
|
instr, param, err = ctx.Next()
|
|
|
|
if err != nil || instr != opcode.SYSCALL || binary.LittleEndian.Uint32(param) != multisigInteropID {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
|
|
|
instr, _, err = ctx.Next()
|
2019-12-03 14:05:06 +00:00
|
|
|
if err != nil || instr != opcode.RET || ctx.ip != len(script) {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, pubs, true
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsSignatureContract checks whether the passed script is a signature check
|
|
|
|
// contract.
|
|
|
|
func IsSignatureContract(script []byte) bool {
|
2021-03-05 09:22:48 +00:00
|
|
|
_, ok := ParseSignatureContract(script)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseSignatureContract parses simple signature contract and returns
|
|
|
|
// public key.
|
|
|
|
func ParseSignatureContract(script []byte) ([]byte, bool) {
|
2021-03-05 07:18:03 +00:00
|
|
|
if len(script) != 40 {
|
2021-03-05 09:22:48 +00:00
|
|
|
return nil, false
|
2020-04-14 14:24:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 16:52:10 +00:00
|
|
|
ctx := NewContext(script)
|
2020-04-14 14:24:21 +00:00
|
|
|
instr, param, err := ctx.Next()
|
|
|
|
if err != nil || instr != opcode.PUSHDATA1 || len(param) != 33 {
|
2021-03-05 09:22:48 +00:00
|
|
|
return nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
2021-03-05 09:22:48 +00:00
|
|
|
pub := param
|
2020-04-14 14:24:21 +00:00
|
|
|
instr, param, err = ctx.Next()
|
|
|
|
if err != nil || instr != opcode.SYSCALL || binary.LittleEndian.Uint32(param) != verifyInteropID {
|
2021-03-05 09:22:48 +00:00
|
|
|
return nil, false
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
2021-03-05 09:22:48 +00:00
|
|
|
return pub, true
|
2019-10-10 16:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsStandardContract checks whether the passed script is a signature or
|
|
|
|
// multi-signature contract.
|
|
|
|
func IsStandardContract(script []byte) bool {
|
|
|
|
return IsSignatureContract(script) || IsMultiSigContract(script)
|
|
|
|
}
|
2021-02-09 18:42:39 +00:00
|
|
|
|
|
|
|
// IsScriptCorrect checks script for errors and mask provided for correctness wrt
|
|
|
|
// instruction boundaries. Normally it returns nil, but can return some specific
|
|
|
|
// error if there is any.
|
|
|
|
func IsScriptCorrect(script []byte, methods bitfield.Field) error {
|
|
|
|
var (
|
|
|
|
l = len(script)
|
|
|
|
instrs = bitfield.New(l)
|
|
|
|
jumps = bitfield.New(l)
|
|
|
|
)
|
|
|
|
ctx := NewContext(script)
|
|
|
|
for ctx.nextip < l {
|
|
|
|
op, param, err := ctx.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
instrs.Set(ctx.ip)
|
|
|
|
switch op {
|
|
|
|
case opcode.JMP, opcode.JMPIF, opcode.JMPIFNOT, opcode.JMPEQ, opcode.JMPNE,
|
|
|
|
opcode.JMPGT, opcode.JMPGE, opcode.JMPLT, opcode.JMPLE,
|
|
|
|
opcode.CALL, opcode.ENDTRY, opcode.JMPL, opcode.JMPIFL,
|
|
|
|
opcode.JMPIFNOTL, opcode.JMPEQL, opcode.JMPNEL,
|
|
|
|
opcode.JMPGTL, opcode.JMPGEL, opcode.JMPLTL, opcode.JMPLEL,
|
|
|
|
opcode.ENDTRYL, opcode.CALLL, opcode.PUSHA:
|
|
|
|
off, _, err := calcJumpOffset(ctx, param) // It does bounds checking.
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
jumps.Set(off)
|
|
|
|
case opcode.TRY, opcode.TRYL:
|
|
|
|
catchP, finallyP := getTryParams(op, param)
|
|
|
|
off, _, err := calcJumpOffset(ctx, catchP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
jumps.Set(off)
|
|
|
|
off, _, err = calcJumpOffset(ctx, finallyP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
jumps.Set(off)
|
|
|
|
case opcode.NEWARRAYT, opcode.ISTYPE, opcode.CONVERT:
|
|
|
|
typ := stackitem.Type(param[0])
|
|
|
|
if !typ.IsValid() {
|
|
|
|
return fmt.Errorf("invalid type specification at offset %d", ctx.ip)
|
|
|
|
}
|
|
|
|
if typ == stackitem.AnyT && op != opcode.NEWARRAYT {
|
|
|
|
return fmt.Errorf("using type ANY is incorrect at offset %d", ctx.ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !jumps.IsSubset(instrs) {
|
|
|
|
return errors.New("some jumps are done to wrong offsets (not to instruction boundary)")
|
|
|
|
}
|
|
|
|
if methods != nil && !methods.IsSubset(instrs) {
|
|
|
|
return errors.New("some methods point to wrong offsets (not to instruction boundary)")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|