2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
2021-06-28 16:09:19 +00:00
|
|
|
"bytes"
|
2020-03-18 15:21:12 +00:00
|
|
|
"encoding/json"
|
2021-02-03 18:09:50 +00:00
|
|
|
"errors"
|
2022-08-22 10:26:12 +00:00
|
|
|
"fmt"
|
2020-11-17 19:10:40 +00:00
|
|
|
"math"
|
2024-08-23 21:07:41 +00:00
|
|
|
"slices"
|
2024-07-26 08:43:10 +00:00
|
|
|
"strings"
|
2020-03-18 15:21:12 +00:00
|
|
|
|
2021-09-15 12:01:01 +00:00
|
|
|
ojson "github.com/nspcc-dev/go-ordered-json"
|
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 (
|
2022-04-20 18:30:09 +00:00
|
|
|
// MaxManifestSize is the 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
|
|
|
|
2021-11-18 13:37:42 +00:00
|
|
|
// NEP11StandardName represents the name of NEP-11 smartcontract standard.
|
2021-02-08 15:17:18 +00:00
|
|
|
NEP11StandardName = "NEP-11"
|
2021-11-18 13:37:42 +00:00
|
|
|
// NEP17StandardName represents the name of NEP-17 smartcontract standard.
|
2020-11-19 15:01:42 +00:00
|
|
|
NEP17StandardName = "NEP-17"
|
2021-02-19 10:48:45 +00:00
|
|
|
// NEP11Payable represents the name of contract interface which can receive NEP-11 tokens.
|
|
|
|
NEP11Payable = "NEP-11-Payable"
|
|
|
|
// NEP17Payable represents the name of contract interface which can receive NEP-17 tokens.
|
|
|
|
NEP17Payable = "NEP-17-Payable"
|
2024-07-26 08:43:10 +00:00
|
|
|
|
|
|
|
emptyFeatures = "{}"
|
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"`
|
2021-04-30 14:19:52 +00:00
|
|
|
// Features is a set of contract features. Currently unused.
|
|
|
|
Features json.RawMessage `json:"features"`
|
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.
|
2021-05-04 11:48:50 +00:00
|
|
|
Trusts WildPermissionDescs `json:"trusts"`
|
2020-03-18 15:21:12 +00:00
|
|
|
// Extra is an implementation-defined user data.
|
2021-02-12 19:32:34 +00:00
|
|
|
Extra json.RawMessage `json:"extra"`
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewManifest returns a 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{},
|
|
|
|
},
|
2024-07-26 08:43:10 +00:00
|
|
|
Features: json.RawMessage(emptyFeatures),
|
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{},
|
2021-02-12 19:32:34 +00:00
|
|
|
Extra: json.RawMessage("null"),
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
m.Trusts.Restrict()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DefaultManifest returns the 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
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CanCall returns true if the current contract is allowed to call
|
|
|
|
// the method of another contract with the specified hash.
|
2020-11-18 09:43:51 +00:00
|
|
|
func (m *Manifest) CanCall(hash util.Uint160, toCall *Manifest, method string) bool {
|
2024-08-24 19:03:41 +00:00
|
|
|
return slices.ContainsFunc(m.Permissions, func(p Permission) bool {
|
|
|
|
return p.IsAllowed(hash, toCall, method)
|
|
|
|
})
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2022-07-14 12:36:21 +00:00
|
|
|
// If hash is empty, then hash-related checks are omitted.
|
2023-11-22 17:15:43 +00:00
|
|
|
func (m *Manifest) IsValid(hash util.Uint160, checkSize bool) error {
|
2021-02-08 09:04:57 +00:00
|
|
|
var err error
|
2021-02-08 10:15:10 +00:00
|
|
|
|
2021-02-08 19:58:22 +00:00
|
|
|
if m.Name == "" {
|
|
|
|
return errors.New("no name")
|
|
|
|
}
|
|
|
|
|
2024-08-24 19:03:41 +00:00
|
|
|
if slices.Contains(m.SupportedStandards, "") {
|
|
|
|
return errors.New("invalid nameless supported standard")
|
2021-02-08 19:58:22 +00:00
|
|
|
}
|
2024-08-26 17:08:58 +00:00
|
|
|
if sliceHasDups(m.SupportedStandards, strings.Compare) {
|
|
|
|
return errors.New("duplicate supported standards")
|
2021-02-08 19:58:22 +00:00
|
|
|
}
|
2021-02-08 10:15:10 +00:00
|
|
|
err = m.ABI.IsValid()
|
|
|
|
if err != nil {
|
2022-08-22 10:26:12 +00:00
|
|
|
return fmt.Errorf("ABI: %w", err)
|
2021-02-08 10:15:10 +00:00
|
|
|
}
|
2024-07-26 08:43:10 +00:00
|
|
|
|
|
|
|
if strings.Map(func(c rune) rune {
|
|
|
|
switch c {
|
|
|
|
case ' ', '\n', '\t', '\r': // Strip all JSON whitespace.
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}, string(m.Features)) != emptyFeatures { // empty struct should be left.
|
|
|
|
return errors.New("invalid features")
|
|
|
|
}
|
2021-02-08 19:58:22 +00:00
|
|
|
err = Groups(m.Groups).AreValid(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-26 09:32:49 +00:00
|
|
|
if m.Trusts.Value == nil && !m.Trusts.Wildcard {
|
|
|
|
return errors.New("invalid (null?) trusts")
|
|
|
|
}
|
2024-08-26 17:08:58 +00:00
|
|
|
if sliceHasDups(m.Trusts.Value, PermissionDesc.Compare) {
|
|
|
|
return errors.New("duplicate trusted contracts")
|
2020-07-15 11:39:20 +00:00
|
|
|
}
|
2023-11-22 11:07:15 +00:00
|
|
|
err = Permissions(m.Permissions).AreValid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-22 17:15:43 +00:00
|
|
|
if !checkSize {
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-22 11:07:15 +00:00
|
|
|
si, err := m.ToStackItem()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check manifest serialisation: %w", err)
|
|
|
|
}
|
|
|
|
_, err = stackitem.Serialize(si)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("manifest is not serializable: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-15 11:39:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsStandardSupported denotes whether the specified standard is supported by the contract.
|
2021-07-23 12:52:30 +00:00
|
|
|
func (m *Manifest) IsStandardSupported(standard string) bool {
|
2024-08-24 19:03:41 +00:00
|
|
|
return slices.Contains(m.SupportedStandards, standard)
|
2021-07-23 12:52:30 +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))
|
2021-05-04 11:48:50 +00:00
|
|
|
for i, v := range m.Trusts.Value {
|
|
|
|
tItems[i] = v.ToStackItem()
|
2021-02-03 18:09:50 +00:00
|
|
|
}
|
|
|
|
trusts = stackitem.Make(tItems)
|
|
|
|
}
|
2021-06-28 16:09:19 +00:00
|
|
|
extra := extraToStackItem(m.Extra)
|
2021-02-03 18:09:50 +00:00
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.Make(m.Name),
|
|
|
|
stackitem.Make(groups),
|
2021-04-30 14:19:52 +00:00
|
|
|
stackitem.NewMap(),
|
2021-02-03 18:09:50 +00:00
|
|
|
stackitem.Make(supportedStandards),
|
|
|
|
abi,
|
|
|
|
stackitem.Make(permissions),
|
|
|
|
trusts,
|
|
|
|
extra,
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:09:19 +00:00
|
|
|
// extraToStackItem removes indentation from `Extra` field in JSON and
|
|
|
|
// converts it to a byte-array stack item.
|
|
|
|
func extraToStackItem(rawExtra []byte) stackitem.Item {
|
|
|
|
extra := stackitem.Make("null")
|
|
|
|
if rawExtra == nil || string(rawExtra) == "null" {
|
|
|
|
return extra
|
|
|
|
}
|
|
|
|
|
|
|
|
d := ojson.NewDecoder(bytes.NewReader(rawExtra))
|
|
|
|
// The result is put directly in the database and affects state-root calculation,
|
|
|
|
// thus use ordered map to stay compatible with C# implementation.
|
|
|
|
d.UseOrderedObject()
|
|
|
|
// Prevent accidental precision loss.
|
|
|
|
d.UseNumber()
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
var obj any
|
2021-06-28 16:09:19 +00:00
|
|
|
|
|
|
|
// The error can't really occur because `json.RawMessage` is already a valid json.
|
|
|
|
_ = d.Decode(&obj)
|
|
|
|
res, _ := ojson.Marshal(obj)
|
|
|
|
return stackitem.NewByteArray(res)
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:09:50 +00:00
|
|
|
// 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)
|
2021-04-30 14:19:52 +00:00
|
|
|
if len(str) != 8 {
|
2021-02-03 18:09:50 +00:00
|
|
|
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
|
|
|
|
}
|
2021-04-30 14:19:52 +00:00
|
|
|
if str[2].Type() != stackitem.MapT || str[2].(*stackitem.Map).Len() != 0 {
|
|
|
|
return errors.New("invalid Features stackitem")
|
|
|
|
}
|
2024-07-26 08:43:10 +00:00
|
|
|
m.Features = json.RawMessage(emptyFeatures)
|
2021-04-30 14:19:52 +00:00
|
|
|
if str[3].Type() != stackitem.ArrayT {
|
2021-02-03 18:09:50 +00:00
|
|
|
return errors.New("invalid SupportedStandards stackitem type")
|
|
|
|
}
|
2021-04-30 14:19:52 +00:00
|
|
|
supportedStandards := str[3].Value().([]stackitem.Item)
|
2021-02-03 18:09:50 +00:00
|
|
|
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)
|
2021-04-30 14:19:52 +00:00
|
|
|
if err := abi.FromStackItem(str[4]); err != nil {
|
2021-02-03 18:09:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.ABI = *abi
|
2021-04-30 14:19:52 +00:00
|
|
|
if str[5].Type() != stackitem.ArrayT {
|
2021-02-03 18:09:50 +00:00
|
|
|
return errors.New("invalid Permissions stackitem type")
|
|
|
|
}
|
2021-04-30 14:19:52 +00:00
|
|
|
permissions := str[5].Value().([]stackitem.Item)
|
2021-02-03 18:09:50 +00:00
|
|
|
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
|
|
|
|
}
|
2021-04-30 14:19:52 +00:00
|
|
|
if _, ok := str[6].(stackitem.Null); ok {
|
2024-07-26 09:32:49 +00:00
|
|
|
m.Trusts = WildPermissionDescs{Value: nil, Wildcard: true} // wildcard by default
|
2021-02-03 18:09:50 +00:00
|
|
|
} else {
|
2021-04-30 14:19:52 +00:00
|
|
|
if str[6].Type() != stackitem.ArrayT {
|
2021-02-03 18:09:50 +00:00
|
|
|
return errors.New("invalid Trusts stackitem type")
|
|
|
|
}
|
2021-04-30 14:19:52 +00:00
|
|
|
trusts := str[6].Value().([]stackitem.Item)
|
2021-05-04 11:48:50 +00:00
|
|
|
m.Trusts = WildPermissionDescs{Value: make([]PermissionDesc, len(trusts))}
|
2021-02-03 18:09:50 +00:00
|
|
|
for i := range trusts {
|
2021-05-04 11:48:50 +00:00
|
|
|
v := new(PermissionDesc)
|
|
|
|
err = v.FromStackItem(trusts[i])
|
2021-02-03 18:09:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-04 11:48:50 +00:00
|
|
|
m.Trusts.Value[i] = *v
|
2021-02-03 18:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-30 14:19:52 +00:00
|
|
|
extra, err := str[7].TryBytes()
|
2021-02-03 18:09:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-12 19:32:34 +00:00
|
|
|
m.Extra = extra
|
|
|
|
return nil
|
2021-02-03 18:09:50 +00:00
|
|
|
}
|