manifest: move Group code into a file of its own

This commit is contained in:
Roman Khimov 2021-02-08 13:28:49 +03:00
parent 1a4958b1d5
commit 296c7a10fc
2 changed files with 95 additions and 88 deletions

View file

@ -0,0 +1,95 @@
package manifest
import (
"crypto/elliptic"
"encoding/hex"
"encoding/json"
"errors"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// 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 {
PublicKey *keys.PublicKey `json:"pubkey"`
Signature []byte `json:"signature"`
}
type groupAux struct {
PublicKey string `json:"pubkey"`
Signature []byte `json:"signature"`
}
// IsValid checks whether group's signature corresponds to the given hash.
func (g *Group) IsValid(h util.Uint160) error {
if !g.PublicKey.Verify(g.Signature, hash.Sha256(h.BytesBE()).BytesBE()) {
return errors.New("incorrect group signature")
}
return nil
}
// 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
}
// ToStackItem converts Group to stackitem.Item.
func (g *Group) ToStackItem() stackitem.Item {
return stackitem.NewStruct([]stackitem.Item{
stackitem.NewByteArray(g.PublicKey.Bytes()),
stackitem.NewByteArray(g.Signature),
})
}
// FromStackItem converts stackitem.Item to Group.
func (g *Group) FromStackItem(item stackitem.Item) error {
if item.Type() != stackitem.StructT {
return errors.New("invalid Group stackitem type")
}
group := item.Value().([]stackitem.Item)
if len(group) != 2 {
return errors.New("invalid Group stackitem length")
}
pKey, err := group[0].TryBytes()
if err != nil {
return err
}
g.PublicKey, err = keys.NewPublicKeyFromBytes(pKey, elliptic.P256())
if err != nil {
return err
}
sig, err := group[1].TryBytes()
if err != nil {
return err
}
g.Signature = sig
return nil
}

View file

@ -1,15 +1,9 @@
package manifest
import (
"crypto/elliptic"
"encoding/hex"
"encoding/json"
"errors"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
@ -25,19 +19,6 @@ type Event struct {
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 {
PublicKey *keys.PublicKey `json:"pubkey"`
Signature []byte `json:"signature"`
}
type groupAux struct {
PublicKey string `json:"pubkey"`
Signature []byte `json:"signature"`
}
// Method represents method's metadata.
type Method struct {
Name string `json:"name"`
@ -55,75 +36,6 @@ func NewParameter(name string, typ smartcontract.ParamType) Parameter {
}
}
// IsValid checks whether group's signature corresponds to the given hash.
func (g *Group) IsValid(h util.Uint160) error {
if !g.PublicKey.Verify(g.Signature, hash.Sha256(h.BytesBE()).BytesBE()) {
return errors.New("incorrect group signature")
}
return nil
}
// 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
}
// ToStackItem converts Group to stackitem.Item.
func (g *Group) ToStackItem() stackitem.Item {
return stackitem.NewStruct([]stackitem.Item{
stackitem.NewByteArray(g.PublicKey.Bytes()),
stackitem.NewByteArray(g.Signature),
})
}
// FromStackItem converts stackitem.Item to Group.
func (g *Group) FromStackItem(item stackitem.Item) error {
if item.Type() != stackitem.StructT {
return errors.New("invalid Group stackitem type")
}
group := item.Value().([]stackitem.Item)
if len(group) != 2 {
return errors.New("invalid Group stackitem length")
}
pKey, err := group[0].TryBytes()
if err != nil {
return err
}
g.PublicKey, err = keys.NewPublicKeyFromBytes(pKey, elliptic.P256())
if err != nil {
return err
}
sig, err := group[1].TryBytes()
if err != nil {
return err
}
g.Signature = sig
return nil
}
// ToStackItem converts Method to stackitem.Item.
func (m *Method) ToStackItem() stackitem.Item {
params := make([]stackitem.Item, len(m.Parameters))