-
Notifications
You must be signed in to change notification settings - Fork 45
/
msgs_app_links.go
141 lines (118 loc) · 4 KB
/
msgs_app_links.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/hex"
"strings"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
)
// NewMsgLinkApplication creates a new MsgLinkApplication instance
//
//nolint:interfacer
func NewMsgLinkApplication(
linkData Data, callData string, sender string,
sourcePort, sourceChannel string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64,
) *MsgLinkApplication {
return &MsgLinkApplication{
Sender: sender,
LinkData: linkData,
CallData: callData,
SourcePort: sourcePort,
SourceChannel: sourceChannel,
TimeoutHeight: timeoutHeight,
TimeoutTimestamp: timeoutTimestamp,
}
}
// Route implements sdk.Msg
func (MsgLinkApplication) Route() string {
return RouterKey
}
// Type implements sdk.Msg
func (MsgLinkApplication) Type() string {
return ActionLinkApplication
}
// ValidateBasic performs a basic check of the MsgLinkApplication fields.
// NOTE: timeout height or timestamp values can be 0 to disable the timeout.
func (msg *MsgLinkApplication) ValidateBasic() error {
err := msg.LinkData.Validate()
if err != nil {
return errors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
if _, err := hex.DecodeString(msg.CallData); err != nil {
return errors.Wrap(sdkerrors.ErrInvalidRequest, "invalid call data: must be hex encoded")
}
err = host.ChannelIdentifierValidator(msg.SourceChannel)
if err != nil {
return errors.Wrap(err, "invalid source channel ID")
}
err = host.PortIdentifierValidator(msg.SourcePort)
if err != nil {
return errors.Wrap(err, "invalid source port ID")
}
_, err = sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return nil
}
// GetSignBytes implements sdk.Msg.
func (msg *MsgLinkApplication) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners implements sdk.Msg
func (msg *MsgLinkApplication) GetSigners() []sdk.AccAddress {
valAddr, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{valAddr}
}
// -------------------------------------------------------------------------------------------------------------------
// NewMsgUnlinkApplication creates a new MsgUnlinkApplication instance
//
//nolint:interfacer
func NewMsgUnlinkApplication(application, username string, signer string) *MsgUnlinkApplication {
return &MsgUnlinkApplication{
Application: application,
Username: username,
Signer: signer,
}
}
// Route implements sdk.Msg
func (MsgUnlinkApplication) Route() string {
return RouterKey
}
// Type implements sdk.Msg
func (MsgUnlinkApplication) Type() string {
return ActionUnlinkApplication
}
// ValidateBasic performs a basic check of the MsgUnlinkApplication fields.
// NOTE: timeout height or timestamp values can be 0 to disable the timeout.
func (msg *MsgUnlinkApplication) ValidateBasic() error {
if len(strings.TrimSpace(msg.Application)) == 0 {
return errors.Wrap(sdkerrors.ErrInvalidRequest, "application cannot be empty or blank")
}
if len(strings.TrimSpace(msg.Username)) == 0 {
return errors.Wrap(sdkerrors.ErrInvalidRequest, "username cannot be empty or blank")
}
// NOTE: sender format must be validated as it is required by the GetSigners function.
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return nil
}
// GetSignBytes implements sdk.Msg.
func (msg *MsgUnlinkApplication) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners implements sdk.Msg
func (msg *MsgUnlinkApplication) GetSigners() []sdk.AccAddress {
valAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{valAddr}
}