-
Notifications
You must be signed in to change notification settings - Fork 113
/
types.go
161 lines (138 loc) · 3.73 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
package types
import (
"bytes"
"crypto/ecdsa"
"github.com/axelarnetwork/axelar-core/x/tss/exported"
"github.com/btcsuite/btcd/btcec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
// TSSEnabled indicates if threshold signing is permitted
TSSEnabled bool = false
)
// 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
GetSigKeyPairs() []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 {
for _, d := range info.Data {
data = append(data, d)
}
}
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
}
// GetSigKeyPairs returns list of pub key and signature pairs
func (m MultisigInfo) GetSigKeyPairs() []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)
}
}
return pairs
}