-
Notifications
You must be signed in to change notification settings - Fork 115
/
types.go
110 lines (87 loc) · 2.81 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
package tofnd
import (
"fmt"
"github.com/btcsuite/btcd/btcec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Validate checks if the criminal list is valid
func (m *MessageOut_CriminalList) Validate() error {
if len(m.Criminals) == 0 {
return fmt.Errorf("missing criminals")
}
criminalSeen := make(map[string]bool)
for i, criminal := range m.Criminals {
if criminalSeen[criminal.String()] {
return fmt.Errorf("duplicate criminal %s", criminal.String())
}
_, err := sdk.ValAddressFromBech32(criminal.GetPartyUid())
if err != nil {
return fmt.Errorf("invalid criminal address %s", criminal.GetPartyUid())
}
if criminal.CrimeType != CRIME_TYPE_MALICIOUS && criminal.CrimeType != CRIME_TYPE_NON_MALICIOUS {
return fmt.Errorf("invalid crime type %s", criminal.CrimeType.String())
}
if i < len(m.Criminals)-1 && !m.Less(i, i+1) {
return fmt.Errorf("criminals have to be sorted in ascending order")
}
criminalSeen[criminal.String()] = true
}
return nil
}
// Validate checks if the sign result is valid
func (m *MessageOut_SignResult) Validate() error {
if signature := m.GetSignature(); signature != nil {
if _, err := btcec.ParseDERSignature(signature, btcec.S256()); err != nil {
return err
}
return nil
}
if criminalList := m.GetCriminals(); criminalList != nil {
if err := criminalList.Validate(); err != nil {
return err
}
return nil
}
return fmt.Errorf("missing signature or criminals")
}
// Validate checks if the keygen result is valid
func (m *MessageOut_KeygenResult) Validate() error {
if keygenData := m.GetData(); keygenData != nil {
pubKeyBytes := keygenData.GetPubKey()
if pubKeyBytes == nil {
return fmt.Errorf("pubkey is nil")
}
groupRecoverInfo := keygenData.GetGroupRecoverInfo()
if groupRecoverInfo == nil {
return fmt.Errorf("group recovery info is nil")
}
privateRecoverInfo := keygenData.GetPrivateRecoverInfo()
if privateRecoverInfo == nil {
return fmt.Errorf("private recovery info is nil")
}
_, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256())
if err != nil {
return err
}
return nil
}
if criminalList := m.GetCriminals(); criminalList != nil {
if err := criminalList.Validate(); err != nil {
return err
}
return nil
}
return fmt.Errorf("missing pubkey or criminals")
}
// Len returns the number of criminals in the criminal list
func (m *MessageOut_CriminalList) Len() int {
return len(m.Criminals)
}
// Swap swaps the criminals at given indexes in the criminal list
func (m *MessageOut_CriminalList) Swap(i, j int) {
m.Criminals[i], m.Criminals[j] = m.Criminals[j], m.Criminals[i]
}
// Less returns true if the criminal at index i is considered less than the criminal at index j in the criminal list
func (m *MessageOut_CriminalList) Less(i, j int) bool {
return m.Criminals[i].String() < m.Criminals[j].String()
}