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"
|
|
|
|
"strconv"
|
2019-11-20 13:07:43 +00:00
|
|
|
|
2020-08-14 10:50:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
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-06-09 13:12:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
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
|
|
|
)
|
|
|
|
|
2020-03-11 17:32:06 +00:00
|
|
|
// CreateDeploymentScript returns a script that deploys given smart contract
|
2020-07-02 13:22:49 +00:00
|
|
|
// with its metadata.
|
|
|
|
func CreateDeploymentScript(avm []byte, manif *manifest.Manifest) ([]byte, error) {
|
2020-03-11 17:32:06 +00:00
|
|
|
script := io.NewBufBinWriter()
|
2020-06-18 19:36:28 +00:00
|
|
|
rawManifest, err := manif.MarshalJSON()
|
|
|
|
if err != nil {
|
2020-07-02 13:22:49 +00:00
|
|
|
return nil, err
|
2020-06-18 19:36:28 +00:00
|
|
|
}
|
2020-06-09 13:12:58 +00:00
|
|
|
emit.Bytes(script.BinWriter, rawManifest)
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Bytes(script.BinWriter, avm)
|
2020-08-14 10:50:52 +00:00
|
|
|
emit.Syscall(script.BinWriter, interopnames.SystemContractCreate)
|
2020-07-02 13:22:49 +00:00
|
|
|
return script.Bytes(), nil
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
2019-11-26 10:13:17 +00:00
|
|
|
|
2019-11-28 16:08:31 +00:00
|
|
|
// expandArrayIntoScript pushes all FuncParam parameters from the given array
|
|
|
|
// into the given buffer in reverse order.
|
2020-02-17 11:54:53 +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:
|
2019-11-28 16:08:31 +00:00
|
|
|
val, err := fp.Value.GetInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, int64(val))
|
2020-02-21 14:34:18 +00:00
|
|
|
case smartcontract.BoolType:
|
2019-11-28 16:08:31 +00:00
|
|
|
str, err := fp.Value.GetString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch str {
|
|
|
|
case "true":
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, 1)
|
2019-11-28 16:08:31 +00:00
|
|
|
case "false":
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script, 0)
|
2019-11-28 16:08:31 +00:00
|
|
|
default:
|
2020-02-03 14:46:51 +00:00
|
|
|
return errors.New("wrong boolean value")
|
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
|
|
|
|
}
|
|
|
|
err = expandArrayIntoScript(script, val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
emit.Int(script, int64(len(val)))
|
|
|
|
emit.Opcode(script, opcode.PACK)
|
2019-11-28 16:08:31 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("parameter type %v is not supported", fp.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:13:17 +00:00
|
|
|
// CreateFunctionInvocationScript creates a script to invoke given contract with
|
|
|
|
// given parameters.
|
2020-02-17 11:54:53 +00:00
|
|
|
func CreateFunctionInvocationScript(contract util.Uint160, params Params) ([]byte, error) {
|
2020-02-03 14:46:51 +00:00
|
|
|
script := io.NewBufBinWriter()
|
2019-11-26 10:13:17 +00:00
|
|
|
for i := len(params) - 1; i >= 0; i-- {
|
|
|
|
switch params[i].Type {
|
2020-02-17 11:54:53 +00:00
|
|
|
case StringT:
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.String(script.BinWriter, params[i].String())
|
2020-02-17 11:54:53 +00:00
|
|
|
case NumberT:
|
2019-11-26 10:13:17 +00:00
|
|
|
num, err := params[i].GetInt()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.String(script.BinWriter, strconv.Itoa(num))
|
2020-02-17 11:54:53 +00:00
|
|
|
case ArrayT:
|
2019-11-26 10:13:17 +00:00
|
|
|
slice, err := params[i].GetArray()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
err = expandArrayIntoScript(script.BinWriter, slice)
|
2019-11-26 10:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
emit.Int(script.BinWriter, int64(len(slice)))
|
|
|
|
emit.Opcode(script.BinWriter, opcode.PACK)
|
2019-11-26 10:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 11:58:59 +00:00
|
|
|
emit.AppCall(script.BinWriter, contract)
|
2019-11-26 10:13:17 +00:00
|
|
|
return script.Bytes(), nil
|
|
|
|
}
|