2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-03-18 15:21:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
2020-08-04 09:55:36 +00:00
|
|
|
const (
|
|
|
|
// MaxManifestSize is a max length for a valid contract manifest.
|
2020-08-27 07:10:27 +00:00
|
|
|
MaxManifestSize = 4096
|
2020-03-18 15:21:12 +00:00
|
|
|
|
2020-08-04 09:55:36 +00:00
|
|
|
// MethodInit is a name for default initialization method.
|
|
|
|
MethodInit = "_initialize"
|
|
|
|
|
2020-10-02 09:56:51 +00:00
|
|
|
// MethodDeploy is a name for default method called during contract deployment.
|
|
|
|
MethodDeploy = "_deploy"
|
|
|
|
|
2020-08-13 15:42:53 +00:00
|
|
|
// MethodVerify is a name for default verification method.
|
|
|
|
MethodVerify = "verify"
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
// MethodOnPayment is name of the method which is called when contract receives funds.
|
|
|
|
MethodOnPayment = "onPayment"
|
|
|
|
|
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
|
|
|
// ABI represents a contract application binary interface.
|
|
|
|
type ABI struct {
|
2020-07-23 13:03:00 +00:00
|
|
|
Hash util.Uint160 `json:"hash"`
|
|
|
|
Methods []Method `json:"methods"`
|
|
|
|
Events []Event `json:"events"`
|
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
|
|
|
// SafeMethods is a set of names of safe methods.
|
2020-11-13 18:46:26 +00:00
|
|
|
SafeMethods WildStrings `json:"safemethods"`
|
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-20 08:02:58 +00:00
|
|
|
func NewManifest(h util.Uint160, 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{
|
|
|
|
Hash: h,
|
|
|
|
Methods: []Method{},
|
|
|
|
Events: []Event{},
|
|
|
|
},
|
2020-08-04 09:55:36 +00:00
|
|
|
Groups: []Group{},
|
|
|
|
SupportedStandards: []string{},
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
m.Trusts.Restrict()
|
|
|
|
m.SafeMethods.Restrict()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultManifest returns default contract manifest.
|
2020-11-20 08:02:58 +00:00
|
|
|
func DefaultManifest(h util.Uint160, name string) *Manifest {
|
|
|
|
m := NewManifest(h, name)
|
2020-03-18 15:21:12 +00:00
|
|
|
m.Permissions = []Permission{*NewPermission(PermissionWildcard)}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:13:02 +00:00
|
|
|
// GetMethod returns methods with the specified name.
|
|
|
|
func (a *ABI) GetMethod(name string) *Method {
|
|
|
|
for i := range a.Methods {
|
|
|
|
if a.Methods[i].Name == name {
|
|
|
|
return &a.Methods[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:17:14 +00:00
|
|
|
// GetEvent returns event with the specified name.
|
|
|
|
func (a *ABI) GetEvent(name string) *Event {
|
|
|
|
for i := range a.Events {
|
|
|
|
if a.Events[i].Name == name {
|
|
|
|
return &a.Events[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-18 15:21:12 +00:00
|
|
|
// CanCall returns true is current contract is allowed to call
|
|
|
|
// method of another contract.
|
|
|
|
func (m *Manifest) CanCall(toCall *Manifest, method string) bool {
|
|
|
|
// this if is not present in the original code but should probably be here
|
|
|
|
if toCall.SafeMethods.Contains(method) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for i := range m.Permissions {
|
|
|
|
if m.Permissions[i].IsAllowed(toCall, method) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:39:20 +00:00
|
|
|
// IsValid checks whether the given hash is the one specified in manifest and
|
|
|
|
// verifies it against all the keys in manifest groups.
|
|
|
|
func (m *Manifest) IsValid(hash util.Uint160) bool {
|
|
|
|
if m.ABI.Hash != hash {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, g := range m.Groups {
|
|
|
|
if !g.IsValid(hash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:12:56 +00:00
|
|
|
// EncodeBinary implements io.Serializable.
|
|
|
|
func (m *Manifest) EncodeBinary(w *io.BinWriter) {
|
|
|
|
data, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
w.Err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteVarBytes(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable.
|
|
|
|
func (m *Manifest) DecodeBinary(r *io.BinReader) {
|
2020-10-07 15:29:19 +00:00
|
|
|
data := r.ReadVarBytes(MaxManifestSize)
|
2020-06-09 09:12:56 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
} else if err := json.Unmarshal(data, m); err != nil {
|
|
|
|
r.Err = err
|
|
|
|
}
|
|
|
|
}
|