2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-07-15 11:39:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-03-18 15:21:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-07-15 11:39:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event is a description of a single event.
|
|
|
|
type Event struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Parameters []Parameter `json:"parameters"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group represents a group of smartcontracts identified by a public key.
|
|
|
|
// Every SC in a group must provide signature of it's hash to prove
|
|
|
|
// it belongs to a group.
|
|
|
|
type Group struct {
|
2020-07-09 13:29:56 +00:00
|
|
|
PublicKey *keys.PublicKey `json:"pubkey"`
|
2020-03-18 15:21:12 +00:00
|
|
|
Signature []byte `json:"signature"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type groupAux struct {
|
2020-07-09 13:29:56 +00:00
|
|
|
PublicKey string `json:"pubkey"`
|
2020-03-18 15:21:12 +00:00
|
|
|
Signature []byte `json:"signature"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:39:20 +00:00
|
|
|
// IsValid checks whether group's signature corresponds to the given hash.
|
|
|
|
func (g *Group) IsValid(h util.Uint160) bool {
|
|
|
|
return g.PublicKey.Verify(g.Signature, hash.Sha256(h.BytesBE()).BytesBE())
|
|
|
|
}
|
|
|
|
|
2020-03-18 15:21:12 +00:00
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (g *Group) MarshalJSON() ([]byte, error) {
|
|
|
|
aux := &groupAux{
|
|
|
|
PublicKey: hex.EncodeToString(g.PublicKey.Bytes()),
|
|
|
|
Signature: g.Signature,
|
|
|
|
}
|
|
|
|
return json.Marshal(aux)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (g *Group) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := new(groupAux)
|
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b, err := hex.DecodeString(aux.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pub := new(keys.PublicKey)
|
|
|
|
if err := pub.DecodeBytes(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.PublicKey = pub
|
|
|
|
g.Signature = aux.Signature
|
|
|
|
return nil
|
|
|
|
}
|