2019-10-10 16:52:10 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2019-12-03 14:05:06 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2019-10-10 16:52:10 +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
|
|
|
|
|
|
|
|
switch instr {
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSH1, opcode.PUSH2, opcode.PUSH3, opcode.PUSH4,
|
|
|
|
opcode.PUSH5, opcode.PUSH6, opcode.PUSH7, opcode.PUSH8,
|
|
|
|
opcode.PUSH9, opcode.PUSH10, opcode.PUSH11, opcode.PUSH12,
|
|
|
|
opcode.PUSH13, opcode.PUSH14, opcode.PUSH15, opcode.PUSH16:
|
|
|
|
nthings = int(instr-opcode.PUSH1) + 1
|
|
|
|
case opcode.PUSHBYTES1:
|
2019-10-10 16:52:10 +00:00
|
|
|
nthings = int(param[0])
|
2019-12-03 14:05:06 +00:00
|
|
|
case opcode.PUSHBYTES2:
|
2019-10-10 16:52:10 +00:00
|
|
|
nthings = int(binary.LittleEndian.Uint16(param))
|
|
|
|
default:
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
if nthings < 1 || nthings > MaxArraySize {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return nthings, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMultiSigContract checks whether the passed script is a multi-signature
|
|
|
|
// contract.
|
|
|
|
func IsMultiSigContract(script []byte) bool {
|
|
|
|
var nsigs, nkeys int
|
|
|
|
|
|
|
|
ctx := NewContext(script)
|
|
|
|
instr, param, err := ctx.Next()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nsigs, ok := getNumOfThingsFromInstr(instr, param)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
instr, param, err = ctx.Next()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-03 14:05:06 +00:00
|
|
|
if instr != opcode.PUSHBYTES33 {
|
2019-10-10 16:52:10 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
nkeys++
|
|
|
|
if nkeys > MaxArraySize {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nkeys < nsigs {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nkeys2, ok := getNumOfThingsFromInstr(instr, param)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if nkeys2 != nkeys {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
instr, _, err = ctx.Next()
|
2019-12-03 14:05:06 +00:00
|
|
|
if err != nil || instr != opcode.CHECKMULTISIG {
|
2019-10-10 16:52:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
instr, _, err = ctx.Next()
|
2019-12-03 14:05:06 +00:00
|
|
|
if err != nil || instr != opcode.RET || ctx.ip != len(script) {
|
2019-10-10 16:52:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSignatureContract checks whether the passed script is a signature check
|
|
|
|
// contract.
|
|
|
|
func IsSignatureContract(script []byte) bool {
|
|
|
|
ctx := NewContext(script)
|
|
|
|
instr, _, err := ctx.Next()
|
2019-12-03 14:05:06 +00:00
|
|
|
if err != nil || instr != opcode.PUSHBYTES33 {
|
2019-10-10 16:52:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
instr, _, err = ctx.Next()
|
2019-12-03 14:05:06 +00:00
|
|
|
if err != nil || instr != opcode.CHECKSIG {
|
2019-10-10 16:52:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
instr, _, err = ctx.Next()
|
2019-12-03 14:05:06 +00:00
|
|
|
if err != nil || instr != opcode.RET || ctx.ip != len(script) {
|
2019-10-10 16:52:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsStandardContract checks whether the passed script is a signature or
|
|
|
|
// multi-signature contract.
|
|
|
|
func IsStandardContract(script []byte) bool {
|
|
|
|
return IsSignatureContract(script) || IsMultiSigContract(script)
|
|
|
|
}
|