2018-03-04 13:56:49 +00:00
|
|
|
package smartcontract
|
|
|
|
|
2019-11-14 08:14:06 +00:00
|
|
|
import (
|
2021-09-10 18:34:48 +00:00
|
|
|
"bytes"
|
2020-06-05 12:45:15 +00:00
|
|
|
"encoding/base64"
|
2019-11-27 09:52:15 +00:00
|
|
|
"encoding/hex"
|
2020-02-20 10:25:01 +00:00
|
|
|
"encoding/json"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2020-03-23 11:53:17 +00:00
|
|
|
"fmt"
|
2022-02-09 13:07:07 +00:00
|
|
|
"math/big"
|
2022-02-22 16:27:32 +00:00
|
|
|
"os"
|
2024-11-15 19:29:09 +00:00
|
|
|
"reflect"
|
2024-08-23 21:07:41 +00:00
|
|
|
"slices"
|
2019-11-27 09:52:15 +00:00
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2021-04-16 14:30:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-02-09 13:07:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parameter represents a smart contract parameter.
|
|
|
|
type Parameter struct {
|
2019-10-22 14:56:03 +00:00
|
|
|
// Type of the parameter.
|
2018-05-04 17:15:35 +00:00
|
|
|
Type ParamType `json:"type"`
|
2018-03-04 13:56:49 +00:00
|
|
|
// The actual value of the parameter.
|
2023-04-03 10:34:24 +00:00
|
|
|
Value any `json:"value"`
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 11:47:07 +00:00
|
|
|
// Convertible is something that can be converted to Parameter.
|
|
|
|
type Convertible interface {
|
|
|
|
ToSCParameter() (Parameter, error)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ParameterPair represents a key-value pair, a slice of which is stored in
|
2020-03-30 15:24:06 +00:00
|
|
|
// MapType Parameter.
|
|
|
|
type ParameterPair struct {
|
|
|
|
Key Parameter `json:"key"`
|
|
|
|
Value Parameter `json:"value"`
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewParameter returns a Parameter with a proper initialized Value
|
2018-03-04 13:56:49 +00:00
|
|
|
// of the given ParamType.
|
|
|
|
func NewParameter(t ParamType) Parameter {
|
|
|
|
return Parameter{
|
|
|
|
Type: t,
|
|
|
|
Value: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:34:18 +00:00
|
|
|
type rawParameter struct {
|
|
|
|
Type ParamType `json:"type"`
|
2020-06-19 07:44:05 +00:00
|
|
|
Value json.RawMessage `json:"value,omitempty"`
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the Marshaler interface.
|
2020-06-23 13:11:49 +00:00
|
|
|
func (p Parameter) MarshalJSON() ([]byte, error) {
|
2020-02-27 14:38:17 +00:00
|
|
|
var (
|
|
|
|
resultRawValue json.RawMessage
|
2020-03-03 14:22:15 +00:00
|
|
|
resultErr error
|
2020-02-27 14:38:17 +00:00
|
|
|
)
|
2021-09-10 18:34:48 +00:00
|
|
|
if p.Value == nil {
|
|
|
|
if _, ok := validParamTypes[p.Type]; ok && p.Type != UnknownType {
|
|
|
|
return json.Marshal(rawParameter{Type: p.Type})
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("can't marshal %s", p.Type)
|
|
|
|
}
|
2020-02-27 14:38:17 +00:00
|
|
|
switch p.Type {
|
2020-04-01 17:31:29 +00:00
|
|
|
case BoolType, StringType, Hash160Type, Hash256Type:
|
2020-02-27 14:38:17 +00:00
|
|
|
resultRawValue, resultErr = json.Marshal(p.Value)
|
2020-03-17 13:04:49 +00:00
|
|
|
case IntegerType:
|
2022-02-09 13:07:07 +00:00
|
|
|
val, ok := p.Value.(*big.Int)
|
2020-03-17 13:04:49 +00:00
|
|
|
if !ok {
|
|
|
|
resultErr = errors.New("invalid integer value")
|
|
|
|
break
|
|
|
|
}
|
2022-02-09 13:07:07 +00:00
|
|
|
resultRawValue = json.RawMessage(`"` + val.String() + `"`)
|
2020-02-27 14:38:17 +00:00
|
|
|
case PublicKeyType, ByteArrayType, SignatureType:
|
2021-09-10 18:34:48 +00:00
|
|
|
if p.Type == PublicKeyType {
|
2020-03-04 16:47:10 +00:00
|
|
|
resultRawValue, resultErr = json.Marshal(hex.EncodeToString(p.Value.([]byte)))
|
2020-06-05 12:45:15 +00:00
|
|
|
} else {
|
|
|
|
resultRawValue, resultErr = json.Marshal(base64.StdEncoding.EncodeToString(p.Value.([]byte)))
|
2020-03-04 16:47:10 +00:00
|
|
|
}
|
2020-02-27 14:38:17 +00:00
|
|
|
case ArrayType:
|
2020-03-30 21:27:58 +00:00
|
|
|
var value = p.Value.([]Parameter)
|
2020-06-23 13:11:49 +00:00
|
|
|
if value == nil {
|
|
|
|
resultRawValue, resultErr = json.Marshal([]Parameter{})
|
|
|
|
} else {
|
|
|
|
resultRawValue, resultErr = json.Marshal(value)
|
|
|
|
}
|
2020-02-27 14:38:17 +00:00
|
|
|
case MapType:
|
2020-03-30 15:24:06 +00:00
|
|
|
ppair := p.Value.([]ParameterPair)
|
|
|
|
resultRawValue, resultErr = json.Marshal(ppair)
|
2020-06-03 15:56:20 +00:00
|
|
|
case InteropInterfaceType, AnyType:
|
2020-06-19 07:44:05 +00:00
|
|
|
resultRawValue = nil
|
2020-02-27 14:38:17 +00:00
|
|
|
default:
|
2020-08-06 14:44:08 +00:00
|
|
|
resultErr = fmt.Errorf("can't marshal %s", p.Type)
|
2020-02-27 14:38:17 +00:00
|
|
|
}
|
|
|
|
if resultErr != nil {
|
|
|
|
return nil, resultErr
|
|
|
|
}
|
|
|
|
return json.Marshal(rawParameter{
|
|
|
|
Type: p.Type,
|
|
|
|
Value: resultRawValue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the Unmarshaler interface.
|
2020-02-21 14:34:18 +00:00
|
|
|
func (p *Parameter) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
var (
|
2020-02-27 14:38:17 +00:00
|
|
|
r rawParameter
|
|
|
|
i int64
|
|
|
|
s string
|
|
|
|
b []byte
|
|
|
|
boolean bool
|
2020-02-21 14:34:18 +00:00
|
|
|
)
|
|
|
|
if err = json.Unmarshal(data, &r); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-07-23 08:46:26 +00:00
|
|
|
p.Type = r.Type
|
2021-09-10 18:34:48 +00:00
|
|
|
p.Value = nil
|
|
|
|
if len(r.Value) == 0 || bytes.Equal(r.Value, []byte("null")) {
|
2021-07-23 08:46:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
switch r.Type {
|
2020-02-27 14:38:17 +00:00
|
|
|
case BoolType:
|
|
|
|
if err = json.Unmarshal(r.Value, &boolean); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.Value = boolean
|
2020-03-04 09:30:48 +00:00
|
|
|
case ByteArrayType, PublicKeyType, SignatureType:
|
2020-02-21 14:34:18 +00:00
|
|
|
if err = json.Unmarshal(r.Value, &s); err != nil {
|
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-06-05 12:45:15 +00:00
|
|
|
if r.Type == PublicKeyType {
|
|
|
|
b, err = hex.DecodeString(s)
|
|
|
|
} else {
|
|
|
|
b, err = base64.StdEncoding.DecodeString(s)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-02-21 14:34:18 +00:00
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
p.Value = b
|
|
|
|
case StringType:
|
|
|
|
if err = json.Unmarshal(r.Value, &s); err != nil {
|
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
p.Value = s
|
2019-11-27 09:52:15 +00:00
|
|
|
case IntegerType:
|
2020-02-21 14:34:18 +00:00
|
|
|
if err = json.Unmarshal(r.Value, &i); err == nil {
|
2022-02-09 13:07:07 +00:00
|
|
|
p.Value = big.NewInt(i)
|
2020-02-21 14:34:18 +00:00
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
// sometimes integer comes as string
|
2022-02-09 13:07:07 +00:00
|
|
|
if jErr := json.Unmarshal(r.Value, &s); jErr != nil {
|
|
|
|
return jErr
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2022-02-09 13:07:07 +00:00
|
|
|
bi, ok := new(big.Int).SetString(s, 10)
|
|
|
|
if !ok {
|
|
|
|
// In this case previous err should mean string contains non-digit characters.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = stackitem.CheckIntegerSize(bi)
|
|
|
|
if err == nil {
|
|
|
|
p.Value = bi
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
case ArrayType:
|
|
|
|
// https://github.com/neo-project/neo/blob/3d59ecca5a8deb057bdad94b3028a6d5e25ac088/neo/Network/RPC/RpcServer.cs#L67
|
|
|
|
var rs []Parameter
|
|
|
|
if err = json.Unmarshal(r.Value, &rs); err != nil {
|
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
p.Value = rs
|
2020-02-27 14:38:17 +00:00
|
|
|
case MapType:
|
2020-03-30 15:24:06 +00:00
|
|
|
var ppair []ParameterPair
|
|
|
|
if err = json.Unmarshal(r.Value, &ppair); err != nil {
|
2020-02-27 14:38:17 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-30 15:24:06 +00:00
|
|
|
p.Value = ppair
|
2020-02-21 14:34:18 +00:00
|
|
|
case Hash160Type:
|
|
|
|
var h util.Uint160
|
|
|
|
if err = json.Unmarshal(r.Value, &h); err != nil {
|
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-04-01 17:31:29 +00:00
|
|
|
p.Value = h
|
2020-02-21 14:34:18 +00:00
|
|
|
case Hash256Type:
|
|
|
|
var h util.Uint256
|
|
|
|
if err = json.Unmarshal(r.Value, &h); err != nil {
|
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
p.Value = h
|
2020-06-03 15:56:20 +00:00
|
|
|
case InteropInterfaceType, AnyType:
|
2020-05-10 21:55:15 +00:00
|
|
|
// stub, ignore value, it can only be null
|
|
|
|
p.Value = nil
|
2019-11-27 09:52:15 +00:00
|
|
|
default:
|
2020-08-06 14:44:08 +00:00
|
|
|
return fmt.Errorf("can't unmarshal %s", p.Type)
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
return
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParameterFromString returns a new Parameter initialized from the given
|
|
|
|
// string in neo-go-specific format. It is intended to be used in user-facing
|
2022-04-20 18:30:09 +00:00
|
|
|
// interfaces and has some heuristics in it to simplify parameter passing. The exact
|
2024-08-26 18:27:09 +00:00
|
|
|
// syntax is documented in the cli documentation. [errors.ErrUnsupported] will be
|
|
|
|
// returned in case of unsupported parameter types.
|
2019-11-27 09:52:15 +00:00
|
|
|
func NewParameterFromString(in string) (*Parameter, error) {
|
|
|
|
var (
|
|
|
|
char rune
|
|
|
|
val string
|
|
|
|
err error
|
|
|
|
r *strings.Reader
|
|
|
|
buf strings.Builder
|
|
|
|
escaped bool
|
|
|
|
hadType bool
|
|
|
|
res = &Parameter{}
|
2021-02-18 15:13:03 +00:00
|
|
|
typStr string
|
2019-11-27 09:52:15 +00:00
|
|
|
)
|
|
|
|
r = strings.NewReader(in)
|
|
|
|
for char, _, err = r.ReadRune(); err == nil && char != utf8.RuneError; char, _, err = r.ReadRune() {
|
|
|
|
if char == '\\' && !escaped {
|
|
|
|
escaped = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if char == ':' && !escaped && !hadType {
|
2021-02-18 15:13:03 +00:00
|
|
|
typStr = buf.String()
|
2020-02-21 14:34:18 +00:00
|
|
|
res.Type, err = ParseParamType(typStr)
|
2019-11-27 09:52:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-21 14:34:18 +00:00
|
|
|
// We currently do not support following types:
|
|
|
|
if res.Type == ArrayType || res.Type == MapType || res.Type == InteropInterfaceType || res.Type == VoidType {
|
2024-08-26 18:27:09 +00:00
|
|
|
return nil, fmt.Errorf("%w: type %s", errors.ErrUnsupported, res.Type)
|
2020-02-21 14:34:18 +00:00
|
|
|
}
|
2019-11-27 09:52:15 +00:00
|
|
|
buf.Reset()
|
|
|
|
hadType = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
escaped = false
|
|
|
|
// We don't care about length and it never fails.
|
|
|
|
_, _ = buf.WriteRune(char)
|
|
|
|
}
|
|
|
|
if char == utf8.RuneError {
|
|
|
|
return nil, errors.New("bad UTF-8 string")
|
|
|
|
}
|
|
|
|
// The only other error `ReadRune` returns is io.EOF, which is fine and
|
|
|
|
// expected, so we don't check err here.
|
|
|
|
|
|
|
|
val = buf.String()
|
|
|
|
if !hadType {
|
|
|
|
res.Type = inferParamType(val)
|
|
|
|
}
|
2021-02-18 15:13:03 +00:00
|
|
|
if res.Type == ByteArrayType && typStr == fileBytesParamType {
|
2022-02-22 16:27:32 +00:00
|
|
|
res.Value, err = os.ReadFile(val)
|
2021-02-18 15:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read '%s' parameter from file '%s': %w", fileBytesParamType, val, err)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2019-11-27 09:52:15 +00:00
|
|
|
res.Value, err = adjustValToType(res.Type, val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2020-03-03 14:22:15 +00:00
|
|
|
}
|
2021-04-16 14:30:25 +00:00
|
|
|
|
2022-07-29 09:24:12 +00:00
|
|
|
// NewParameterFromValue infers Parameter type from the value given and adjusts
|
|
|
|
// the value if needed. It does not copy the value if it can avoid doing so. All
|
|
|
|
// regular integers, util.*, keys.PublicKey*, string and bool types are supported,
|
2024-11-15 19:29:09 +00:00
|
|
|
// slices of various kinds are converted as well. [errors.ErrUnsupported]
|
2024-08-26 18:27:09 +00:00
|
|
|
// will be returned for types that can't be used now.
|
2023-04-03 10:34:24 +00:00
|
|
|
func NewParameterFromValue(value any) (Parameter, error) {
|
2022-07-29 09:24:12 +00:00
|
|
|
var result = Parameter{
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := value.(type) {
|
|
|
|
case []byte:
|
|
|
|
result.Type = ByteArrayType
|
|
|
|
case string:
|
|
|
|
result.Type = StringType
|
|
|
|
case bool:
|
|
|
|
result.Type = BoolType
|
|
|
|
case *big.Int:
|
|
|
|
result.Type = IntegerType
|
|
|
|
case int8:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(int64(v))
|
|
|
|
case byte:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(int64(v))
|
|
|
|
case int16:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(int64(v))
|
|
|
|
case uint16:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(int64(v))
|
|
|
|
case int32:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(int64(v))
|
|
|
|
case uint32:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(int64(v))
|
|
|
|
case int:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(int64(v))
|
|
|
|
case uint:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = new(big.Int).SetUint64(uint64(v))
|
|
|
|
case int64:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = big.NewInt(v)
|
|
|
|
case uint64:
|
|
|
|
result.Type = IntegerType
|
|
|
|
result.Value = new(big.Int).SetUint64(v)
|
2022-08-06 10:16:07 +00:00
|
|
|
case *Parameter:
|
|
|
|
result = *v
|
|
|
|
case Parameter:
|
|
|
|
result = v
|
2024-01-23 11:47:07 +00:00
|
|
|
case Convertible:
|
|
|
|
var err error
|
|
|
|
result, err = v.ToSCParameter()
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("failed to convert smartcontract.Convertible (%T) to Parameter: %w", v, err)
|
|
|
|
}
|
2022-07-29 09:24:12 +00:00
|
|
|
case util.Uint160:
|
|
|
|
result.Type = Hash160Type
|
|
|
|
case util.Uint256:
|
|
|
|
result.Type = Hash256Type
|
2022-09-07 19:35:45 +00:00
|
|
|
case *util.Uint160:
|
|
|
|
if v != nil {
|
|
|
|
return NewParameterFromValue(*v)
|
|
|
|
}
|
|
|
|
result.Type = AnyType
|
|
|
|
result.Value = nil
|
|
|
|
case *util.Uint256:
|
|
|
|
if v != nil {
|
|
|
|
return NewParameterFromValue(*v)
|
|
|
|
}
|
|
|
|
result.Type = AnyType
|
|
|
|
result.Value = nil
|
2022-07-29 09:24:12 +00:00
|
|
|
case keys.PublicKey:
|
|
|
|
return NewParameterFromValue(&v)
|
|
|
|
case *keys.PublicKey:
|
|
|
|
result.Type = PublicKeyType
|
|
|
|
result.Value = v.Bytes()
|
2022-08-06 10:16:07 +00:00
|
|
|
case []Parameter:
|
|
|
|
result.Type = ArrayType
|
2024-08-23 21:07:41 +00:00
|
|
|
result.Value = slices.Clone(v)
|
2023-07-21 10:18:08 +00:00
|
|
|
case nil:
|
|
|
|
result.Type = AnyType
|
2022-07-29 09:24:12 +00:00
|
|
|
default:
|
2024-11-15 19:29:09 +00:00
|
|
|
rv := reflect.ValueOf(value)
|
|
|
|
switch rv.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
res := make([]Parameter, 0, rv.Len())
|
|
|
|
for i := range rv.Len() {
|
|
|
|
elem, err := NewParameterFromValue(rv.Index(i).Interface())
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("array index %d: %w", i, err)
|
|
|
|
}
|
|
|
|
res = append(res, elem)
|
|
|
|
}
|
|
|
|
result.Type = ArrayType
|
|
|
|
result.Value = res
|
2024-11-15 19:51:35 +00:00
|
|
|
case reflect.Map:
|
|
|
|
res := make([]ParameterPair, 0, rv.Len())
|
|
|
|
iter := rv.MapRange()
|
|
|
|
for iter.Next() {
|
|
|
|
k, err := NewParameterFromValue(iter.Key().Interface())
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("map key: %w", err)
|
|
|
|
}
|
|
|
|
v, err := NewParameterFromValue(iter.Value().Interface())
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("map value: %w", err)
|
|
|
|
}
|
|
|
|
res = append(res, ParameterPair{Key: k, Value: v})
|
|
|
|
}
|
|
|
|
result.Type = MapType
|
|
|
|
result.Value = res
|
2024-11-15 19:29:09 +00:00
|
|
|
default:
|
|
|
|
return result, fmt.Errorf("%w: %T type", errors.ErrUnsupported, value)
|
|
|
|
}
|
2022-07-29 09:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-09-11 13:20:08 +00:00
|
|
|
func newArrayOfParameters[E any, S ~[]E](values S) ([]Parameter, error) {
|
2022-07-29 09:38:33 +00:00
|
|
|
res := make([]Parameter, 0, len(values))
|
|
|
|
for i := range values {
|
|
|
|
elem, err := NewParameterFromValue(values[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, elem)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-09-11 13:20:08 +00:00
|
|
|
// NewParametersFromValues is similar to NewParameterFromValue except that it
|
|
|
|
// works with multiple values and returns a simple slice of Parameter.
|
|
|
|
func NewParametersFromValues(values ...any) ([]Parameter, error) {
|
|
|
|
return newArrayOfParameters(values)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ExpandParameterToEmitable converts a parameter to a type which can be handled as
|
|
|
|
// an array item by emit.Array. It correlates with the way an RPC server handles
|
2021-04-16 14:30:25 +00:00
|
|
|
// FuncParams for invoke* calls inside the request.ExpandArrayIntoScript function.
|
2024-08-26 18:27:09 +00:00
|
|
|
// [errors.ErrUnsupported] is returned for unsupported types.
|
2023-04-03 10:34:24 +00:00
|
|
|
func ExpandParameterToEmitable(param Parameter) (any, error) {
|
2021-04-16 14:30:25 +00:00
|
|
|
var err error
|
|
|
|
switch t := param.Type; t {
|
|
|
|
case ArrayType:
|
|
|
|
arr := param.Value.([]Parameter)
|
2023-04-03 10:34:24 +00:00
|
|
|
res := make([]any, len(arr))
|
2021-04-16 14:30:25 +00:00
|
|
|
for i := range arr {
|
|
|
|
res[i], err = ExpandParameterToEmitable(arr[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
2022-09-07 19:35:45 +00:00
|
|
|
case MapType, InteropInterfaceType, UnknownType, VoidType:
|
2024-08-26 18:27:09 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s type", errors.ErrUnsupported, t.String())
|
2021-04-16 14:30:25 +00:00
|
|
|
default:
|
|
|
|
return param.Value, nil
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 11:00:26 +00:00
|
|
|
|
|
|
|
// ToStackItem converts smartcontract parameter to stackitem.Item.
|
|
|
|
func (p *Parameter) ToStackItem() (stackitem.Item, error) {
|
|
|
|
e, err := ExpandParameterToEmitable(*p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stackitem.Make(e), nil
|
|
|
|
}
|