-
Notifications
You must be signed in to change notification settings - Fork 76
/
tx.go
162 lines (124 loc) · 4.06 KB
/
tx.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
package types
import (
"fmt"
"cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
)
// const interchainAccountIDLimit = 47
const interchainAccountIDLimit = 128 -
len("icacontroller-") -
len("neutron1unyuj8qnmygvzuex3dwmg9yzt9alhvyeat0uu0jedg2wj33efl5qmysp02") - // just a random contract address
len(".")
var _ codectypes.UnpackInterfacesMessage = &MsgSubmitTx{}
func (msg *MsgRegisterInterchainAccount) Validate() error {
if len(msg.ConnectionId) == 0 {
return ErrEmptyConnectionID
}
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to parse FromAddress: %s", msg.FromAddress)
}
if len(msg.InterchainAccountId) == 0 {
return ErrEmptyInterchainAccountID
}
if len(msg.InterchainAccountId) > interchainAccountIDLimit {
return errors.Wrapf(ErrLongInterchainAccountID, "max length is %d, got %d", interchainAccountIDLimit, len(msg.InterchainAccountId))
}
return nil
}
func (msg *MsgRegisterInterchainAccount) GetSigners() []sdk.AccAddress {
fromAddress, _ := sdk.AccAddressFromBech32(msg.FromAddress)
return []sdk.AccAddress{fromAddress}
}
func (msg *MsgRegisterInterchainAccount) Route() string {
return RouterKey
}
func (msg *MsgRegisterInterchainAccount) Type() string {
return "register-interchain-account"
}
func (msg *MsgRegisterInterchainAccount) GetSignBytes() []byte {
return ModuleCdc.MustMarshalJSON(msg)
}
//----------------------------------------------------------------
func (msg *MsgSubmitTx) Validate() error {
if err := msg.Fee.Validate(); err != nil {
return err
}
if len(msg.ConnectionId) == 0 {
return ErrEmptyConnectionID
}
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to parse FromAddress: %s", msg.FromAddress)
}
if len(msg.InterchainAccountId) == 0 {
return ErrEmptyInterchainAccountID
}
if len(msg.Msgs) == 0 {
return ErrNoMessages
}
if msg.Timeout <= 0 {
return errors.Wrapf(ErrInvalidTimeout, "timeout must be greater than zero")
}
return nil
}
func (msg *MsgSubmitTx) GetSigners() []sdk.AccAddress {
fromAddress, _ := sdk.AccAddressFromBech32(msg.FromAddress)
return []sdk.AccAddress{fromAddress}
}
func (msg *MsgSubmitTx) Route() string {
return RouterKey
}
func (msg *MsgSubmitTx) Type() string {
return "submit-tx"
}
func (msg *MsgSubmitTx) GetSignBytes() []byte {
return ModuleCdc.MustMarshalJSON(msg)
}
// PackTxMsgAny marshals the sdk.Msg payload to a protobuf Any type
func PackTxMsgAny(sdkMsg sdk.Msg) (*codectypes.Any, error) {
msg, ok := sdkMsg.(proto.Message)
if !ok {
return nil, fmt.Errorf("can't proto marshal %T", sdkMsg)
}
value, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
return value, nil
}
// implements UnpackInterfacesMessage.UnpackInterfaces (https://github.com/cosmos/cosmos-sdk/blob/d07d35f29e0a0824b489c552753e8798710ff5a8/codec/types/interface_registry.go#L60)
func (msg *MsgSubmitTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var sdkMsg sdk.Msg
for _, m := range msg.Msgs {
if err := unpacker.UnpackAny(m, &sdkMsg); err != nil {
return err
}
}
return nil
}
//----------------------------------------------------------------
var _ sdk.Msg = &MsgUpdateParams{}
func (msg *MsgUpdateParams) Route() string {
return RouterKey
}
func (msg *MsgUpdateParams) Type() string {
return "update-params"
}
func (msg *MsgUpdateParams) GetSigners() []sdk.AccAddress {
authority, err := sdk.AccAddressFromBech32(msg.Authority)
if err != nil { // should never happen as valid basic rejects invalid addresses
panic(err.Error())
}
return []sdk.AccAddress{authority}
}
func (msg *MsgUpdateParams) GetSignBytes() []byte {
return ModuleCdc.MustMarshalJSON(msg)
}
func (msg *MsgUpdateParams) Validate() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errors.Wrap(err, "authority is invalid")
}
return nil
}