2020-08-10 16:17:14 +00:00
|
|
|
package standard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Various validation errors.
|
|
|
|
var (
|
|
|
|
ErrMethodMissing = errors.New("method missing")
|
|
|
|
ErrEventMissing = errors.New("event missing")
|
|
|
|
ErrInvalidReturnType = errors.New("invalid return type")
|
|
|
|
ErrInvalidParameterCount = errors.New("invalid parameter count")
|
2021-02-19 10:13:36 +00:00
|
|
|
ErrInvalidParameterName = errors.New("invalid parameter name")
|
2020-08-10 16:17:14 +00:00
|
|
|
ErrInvalidParameterType = errors.New("invalid parameter type")
|
2020-12-08 10:47:05 +00:00
|
|
|
ErrSafeMethodMismatch = errors.New("method has wrong safe flag")
|
2020-08-10 16:17:14 +00:00
|
|
|
)
|
|
|
|
|
2021-02-08 15:17:18 +00:00
|
|
|
var checks = map[string][]*Standard{
|
2022-10-26 18:23:12 +00:00
|
|
|
manifest.NEP11StandardName: {Nep11NonDivisible, Nep11Divisible},
|
|
|
|
manifest.NEP17StandardName: {Nep17},
|
|
|
|
manifest.NEP11Payable: {Nep11Payable},
|
|
|
|
manifest.NEP17Payable: {Nep17Payable},
|
2020-08-10 16:17:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Check checks if the manifest complies with all provided standards.
|
|
|
|
// Currently, only NEP-17 is supported.
|
2020-08-10 16:17:14 +00:00
|
|
|
func Check(m *manifest.Manifest, standards ...string) error {
|
2021-02-19 10:13:36 +00:00
|
|
|
return check(m, true, standards...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckABI is similar to Check but doesn't check parameter names.
|
|
|
|
func CheckABI(m *manifest.Manifest, standards ...string) error {
|
|
|
|
return check(m, false, standards...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(m *manifest.Manifest, checkNames bool, standards ...string) error {
|
2020-08-10 16:17:14 +00:00
|
|
|
for i := range standards {
|
2021-02-08 15:17:18 +00:00
|
|
|
ss, ok := checks[standards[i]]
|
2020-08-10 16:17:14 +00:00
|
|
|
if ok {
|
2021-02-08 15:17:18 +00:00
|
|
|
var err error
|
|
|
|
for i := range ss {
|
2021-02-19 10:13:36 +00:00
|
|
|
if err = comply(m, checkNames, ss[i]); err == nil {
|
2021-02-08 15:17:18 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-08-10 16:17:14 +00:00
|
|
|
return fmt.Errorf("manifest is not compliant with '%s': %w", standards[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comply if m has all methods and event from st manifest and they have the same signature.
|
2022-10-26 18:24:34 +00:00
|
|
|
// Parameter names are checked to exactly match the ones in the given standard.
|
2021-02-08 15:17:18 +00:00
|
|
|
func Comply(m *manifest.Manifest, st *Standard) error {
|
2021-02-19 10:13:36 +00:00
|
|
|
return comply(m, true, st)
|
|
|
|
}
|
|
|
|
|
2022-10-26 18:24:34 +00:00
|
|
|
// ComplyABI is similar to Comply but doesn't check parameter names.
|
2021-02-19 10:13:36 +00:00
|
|
|
func ComplyABI(m *manifest.Manifest, st *Standard) error {
|
|
|
|
return comply(m, false, st)
|
|
|
|
}
|
|
|
|
|
|
|
|
func comply(m *manifest.Manifest, checkNames bool, st *Standard) error {
|
2021-02-08 15:17:18 +00:00
|
|
|
if st.Base != nil {
|
2021-02-19 10:13:36 +00:00
|
|
|
if err := comply(m, checkNames, st.Base); err != nil {
|
2021-02-08 15:17:18 +00:00
|
|
|
return err
|
2020-08-10 16:17:14 +00:00
|
|
|
}
|
2021-02-08 15:17:18 +00:00
|
|
|
}
|
|
|
|
for _, stm := range st.ABI.Methods {
|
2021-02-19 10:13:36 +00:00
|
|
|
if err := checkMethod(m, &stm, false, checkNames); err != nil {
|
2021-02-08 15:17:18 +00:00
|
|
|
return err
|
2020-12-08 10:47:05 +00:00
|
|
|
}
|
2020-08-10 16:17:14 +00:00
|
|
|
}
|
|
|
|
for _, ste := range st.ABI.Events {
|
|
|
|
name := ste.Name
|
|
|
|
ed := m.ABI.GetEvent(name)
|
|
|
|
if ed == nil {
|
|
|
|
return fmt.Errorf("%w: event '%s'", ErrEventMissing, name)
|
|
|
|
} else if len(ste.Parameters) != len(ed.Parameters) {
|
|
|
|
return fmt.Errorf("%w: event '%s' (expected %d, got %d)", ErrInvalidParameterCount,
|
|
|
|
name, len(ste.Parameters), len(ed.Parameters))
|
|
|
|
}
|
|
|
|
for i := range ste.Parameters {
|
2021-02-19 10:13:36 +00:00
|
|
|
if checkNames && ste.Parameters[i].Name != ed.Parameters[i].Name {
|
|
|
|
return fmt.Errorf("%w: event '%s'[%d] (expected %s, got %s)", ErrInvalidParameterName,
|
|
|
|
name, i, ste.Parameters[i].Name, ed.Parameters[i].Name)
|
|
|
|
}
|
2020-08-10 16:17:14 +00:00
|
|
|
if ste.Parameters[i].Type != ed.Parameters[i].Type {
|
|
|
|
return fmt.Errorf("%w: event '%s' (expected %s, got %s)", ErrInvalidParameterType,
|
|
|
|
name, ste.Parameters[i].Type, ed.Parameters[i].Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 15:17:18 +00:00
|
|
|
for _, stm := range st.Optional {
|
2021-02-19 10:13:36 +00:00
|
|
|
if err := checkMethod(m, &stm, true, checkNames); err != nil {
|
2021-02-08 15:17:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 10:13:36 +00:00
|
|
|
func checkMethod(m *manifest.Manifest, expected *manifest.Method,
|
|
|
|
allowMissing bool, checkNames bool) error {
|
2021-02-08 15:17:18 +00:00
|
|
|
actual := m.ABI.GetMethod(expected.Name, len(expected.Parameters))
|
|
|
|
if actual == nil {
|
|
|
|
if allowMissing {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%w: '%s' with %d parameters", ErrMethodMissing,
|
|
|
|
expected.Name, len(expected.Parameters))
|
|
|
|
}
|
|
|
|
if expected.ReturnType != actual.ReturnType {
|
|
|
|
return fmt.Errorf("%w: '%s' (expected %s, got %s)", ErrInvalidReturnType,
|
|
|
|
expected.Name, expected.ReturnType, actual.ReturnType)
|
|
|
|
}
|
|
|
|
for i := range expected.Parameters {
|
2021-02-19 10:13:36 +00:00
|
|
|
if checkNames && expected.Parameters[i].Name != actual.Parameters[i].Name {
|
|
|
|
return fmt.Errorf("%w: '%s'[%d] (expected %s, got %s)", ErrInvalidParameterName,
|
|
|
|
expected.Name, i, expected.Parameters[i].Name, actual.Parameters[i].Name)
|
|
|
|
}
|
2021-02-08 15:17:18 +00:00
|
|
|
if expected.Parameters[i].Type != actual.Parameters[i].Type {
|
|
|
|
return fmt.Errorf("%w: '%s'[%d] (expected %s, got %s)", ErrInvalidParameterType,
|
|
|
|
expected.Name, i, expected.Parameters[i].Type, actual.Parameters[i].Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if expected.Safe != actual.Safe {
|
2022-01-20 18:13:35 +00:00
|
|
|
return fmt.Errorf("'%s' %w: expected %t", expected.Name, ErrSafeMethodMismatch, expected.Safe)
|
2021-02-08 15:17:18 +00:00
|
|
|
}
|
2020-08-10 16:17:14 +00:00
|
|
|
return nil
|
|
|
|
}
|