-
Notifications
You must be signed in to change notification settings - Fork 113
/
types.go
234 lines (195 loc) · 5.26 KB
/
types.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package types
import (
"bytes"
"crypto/ecdsa"
"fmt"
"github.com/btcsuite/btcd/btcec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/axelarnetwork/axelar-core/utils"
"github.com/axelarnetwork/axelar-core/x/tss/exported"
)
const (
// TSSEnabled indicates if threshold signing is permitted
TSSEnabled bool = false
)
// Validate validates the ValidatorStatus
func (m ValidatorStatus) Validate() error {
if err := sdk.VerifyAddressFormat(m.Validator); err != nil {
return err
}
return nil
}
// Validate validates the MultisigInfo
func (m MultisigInfo) Validate() error {
if err := utils.ValidateString(m.ID); err != nil {
return sdkerrors.Wrap(err, "invalid ID")
}
if m.Timeout <= 0 {
return fmt.Errorf("timeout must be >0")
}
if m.TargetNum <= 0 {
return fmt.Errorf("target num must be >0")
}
return nil
}
// Validate validates the ExternalKeys
func (m ExternalKeys) Validate() error {
if err := m.Chain.Validate(); err != nil {
return sdkerrors.Wrap(err, "invalid chain")
}
if len(m.KeyIDs) == 0 {
return fmt.Errorf("key IDs must be set")
}
for _, keyID := range m.KeyIDs {
if err := keyID.Validate(); err != nil {
return err
}
}
return nil
}
// Validate validates the KeyRecoveryInfo
func (m KeyRecoveryInfo) Validate() error {
if err := m.KeyID.Validate(); err != nil {
return err
}
if len(m.Public) == 0 {
return fmt.Errorf("missing public key recovery info")
}
if len(m.Private) == 0 {
return fmt.Errorf("missing private key recovery info")
}
for validator, privateKeyRecoveryInfo := range m.Private {
if len(privateKeyRecoveryInfo) == 0 {
return fmt.Errorf("missing private key recovery info for validator %s", validator)
}
}
return nil
}
// MultisigBaseInfo is an interface for multisig base info
type MultisigBaseInfo interface {
HasData(k []byte) bool
AddData(val sdk.ValAddress, data [][]byte)
GetData() [][]byte
IsCompleted() bool
Count() int64
DoesParticipate(val sdk.ValAddress) bool
GetTimeoutBlock() int64
}
// MultisigKeygenInfo is an interface for multisig keygen info
type MultisigKeygenInfo interface {
MultisigBaseInfo
GetKeys() []ecdsa.PublicKey
GetPubKeysByValidator(val sdk.ValAddress) []ecdsa.PublicKey
}
// MultisigSignInfo is an interface for multisig sign info
type MultisigSignInfo interface {
MultisigBaseInfo
GetTargetSigKeyPairs() []exported.SigKeyPair
}
// HasData checks duplicate data
func (m MultisigInfo) HasData(k []byte) bool {
for _, info := range m.Infos {
for _, d := range info.Data {
if res := bytes.Compare(k, d); res == 0 {
return true
}
}
}
return false
}
// AddData add list of data for a participant
func (m *MultisigInfo) AddData(val sdk.ValAddress, data [][]byte) {
for _, info := range m.Infos {
if val.Equals(info.Participant) {
info.Data = append(info.Data, data...)
return
}
}
m.Infos = append(m.Infos, &MultisigInfo_Info{Participant: val, Data: data})
}
// GetData returns list of data
func (m *MultisigInfo) GetData() [][]byte {
var data [][]byte
for _, info := range m.Infos {
data = append(data, info.Data...)
}
return data
}
// IsCompleted returns true if number of data reaches target
func (m MultisigInfo) IsCompleted() bool {
return m.Count() >= m.TargetNum
}
// Count returns current number of data
func (m MultisigInfo) Count() int64 {
l := 0
for _, info := range m.Infos {
l += len(info.Data)
}
return int64(l)
}
// DoesParticipate returns true if the validator submitted its data
func (m MultisigInfo) DoesParticipate(val sdk.ValAddress) bool {
for _, info := range m.Infos {
if val.Equals(info.Participant) {
return true
}
}
return false
}
// GetTimeoutBlock returns multisig info timeout height
func (m MultisigInfo) GetTimeoutBlock() int64 {
return m.Timeout
}
// GetKeys returns list of all pub keys
func (m MultisigInfo) GetKeys() []ecdsa.PublicKey {
var pubKeys []ecdsa.PublicKey
for _, info := range m.Infos {
for _, pubKey := range info.Data {
btcecPK, err := btcec.ParsePubKey(pubKey, btcec.S256())
// the setter is controlled by the keeper alone, so an error here should be a catastrophic failure
if err != nil {
panic(err)
}
pk := btcecPK.ToECDSA()
pubKeys = append(pubKeys, *pk)
}
}
return pubKeys
}
// GetPubKeysByValidator returns pub keys a validator submitted
func (m MultisigInfo) GetPubKeysByValidator(val sdk.ValAddress) []ecdsa.PublicKey {
var pubKeys []ecdsa.PublicKey
for _, info := range m.Infos {
if val.Equals(info.Participant) {
for _, pubKey := range info.Data {
btcecPK, err := btcec.ParsePubKey(pubKey, btcec.S256())
// the setter is controlled by the keeper alone, so an error here should be a catastrophic failure
if err != nil {
panic(err)
}
pk := btcecPK.ToECDSA()
pubKeys = append(pubKeys, *pk)
}
}
}
return pubKeys
}
// GetTargetSigKeyPairs returns list of pub key and signature pairs
func (m MultisigInfo) GetTargetSigKeyPairs() []exported.SigKeyPair {
var pairs []exported.SigKeyPair
for _, info := range m.Infos {
for _, sigKeyPair := range info.Data {
var pair exported.SigKeyPair
err := pair.Unmarshal(sigKeyPair)
if err != nil {
panic(err)
}
pairs = append(pairs, pair)
if len(pairs) >= int(m.TargetNum) {
return pairs
}
}
}
return pairs
}