-
Notifications
You must be signed in to change notification settings - Fork 105
/
msg.go
141 lines (123 loc) · 4.44 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
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
package types
import (
"encoding/json"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// provider message types
const (
TypeMsgAssignConsumerKey = "assign_consumer_key"
TypeMsgRegisterConsumerRewardDenom = "register_consumer_reward_denom"
)
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
}
// NewMsgRegisterConsumerRewardDenom returns a new MsgRegisterConsumerRewardDenom with a sender and
// a funding amount.
func NewMsgRegisterConsumerRewardDenom(denom string, depositor sdk.AccAddress) *MsgRegisterConsumerRewardDenom {
return &MsgRegisterConsumerRewardDenom{
Denom: denom,
Depositor: depositor.String(),
}
}
// Route returns the MsgRegisterConsumerRewardDenom message route.
func (msg MsgRegisterConsumerRewardDenom) Route() string { return ModuleName }
// Type returns the MsgRegisterConsumerRewardDenom message type.
func (msg MsgRegisterConsumerRewardDenom) Type() string { return TypeMsgRegisterConsumerRewardDenom }
// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
func (msg MsgRegisterConsumerRewardDenom) GetSigners() []sdk.AccAddress {
depoAddr, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
panic(err)
}
return []sdk.AccAddress{depoAddr}
}
// GetSignBytes returns the raw bytes for a MsgRegisterConsumerRewardDenom message that
// the expected signer needs to sign.
func (msg MsgRegisterConsumerRewardDenom) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// ValidateBasic performs basic MsgRegisterConsumerRewardDenom message validation.
func (msg MsgRegisterConsumerRewardDenom) ValidateBasic() error {
if !sdk.NewCoin(msg.Denom, sdk.NewInt(0)).IsValid() {
return ErrInvalidConsumerRewardDenom
}
_, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
return ErrInvalidDepositorAddress
}
return nil
}