2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
2021-02-03 18:09:50 +00:00
|
|
|
"errors"
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-02-03 18:09:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-18 15:21:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parameter represents smartcontract's parameter's definition.
|
|
|
|
type Parameter struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type smartcontract.ParamType `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method represents method's metadata.
|
|
|
|
type Method struct {
|
|
|
|
Name string `json:"name"`
|
2020-07-23 13:12:41 +00:00
|
|
|
Offset int `json:"offset"`
|
2020-03-18 15:21:12 +00:00
|
|
|
Parameters []Parameter `json:"parameters"`
|
2020-07-09 13:29:56 +00:00
|
|
|
ReturnType smartcontract.ParamType `json:"returntype"`
|
2020-12-08 10:27:41 +00:00
|
|
|
Safe bool `json:"safe"`
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 09:16:24 +00:00
|
|
|
// NewParameter returns new parameter of specified name and type.
|
2020-03-18 15:21:12 +00:00
|
|
|
func NewParameter(name string, typ smartcontract.ParamType) Parameter {
|
|
|
|
return Parameter{
|
|
|
|
Name: name,
|
|
|
|
Type: typ,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:09:50 +00:00
|
|
|
// ToStackItem converts Method to stackitem.Item.
|
|
|
|
func (m *Method) ToStackItem() stackitem.Item {
|
|
|
|
params := make([]stackitem.Item, len(m.Parameters))
|
|
|
|
for i := range m.Parameters {
|
|
|
|
params[i] = m.Parameters[i].ToStackItem()
|
|
|
|
}
|
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make(m.Name),
|
|
|
|
stackitem.Make(params),
|
|
|
|
stackitem.Make(int(m.ReturnType)),
|
|
|
|
stackitem.Make(m.Offset),
|
|
|
|
stackitem.Make(m.Safe),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem converts stackitem.Item to Method.
|
|
|
|
func (m *Method) FromStackItem(item stackitem.Item) error {
|
|
|
|
var err error
|
|
|
|
if item.Type() != stackitem.StructT {
|
|
|
|
return errors.New("invalid Method stackitem type")
|
|
|
|
}
|
|
|
|
method := item.Value().([]stackitem.Item)
|
|
|
|
if len(method) != 5 {
|
|
|
|
return errors.New("invalid Method stackitem length")
|
|
|
|
}
|
|
|
|
m.Name, err = stackitem.ToString(method[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if method[1].Type() != stackitem.ArrayT {
|
|
|
|
return errors.New("invalid Params stackitem type")
|
|
|
|
}
|
|
|
|
params := method[1].Value().([]stackitem.Item)
|
|
|
|
m.Parameters = make([]Parameter, len(params))
|
|
|
|
for i := range params {
|
|
|
|
p := new(Parameter)
|
|
|
|
if err := p.FromStackItem(params[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Parameters[i] = *p
|
|
|
|
}
|
|
|
|
rTyp, err := method[2].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.ReturnType, err = smartcontract.ConvertToParamType(int(rTyp.Int64()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
offset, err := method[3].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Offset = int(offset.Int64())
|
|
|
|
safe, err := method[4].TryBool()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Safe = safe
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStackItem converts Parameter to stackitem.Item.
|
|
|
|
func (p *Parameter) ToStackItem() stackitem.Item {
|
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make(p.Name),
|
|
|
|
stackitem.Make(int(p.Type)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem converts stackitem.Item to Parameter.
|
|
|
|
func (p *Parameter) FromStackItem(item stackitem.Item) error {
|
|
|
|
var err error
|
|
|
|
if item.Type() != stackitem.StructT {
|
|
|
|
return errors.New("invalid Parameter stackitem type")
|
|
|
|
}
|
|
|
|
param := item.Value().([]stackitem.Item)
|
|
|
|
if len(param) != 2 {
|
|
|
|
return errors.New("invalid Parameter stackitem length")
|
|
|
|
}
|
|
|
|
p.Name, err = stackitem.ToString(param[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
typ, err := param[1].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Type, err = smartcontract.ConvertToParamType(int(typ.Int64()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|