2020-02-17 11:54:53 +00:00
|
|
|
package request
|
2018-12-21 09:32:18 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-26 10:13:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-20 13:07:43 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
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/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-12-21 09:32:18 +00:00
|
|
|
)
|
|
|
|
|
2021-03-10 14:43:52 +00:00
|
|
|
// ExpandArrayIntoScript pushes all FuncParam parameters from the given array
|
2022-04-20 18:30:09 +00:00
|
|
|
// into the given buffer in the reverse order.
|
2021-03-10 14:43:52 +00:00
|
|
|
func ExpandArrayIntoScript(script *io.BinWriter, slice []Param) error {
|
2019-11-28 16:08:31 +00:00
|
|
|
for j := len(slice) - 1; j >= 0; j-- {
|
|
|
|
fp, err := slice[j].GetFuncParam()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch fp.Type {
|
2020-07-07 19:35:03 +00:00
|
|
|
case smartcontract.ByteArrayType:
|
|
|
|
str, err := fp.Value.GetBytesBase64()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
emit.Bytes(script, str)
|
|
|
|
case smartcontract.SignatureType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetBytesHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, str)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.StringType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.String(script, str)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.Hash160Type:
|
2019-11-28 16:08:31 +00:00
|
|
|
hash, err := fp.Value.GetUint160FromHex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.Hash256Type:
|
2019-11-28 16:08:31 +00:00
|
|
|
hash, err := fp.Value.GetUint256()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, hash.BytesBE())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.PublicKeyType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := keys.NewPublicKeyFromString(string(str))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script, key.Bytes())
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.IntegerType:
|
2022-02-09 12:13:15 +00:00
|
|
|
bi, err := fp.Value.GetBigInt()
|
2019-11-28 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-09 12:13:15 +00:00
|
|
|
emit.BigInt(script, bi)
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.BoolType:
|
2021-10-28 11:10:18 +00:00
|
|
|
val, err := fp.Value.GetBoolean() // not GetBooleanStrict(), because that's the way C# code works
|
|
|
|
if err != nil {
|
2021-05-25 08:24:28 +00:00
|
|
|
return errors.New("not a bool")
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
2021-05-25 08:24:28 +00:00
|
|
|
if val {
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, 1)
|
2021-05-25 08:24:28 +00:00
|
|
|
} else {
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, 0)
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
2020-08-27 08:44:40 +00:00
|
|
|
case smartcontract.ArrayType:
|
|
|
|
val, err := fp.Value.GetArray()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 14:43:52 +00:00
|
|
|
err = ExpandArrayIntoScript(script, val)
|
2020-08-27 08:44:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
emit.Int(script, int64(len(val)))
|
2020-10-02 08:30:15 +00:00
|
|
|
emit.Opcodes(script, opcode.PACK)
|
2021-11-09 07:37:22 +00:00
|
|
|
case smartcontract.AnyType:
|
|
|
|
if fp.Value.IsNull() {
|
|
|
|
emit.Opcodes(script, opcode.PUSHNULL)
|
|
|
|
}
|
2019-11-28 16:08:31 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("parameter type %v is not supported", fp.Type)
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 12:38:22 +00:00
|
|
|
return script.Err
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CreateFunctionInvocationScript creates a script to invoke the given contract with
|
|
|
|
// the given parameters.
|
2021-11-20 16:25:42 +00:00
|
|
|
func CreateFunctionInvocationScript(contract util.Uint160, method string, param *Param) ([]byte, error) {
|
2020-02-03 14:46:51 +00:00
|
|
|
script := io.NewBufBinWriter()
|
2021-11-20 16:25:42 +00:00
|
|
|
if param == nil {
|
2021-10-16 18:09:29 +00:00
|
|
|
emit.Opcodes(script.BinWriter, opcode.NEWARRAY0)
|
2021-11-20 16:25:42 +00:00
|
|
|
} else if slice, err := param.GetArray(); err == nil {
|
|
|
|
err = ExpandArrayIntoScript(script.BinWriter, slice)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
emit.Int(script.BinWriter, int64(len(slice)))
|
|
|
|
emit.Opcodes(script.BinWriter, opcode.PACK)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("failed to convert %s to script parameter", param)
|
2021-10-16 18:09:29 +00:00
|
|
|
}
|
2019-11-26 10:13:17 +00:00
|
|
|
|
2020-12-29 10:44:07 +00:00
|
|
|
emit.AppCallNoArgs(script.BinWriter, contract, method, callflag.All)
|
2019-11-26 10:13:17 +00:00
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|