-
Notifications
You must be signed in to change notification settings - Fork 44
/
cli_app_links.go
243 lines (204 loc) · 7.8 KB
/
cli_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
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
package cli
import (
"context"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channelutils "github.com/cosmos/ibc-go/v3/modules/core/04-channel/client/utils"
"github.com/desmos-labs/desmos/v4/x/profiles/types"
)
const (
flagPacketTimeoutHeight = "packet-timeout-height"
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
flagAbsoluteTimeouts = "absolute-timeouts"
)
// GetCmdLinkApplication returns the command to create a NewMsgLinkApplication transaction
func GetCmdLinkApplication() *cobra.Command {
cmd := &cobra.Command{
Use: "link-app [src-port] [src-channel] [application] [username] [verification-call-data]",
Short: "Link a centralized application account to your Desmos profile",
Long: strings.TrimSpace(`Connect a Desmos profile to a centralized social network account through IBC.
Timeouts can be specified as absolute or relative using the "absolute-timeouts" flag.
Timeout height can be set by passing in the height string in the form {revision}-{height} using the "packet-timeout-height" flag.
Relative timeouts are added to the block height and block timestamp queried from the latest consensus state corresponding
to the counterparty channel. Any timeout set to 0 is disabled.`),
Example: fmt.Sprintf(
`%s tx profiles link-app "profiles" "channel-0" "twitter" "twitter_user" "7B22757365726E616D65223A22526963636172"`,
version.AppName),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
sender := clientCtx.GetFromAddress()
srcPort := args[0]
srcChannel := args[1]
linkData := types.NewData(args[2], args[3])
timeoutHeightStr, err := cmd.Flags().GetString(flagPacketTimeoutHeight)
if err != nil {
return err
}
timeoutHeight, err := clienttypes.ParseHeight(timeoutHeightStr)
if err != nil {
return err
}
timeoutTimestamp, err := cmd.Flags().GetUint64(flagPacketTimeoutTimestamp)
if err != nil {
return err
}
absoluteTimeouts, err := cmd.Flags().GetBool(flagAbsoluteTimeouts)
if err != nil {
return err
}
// if the timeouts are not absolute, retrieve latest block height and block timestamp
// for the consensus state connected to the destination port/channel
if !absoluteTimeouts {
consensusState, height, _, err := channelutils.QueryLatestConsensusState(clientCtx, srcPort, srcChannel)
if err != nil {
return err
}
if !timeoutHeight.IsZero() {
absoluteHeight := height
absoluteHeight.RevisionNumber += timeoutHeight.RevisionNumber
absoluteHeight.RevisionHeight += timeoutHeight.RevisionHeight
timeoutHeight = absoluteHeight
}
if timeoutTimestamp != 0 {
timeoutTimestamp = consensusState.GetTimestamp() + timeoutTimestamp
}
}
msg := types.NewMsgLinkApplication(
linkData, args[4], sender.String(),
srcPort, srcChannel, timeoutHeight, timeoutTimestamp,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(flagPacketTimeoutHeight, types.DefaultRelativePacketTimeoutHeight, "Packet timeout block height. The timeout is disabled when set to 0-0.")
cmd.Flags().Uint64(flagPacketTimeoutTimestamp, types.DefaultRelativePacketTimeoutTimestamp, "Packet timeout timestamp in nanoseconds. Default is 10 minutes. The timeout is disabled when set to 0.")
cmd.Flags().Bool(flagAbsoluteTimeouts, false, "Timeout flags are used as absolute timeouts.")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdUnlinkApplication returns the command allowing to unlink a centralized application from a Desmos profile
func GetCmdUnlinkApplication() *cobra.Command {
cmd := &cobra.Command{
Use: "unlink-app [application] [username]",
Short: "Unlink a centralized application account from your Desmos profile",
Example: fmt.Sprintf(`%s tx profiles unlink-app "twitter" "twitter_user"`, version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
sender := clientCtx.GetFromAddress()
msg := types.NewMsgUnlinkApplication(args[0], args[1], sender.String())
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// -------------------------------------------------------------------------------------------------------------------
// GetCmdQueryApplicationsLinks returns the command allowing to query the application links, optionally associated with a user
func GetCmdQueryApplicationsLinks() *cobra.Command {
cmd := &cobra.Command{
Use: "app-links [[user]] [[application]] [[username]]",
Short: "Get all the application links with optional user address, application, username and pagination",
Example: fmt.Sprintf(`%s query profiles app-links --page=2 --limit=100
%s query profiles app-links desmos13p5pamrljhza3fp4es5m3llgmnde5fzcpq6nud
%s query profiles app-links desmos13p5pamrljhza3fp4es5m3llgmnde5fzcpq6nud "twitter"
%s query profiles app-links desmos13p5pamrljhza3fp4es5m3llgmnde5fzcpq6nud "twitter" "twitter_user"
`, version.AppName, version.AppName, version.AppName, version.AppName),
Args: cobra.RangeArgs(0, 3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
var user string
if len(args) > 0 {
user = args[0]
}
var application string
if len(args) > 1 {
application = args[1]
}
var username string
if len(args) > 2 {
username = args[2]
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
res, err := queryClient.ApplicationLinks(
context.Background(),
types.NewQueryApplicationLinksRequest(user, application, username, pageReq),
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "app links")
return cmd
}
// GetCmdQueryApplicationLinkOwners returns the command allowing to query the application link owners, optionally associated with a target
func GetCmdQueryApplicationLinkOwners() *cobra.Command {
cmd := &cobra.Command{
Use: "app-link-owners [[application]] [[username]]",
Short: "Get all the application link owners with optional application, username and pagination",
Example: fmt.Sprintf(`%s query profiles app-link-owners --page=2 --limit=100
%s query profiles app-link-owners "twitter"
%s query profiles app-link-owners "twitter" "twitter_user"
`, version.AppName, version.AppName, version.AppName),
Args: cobra.RangeArgs(0, 2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
var application string
if len(args) > 0 {
application = args[0]
}
var username string
if len(args) > 1 {
username = args[1]
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
res, err := queryClient.ApplicationLinkOwners(
context.Background(),
types.NewQueryApplicationLinkOwnersRequest(application, username, pageReq),
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "app link owners")
return cmd
}