-
Notifications
You must be signed in to change notification settings - Fork 108
/
msg.go
95 lines (84 loc) · 2.81 KB
/
msg.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
package types
import (
"encoding/json"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// provider message types
const (
TypeMsgAssignConsumerKey = "assign_consumer_key"
)
var _ sdk.Msg = &MsgAssignConsumerKey{}
// NewMsgAssignConsumerKey creates a new MsgAssignConsumerKey instance.
// Delegator address and validator address are the same.
func NewMsgAssignConsumerKey(chainID string, providerValidatorAddress sdk.ValAddress,
consumerConsensusPubKey string,
) (*MsgAssignConsumerKey, error) {
return &MsgAssignConsumerKey{
ChainId: chainID,
ProviderAddr: providerValidatorAddress.String(),
ConsumerKey: consumerConsensusPubKey,
}, nil
}
// Route implements the sdk.Msg interface.
func (msg MsgAssignConsumerKey) Route() string { return RouterKey }
// Type implements the sdk.Msg interface.
func (msg MsgAssignConsumerKey) Type() string {
return TypeMsgAssignConsumerKey
}
// GetSigners implements the sdk.Msg interface. It returns the address(es) that
// must sign over msg.GetSignBytes().
// If the validator address is not same as delegator's, then the validator must
// sign the msg as well.
func (msg MsgAssignConsumerKey) GetSigners() []sdk.AccAddress {
valAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
if err != nil {
// same behavior as in cosmos-sdk
panic(err)
}
return []sdk.AccAddress{valAddr.Bytes()}
}
// GetSignBytes returns the message bytes to sign over.
func (msg MsgAssignConsumerKey) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgAssignConsumerKey) ValidateBasic() error {
if strings.TrimSpace(msg.ChainId) == "" {
return ErrBlankConsumerChainID
}
// It is possible to assign keys for consumer chains that are not yet approved.
// This can only be done by a signing validator, but it is still sensible
// to limit the chainID size to prevent abuse.
// TODO: In future, a mechanism will be added to limit assigning keys to chains
// which are approved or pending approval, only.
if 128 < len(msg.ChainId) {
return ErrBlankConsumerChainID
}
_, err := sdk.ValAddressFromBech32(msg.ProviderAddr)
if err != nil {
return ErrInvalidProviderAddress
}
if msg.ConsumerKey == "" {
return ErrInvalidConsumerConsensusPubKey
}
if _, _, err := ParseConsumerKeyFromJson(msg.ConsumerKey); err != nil {
return ErrInvalidConsumerConsensusPubKey
}
return nil
}
// ParseConsumerKeyFromJson parses the consumer key from a JSON string,
// this replaces deserializing a protobuf any.
func ParseConsumerKeyFromJson(jsonStr string) (pkType, key string, err error) {
type PubKey struct {
Type string `json:"@type"`
Key string `json:"key"`
}
var pubKey PubKey
err = json.Unmarshal([]byte(jsonStr), &pubKey)
if err != nil {
return "", "", err
}
return pubKey.Type, pubKey.Key, nil
}