2019-10-10 16:52:10 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2019-12-03 14:05:06 +00:00
|
|
|
|
2020-04-14 14:24:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
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
|
|
|
)
|
|
|
|
|
2020-04-14 14:24:21 +00:00
|
|
|
var (
|
|
|
|
verifyInteropID = emit.InteropNameToID([]byte("Neo.Crypto.ECDsaVerify"))
|
|
|
|
multisigInteropID = emit.InteropNameToID([]byte("Neo.Crypto.ECDsaCheckMultiSig"))
|
|
|
|
)
|
|
|
|
|
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:
|
|
|
|
n := emit.BytesToInt(param)
|
|
|
|
if !n.IsInt64() || n.Int64() > MaxArraySize {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
nthings = int(n.Int64())
|
2019-10-10 16:52:10 +00:00
|
|
|
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 {
|
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
|
|
|
|
|
|
|
|
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++
|
|
|
|
if nkeys > MaxArraySize {
|
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
|
|
|
}
|
|
|
|
instr, _, err = ctx.Next()
|
2020-04-14 14:24:21 +00:00
|
|
|
if err != nil || instr != opcode.PUSHNULL {
|
2020-05-08 17:54:24 +00:00
|
|
|
return nsigs, nil, false
|
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 {
|
2020-04-14 14:24:21 +00:00
|
|
|
if len(script) != 41 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-10-10 16:52:10 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
instr, _, err = ctx.Next()
|
2020-04-14 14:24:21 +00:00
|
|
|
if err != nil || instr != opcode.PUSHNULL {
|
2019-10-10 16:52:10 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-04-14 14:24:21 +00:00
|
|
|
instr, param, err = ctx.Next()
|
|
|
|
if err != nil || instr != opcode.SYSCALL || binary.LittleEndian.Uint32(param) != verifyInteropID {
|
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)
|
|
|
|
}
|