-
Notifications
You must be signed in to change notification settings - Fork 580
/
header.go
60 lines (46 loc) · 1.86 KB
/
header.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
package solomachine
import (
"strings"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)
// SentinelHeaderPath defines a placeholder path value used for headers in solomachine client updates
const SentinelHeaderPath = "solomachine:header"
var _ exported.ClientMessage = &Header{}
// ClientType defines that the Header is a Solo Machine.
func (Header) ClientType() string {
return exported.Solomachine
}
// GetPubKey unmarshals the new public key into a cryptotypes.PubKey type.
// An error is returned if the new public key is nil or the cached value
// is not a PubKey.
func (h Header) GetPubKey() (cryptotypes.PubKey, error) {
if h.NewPublicKey == nil {
return nil, sdkerrors.Wrap(ErrInvalidHeader, "header NewPublicKey cannot be nil")
}
publicKey, ok := h.NewPublicKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, sdkerrors.Wrap(ErrInvalidHeader, "header NewPublicKey is not cryptotypes.PubKey")
}
return publicKey, nil
}
// ValidateBasic ensures that the timestamp, signature and public key have all
// been initialized.
func (h Header) ValidateBasic() error {
if h.Timestamp == 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "timestamp cannot be zero")
}
if h.NewDiversifier != "" && strings.TrimSpace(h.NewDiversifier) == "" {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "diversifier cannot contain only spaces")
}
if len(h.Signature) == 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "signature cannot be empty")
}
newPublicKey, err := h.GetPubKey()
if err != nil || newPublicKey == nil || len(newPublicKey.Bytes()) == 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "new public key cannot be empty")
}
return nil
}