-
Notifications
You must be signed in to change notification settings - Fork 105
/
types.go
87 lines (68 loc) · 1.97 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
package types
import (
"bytes"
"encoding/hex"
"fmt"
"sort"
"github.com/btcsuite/btcd/btcec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/axelarnetwork/axelar-core/x/multisig/exported"
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported"
"github.com/axelarnetwork/utils/funcs"
)
// Signature is an alias for signature in raw bytes
type Signature []byte
// ValidateBasic returns an error if the signature is not a valid S256 elliptic curve signature
func (sig Signature) ValidateBasic() error {
_, err := btcec.ParseDERSignature(sig, btcec.S256())
if err != nil {
return err
}
return nil
}
// Verify checks if the signature matches the payload and public key
func (sig Signature) Verify(payloadHash exported.Hash, pk exported.PublicKey) bool {
s, err := btcec.ParseDERSignature(sig, btcec.S256())
if err != nil {
return false
}
parsedKey, err := btcec.ParsePubKey(pk, btcec.S256())
if err != nil {
return false
}
return s.Verify(payloadHash[:], parsedKey)
}
// String returns the hex-encoding of signature
func (sig Signature) String() string {
return hex.EncodeToString(sig)
}
func (sig Signature) toECDSASignature() btcec.Signature {
return *funcs.Must(btcec.ParseDERSignature(sig, btcec.S256()))
}
func sortAddresses[T sdk.Address](addrs []T) []T {
sorted := make([]T, len(addrs))
copy(sorted, addrs)
sort.SliceStable(sorted, func(i, j int) bool { return bytes.Compare(sorted[i].Bytes(), sorted[j].Bytes()) < 0 })
return sorted
}
// NewKeyEpoch is the constructor for key rotation
func NewKeyEpoch(epoch uint64, chain nexus.ChainName, keyID exported.KeyID) KeyEpoch {
return KeyEpoch{
Epoch: epoch,
Chain: chain,
KeyID: keyID,
}
}
// ValidateBasic returns an error if the key epoch is invalid
func (m KeyEpoch) ValidateBasic() error {
if m.Epoch == 0 {
return fmt.Errorf("epoch must be >0")
}
if err := m.Chain.Validate(); err != nil {
return err
}
if err := m.KeyID.ValidateBasic(); err != nil {
return err
}
return nil
}