2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-02-03 18:09:50 +00:00
|
|
|
"errors"
|
2020-11-17 19:10:40 +00:00
|
|
|
"math"
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
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
|
|
|
)
|
|
|
|
|
2020-08-04 09:55:36 +00:00
|
|
|
const (
|
|
|
|
// MaxManifestSize is a max length for a valid contract manifest.
|
2020-11-17 19:10:40 +00:00
|
|
|
MaxManifestSize = math.MaxUint16
|
2020-03-18 15:21:12 +00:00
|
|
|
|
2020-08-04 09:55:36 +00:00
|
|
|
// NEP10StandardName represents the name of NEP10 smartcontract standard.
|
|
|
|
NEP10StandardName = "NEP-10"
|
2020-11-19 15:01:42 +00:00
|
|
|
// NEP17StandardName represents the name of NEP17 smartcontract standard.
|
|
|
|
NEP17StandardName = "NEP-17"
|
2020-08-04 09:55:36 +00:00
|
|
|
)
|
2020-07-24 10:40:54 +00:00
|
|
|
|
2020-03-18 15:21:12 +00:00
|
|
|
// Manifest represens contract metadata.
|
|
|
|
type Manifest struct {
|
2020-11-20 08:02:58 +00:00
|
|
|
// Name is a contract's name.
|
|
|
|
Name string `json:"name"`
|
2020-03-18 15:21:12 +00:00
|
|
|
// ABI is a contract's ABI.
|
2020-11-13 18:46:26 +00:00
|
|
|
ABI ABI `json:"abi"`
|
2020-03-18 15:21:12 +00:00
|
|
|
// Groups is a set of groups to which a contract belongs.
|
2020-11-13 18:46:26 +00:00
|
|
|
Groups []Group `json:"groups"`
|
|
|
|
Permissions []Permission `json:"permissions"`
|
2020-08-04 09:55:36 +00:00
|
|
|
// SupportedStandards is a list of standards supported by the contract.
|
2020-11-13 18:46:26 +00:00
|
|
|
SupportedStandards []string `json:"supportedstandards"`
|
2020-03-18 15:21:12 +00:00
|
|
|
// Trusts is a set of hashes to a which contract trusts.
|
2020-11-13 18:46:26 +00:00
|
|
|
Trusts WildUint160s `json:"trusts"`
|
2020-03-18 15:21:12 +00:00
|
|
|
// Extra is an implementation-defined user data.
|
2020-11-13 18:46:26 +00:00
|
|
|
Extra interface{} `json:"extra"`
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManifest returns new manifest with necessary fields initialized.
|
2020-11-18 09:43:51 +00:00
|
|
|
func NewManifest(name string) *Manifest {
|
2020-03-18 15:21:12 +00:00
|
|
|
m := &Manifest{
|
2020-11-20 08:02:58 +00:00
|
|
|
Name: name,
|
2020-03-18 15:21:12 +00:00
|
|
|
ABI: ABI{
|
|
|
|
Methods: []Method{},
|
|
|
|
Events: []Event{},
|
|
|
|
},
|
2020-08-04 09:55:36 +00:00
|
|
|
Groups: []Group{},
|
2021-02-03 18:09:50 +00:00
|
|
|
Permissions: []Permission{},
|
2020-08-04 09:55:36 +00:00
|
|
|
SupportedStandards: []string{},
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
m.Trusts.Restrict()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultManifest returns default contract manifest.
|
2020-11-18 09:43:51 +00:00
|
|
|
func DefaultManifest(name string) *Manifest {
|
|
|
|
m := NewManifest(name)
|
2020-03-18 15:21:12 +00:00
|
|
|
m.Permissions = []Permission{*NewPermission(PermissionWildcard)}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanCall returns true is current contract is allowed to call
|
2020-11-18 09:43:51 +00:00
|
|
|
// method of another contract with specified hash.
|
|
|
|
func (m *Manifest) CanCall(hash util.Uint160, toCall *Manifest, method string) bool {
|
2020-03-18 15:21:12 +00:00
|
|
|
for i := range m.Permissions {
|
2020-11-18 09:43:51 +00:00
|
|
|
if m.Permissions[i].IsAllowed(hash, toCall, method) {
|
2020-03-18 15:21:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:15:10 +00:00
|
|
|
// IsValid checks manifest internal consistency and correctness, one of the
|
|
|
|
// checks is for group signature correctness, contract hash is passed for it.
|
2021-02-08 09:04:57 +00:00
|
|
|
func (m *Manifest) IsValid(hash util.Uint160) error {
|
|
|
|
var err error
|
2021-02-08 10:15:10 +00:00
|
|
|
|
|
|
|
err = m.ABI.IsValid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 11:39:20 +00:00
|
|
|
for _, g := range m.Groups {
|
2021-02-08 09:04:57 +00:00
|
|
|
err = g.IsValid(hash)
|
|
|
|
if err != nil {
|
|
|
|
break
|
2020-07-15 11:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 09:04:57 +00:00
|
|
|
return err
|
2020-07-15 11:39:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 18:09:50 +00:00
|
|
|
// ToStackItem converts Manifest to stackitem.Item.
|
|
|
|
func (m *Manifest) ToStackItem() (stackitem.Item, error) {
|
|
|
|
groups := make([]stackitem.Item, len(m.Groups))
|
|
|
|
for i := range m.Groups {
|
|
|
|
groups[i] = m.Groups[i].ToStackItem()
|
|
|
|
}
|
|
|
|
supportedStandards := make([]stackitem.Item, len(m.SupportedStandards))
|
|
|
|
for i := range m.SupportedStandards {
|
|
|
|
supportedStandards[i] = stackitem.Make(m.SupportedStandards[i])
|
|
|
|
}
|
|
|
|
abi := m.ABI.ToStackItem()
|
|
|
|
permissions := make([]stackitem.Item, len(m.Permissions))
|
|
|
|
for i := range m.Permissions {
|
|
|
|
permissions[i] = m.Permissions[i].ToStackItem()
|
|
|
|
}
|
|
|
|
trusts := stackitem.Item(stackitem.Null{})
|
|
|
|
if !m.Trusts.IsWildcard() {
|
|
|
|
tItems := make([]stackitem.Item, len(m.Trusts.Value))
|
|
|
|
for i := range m.Trusts.Value {
|
|
|
|
tItems[i] = stackitem.NewByteArray(m.Trusts.Value[i].BytesBE())
|
|
|
|
}
|
|
|
|
trusts = stackitem.Make(tItems)
|
|
|
|
}
|
|
|
|
extra := stackitem.Make("null")
|
|
|
|
if m.Extra != nil {
|
|
|
|
e, err := json.Marshal(m.Extra)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
extra = stackitem.NewByteArray(e)
|
|
|
|
}
|
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make(m.Name),
|
|
|
|
stackitem.Make(groups),
|
|
|
|
stackitem.Make(supportedStandards),
|
|
|
|
abi,
|
|
|
|
stackitem.Make(permissions),
|
|
|
|
trusts,
|
|
|
|
extra,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem converts stackitem.Item to Manifest.
|
|
|
|
func (m *Manifest) FromStackItem(item stackitem.Item) error {
|
|
|
|
var err error
|
|
|
|
if item.Type() != stackitem.StructT {
|
|
|
|
return errors.New("invalid Manifest stackitem type")
|
|
|
|
}
|
|
|
|
str := item.Value().([]stackitem.Item)
|
|
|
|
if len(str) != 7 {
|
|
|
|
return errors.New("invalid stackitem length")
|
|
|
|
}
|
|
|
|
m.Name, err = stackitem.ToString(str[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if str[1].Type() != stackitem.ArrayT {
|
|
|
|
return errors.New("invalid Groups stackitem type")
|
|
|
|
}
|
|
|
|
groups := str[1].Value().([]stackitem.Item)
|
|
|
|
m.Groups = make([]Group, len(groups))
|
|
|
|
for i := range groups {
|
|
|
|
group := new(Group)
|
|
|
|
err := group.FromStackItem(groups[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Groups[i] = *group
|
|
|
|
}
|
|
|
|
if str[2].Type() != stackitem.ArrayT {
|
|
|
|
return errors.New("invalid SupportedStandards stackitem type")
|
|
|
|
}
|
|
|
|
supportedStandards := str[2].Value().([]stackitem.Item)
|
|
|
|
m.SupportedStandards = make([]string, len(supportedStandards))
|
|
|
|
for i := range supportedStandards {
|
|
|
|
m.SupportedStandards[i], err = stackitem.ToString(supportedStandards[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abi := new(ABI)
|
|
|
|
if err := abi.FromStackItem(str[3]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.ABI = *abi
|
|
|
|
if str[4].Type() != stackitem.ArrayT {
|
|
|
|
return errors.New("invalid Permissions stackitem type")
|
|
|
|
}
|
|
|
|
permissions := str[4].Value().([]stackitem.Item)
|
|
|
|
m.Permissions = make([]Permission, len(permissions))
|
|
|
|
for i := range permissions {
|
|
|
|
p := new(Permission)
|
|
|
|
if err := p.FromStackItem(permissions[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Permissions[i] = *p
|
|
|
|
}
|
|
|
|
if _, ok := str[5].(stackitem.Null); ok {
|
|
|
|
m.Trusts.Restrict()
|
|
|
|
} else {
|
|
|
|
if str[5].Type() != stackitem.ArrayT {
|
|
|
|
return errors.New("invalid Trusts stackitem type")
|
|
|
|
}
|
|
|
|
trusts := str[5].Value().([]stackitem.Item)
|
|
|
|
m.Trusts = WildUint160s{Value: make([]util.Uint160, len(trusts))}
|
|
|
|
for i := range trusts {
|
|
|
|
bytes, err := trusts[i].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Trusts.Value[i], err = util.Uint160DecodeBytesBE(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extra, err := str[6].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if string(extra) == "null" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.Unmarshal(extra, &m.Extra)
|
|
|
|
}
|