-
Notifications
You must be signed in to change notification settings - Fork 57
/
msgs.go
291 lines (238 loc) · 8.89 KB
/
msgs.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package types
import (
"fmt"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
)
var (
_ sdk.Msg = &MsgDelegateKeys{}
_ sdk.Msg = &MsgSendToEthereum{}
_ sdk.Msg = &MsgCancelSendToEthereum{}
_ sdk.Msg = &MsgSubmitEthereumEvent{}
_ sdk.Msg = &MsgSubmitEthereumTxConfirmation{}
_ sdk.Msg = &MsgEthereumHeightVote{}
_ cdctypes.UnpackInterfacesMessage = &MsgSubmitEthereumEvent{}
_ cdctypes.UnpackInterfacesMessage = &MsgSubmitEthereumTxConfirmation{}
_ cdctypes.UnpackInterfacesMessage = &EthereumEventVoteRecord{}
)
// NewMsgDelegateKeys returns a reference to a new MsgDelegateKeys.
func NewMsgDelegateKeys(val sdk.ValAddress, orchAddr sdk.AccAddress, ethAddr string, ethSig []byte) *MsgDelegateKeys {
return &MsgDelegateKeys{
ValidatorAddress: val.String(),
OrchestratorAddress: orchAddr.String(),
EthereumAddress: ethAddr,
EthSignature: ethSig,
}
}
// Route should return the name of the module
func (msg MsgDelegateKeys) Route() string { return RouterKey }
// Type should return the action
func (msg MsgDelegateKeys) Type() string { return "delegate_keys" }
// ValidateBasic performs stateless checks
func (msg MsgDelegateKeys) ValidateBasic() (err error) {
if _, err = sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.ValidatorAddress)
}
if _, err = sdk.AccAddressFromBech32(msg.OrchestratorAddress); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.OrchestratorAddress)
}
if !common.IsHexAddress(msg.EthereumAddress) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "ethereum address")
}
if len(msg.EthSignature) == 0 {
return ErrEmptyEthSig
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgDelegateKeys) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgDelegateKeys) GetSigners() []sdk.AccAddress {
acc, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sdk.AccAddress(acc)}
}
// Route should return the name of the module
func (msg MsgSubmitEthereumEvent) Route() string { return RouterKey }
// Type should return the action
func (msg MsgSubmitEthereumEvent) Type() string { return "submit_ethereum_event" }
// ValidateBasic performs stateless checks
func (msg MsgSubmitEthereumEvent) ValidateBasic() (err error) {
if _, err = sdk.AccAddressFromBech32(msg.Signer); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer)
}
event, err := UnpackEvent(msg.Event)
if err != nil {
return err
}
return event.Validate()
}
// GetSignBytes encodes the message for signing
func (msg MsgSubmitEthereumEvent) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgSubmitEthereumEvent) GetSigners() []sdk.AccAddress {
// TODO: figure out how to convert between AccAddress and ValAddress properly
acc, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{acc}
}
func (msg MsgSubmitEthereumEvent) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
var event EthereumEvent
return unpacker.UnpackAny(msg.Event, &event)
}
// Route should return the name of the module
func (msg MsgSubmitEthereumTxConfirmation) Route() string { return RouterKey }
// Type should return the action
func (msg MsgSubmitEthereumTxConfirmation) Type() string { return "submit_ethereum_signature" }
// ValidateBasic performs stateless checks
func (msg MsgSubmitEthereumTxConfirmation) ValidateBasic() (err error) {
if _, err = sdk.AccAddressFromBech32(msg.Signer); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer)
}
event, err := UnpackConfirmation(msg.Confirmation)
if err != nil {
return err
}
return event.Validate()
}
// GetSignBytes encodes the message for signing
func (msg MsgSubmitEthereumTxConfirmation) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgSubmitEthereumTxConfirmation) GetSigners() []sdk.AccAddress {
// TODO: figure out how to convert between AccAddress and ValAddress properly
acc, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{acc}
}
func (msg MsgSubmitEthereumTxConfirmation) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
var sig EthereumTxConfirmation
return unpacker.UnpackAny(msg.Confirmation, &sig)
}
// NewMsgSendToEthereum returns a new MsgSendToEthereum
func NewMsgSendToEthereum(sender sdk.AccAddress, destAddress string, send sdk.Coin, bridgeFee sdk.Coin) *MsgSendToEthereum {
return &MsgSendToEthereum{
Sender: sender.String(),
EthereumRecipient: destAddress,
Amount: send,
BridgeFee: bridgeFee,
}
}
// Route should return the name of the module
func (msg MsgSendToEthereum) Route() string { return RouterKey }
// Type should return the action
func (msg MsgSendToEthereum) Type() string { return "send_to_eth" }
// ValidateBasic runs stateless checks on the message
// Checks if the Eth address is valid
func (msg MsgSendToEthereum) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Sender)
}
// fee and send must be of the same denom
// this check is VERY IMPORTANT
if msg.Amount.Denom != msg.BridgeFee.Denom {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins,
fmt.Sprintf("fee and amount must be the same type %s != %s", msg.Amount.Denom, msg.BridgeFee.Denom))
}
if !msg.Amount.IsValid() || msg.Amount.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "amount")
}
if !msg.BridgeFee.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "fee")
}
if !common.IsHexAddress(msg.EthereumRecipient) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "ethereum address")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgSendToEthereum) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgSendToEthereum) GetSigners() []sdk.AccAddress {
acc, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{acc}
}
// NewMsgCancelSendToEthereum returns a new MsgCancelSendToEthereum
func NewMsgCancelSendToEthereum(id uint64, orchestrator sdk.AccAddress) *MsgCancelSendToEthereum {
return &MsgCancelSendToEthereum{
Id: id,
Sender: orchestrator.String(),
}
}
// Route should return the name of the module
func (msg MsgCancelSendToEthereum) Route() string { return RouterKey }
// Type should return the action
func (msg MsgCancelSendToEthereum) Type() string { return "cancel_send_to_ethereum" }
// ValidateBasic performs stateless checks
func (msg MsgCancelSendToEthereum) ValidateBasic() error {
if msg.Id == 0 {
return sdkerrors.Wrap(ErrInvalid, "Id cannot be 0")
}
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Sender)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgCancelSendToEthereum) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgCancelSendToEthereum) GetSigners() []sdk.AccAddress {
acc, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{acc}
}
// NewMsgEthereumHeightVote returns a new MsgEthereumHeightVote
func NewMsgEthereumHeightVote(ethereumHeight uint64, signer sdk.AccAddress) *MsgEthereumHeightVote {
return &MsgEthereumHeightVote{
EthereumHeight: ethereumHeight,
Signer: signer.String(),
}
}
// Route should return the name of the module
func (msg MsgEthereumHeightVote) Route() string { return RouterKey }
// Type should return the action
func (msg MsgEthereumHeightVote) Type() string { return "ethereum_height_vote" }
// ValidateBasic performs stateless checks
func (msg MsgEthereumHeightVote) ValidateBasic() error {
if msg.EthereumHeight == 0 {
return sdkerrors.Wrap(ErrInvalid, "ethereum height cannot be 0")
}
if _, err := sdk.AccAddressFromBech32(msg.Signer); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgEthereumHeightVote) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgEthereumHeightVote) GetSigners() []sdk.AccAddress {
acc, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{acc}
}