2019-10-10 16:52:10 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
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"
|
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
|
|
|
)
|
|
|
|
|
2020-04-14 14:24:21 +00:00
|
|
|
var (
|
2020-08-14 10:50:52 +00:00
|
|
|
verifyInteropID = interopnames.ToID([]byte(interopnames.NeoCryptoVerifyWithECDsaSecp256r1))
|
|
|
|
multisigInteropID = interopnames.ToID([]byte(interopnames.NeoCryptoCheckMultisigWithECDsaSecp256r1))
|
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)
|
2020-06-11 13:31:31 +00:00
|
|
|
if !n.IsInt64() || n.Int64() > stackitem.MaxArraySize {
|
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
|
|
|
|
}
|
2020-06-11 13:31:31 +00:00
|
|
|
if nthings < 1 || nthings > stackitem.MaxArraySize {
|
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
|
|
|
|
|
|
|
|
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++
|
2020-06-11 13:31:31 +00:00
|
|
|
if nkeys > stackitem.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)
|
|
|
|
}
|