-
Notifications
You must be signed in to change notification settings - Fork 77
/
group.go
144 lines (132 loc) · 3.32 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package manifest
import (
"crypto/elliptic"
"encoding/hex"
"encoding/json"
"errors"
"sort"
"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 its hash to prove
// it belongs to the group.
type Group struct {
PublicKey *keys.PublicKey `json:"pubkey"`
Signature []byte `json:"signature"`
}
// Groups is just an array of Group.
type Groups []Group
type groupAux struct {
PublicKey string `json:"pubkey"`
Signature []byte `json:"signature"`
}
// IsValid checks whether the 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
}
// AreValid checks for groups correctness and uniqueness.
// If the contract hash is empty, then hash-related checks are omitted.
func (g Groups) AreValid(h util.Uint160) error {
if !h.Equals(util.Uint160{}) {
for i := range g {
err := g[i].IsValid(h)
if err != nil {
return err
}
}
}
if len(g) < 2 {
return nil
}
pkeys := make(keys.PublicKeys, len(g))
for i := range g {
pkeys[i] = g[i].PublicKey
}
sort.Sort(pkeys)
for i := range pkeys {
if i == 0 {
continue
}
if pkeys[i].Cmp(pkeys[i-1]) == 0 {
return errors.New("duplicate group keys")
}
}
return nil
}
func (g Groups) Contains(k *keys.PublicKey) bool {
for i := range g {
if k.Equal(g[i].PublicKey) {
return true
}
}
return false
}
// MarshalJSON implements the 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 the 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
if len(aux.Signature) != keys.SignatureLen {
return errors.New("wrong signature length")
}
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
}
if len(sig) != keys.SignatureLen {
return errors.New("wrong signature length")
}
g.Signature = sig
return nil
}