/
cmd_wallet_handle_uri.go
292 lines (258 loc) · 8.38 KB
/
cmd_wallet_handle_uri.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
292
package client
import (
"errors"
"fmt"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/stellar1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"github.com/keybase/stellarnet"
context "golang.org/x/net/context"
)
type CmdWalletHandleURI struct {
libkb.Contextified
uri string
}
func newCmdWalletHandleURI(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
cmd := &CmdWalletHandleURI{
Contextified: libkb.NewContextified(g),
}
return cli.Command{
Name: "handle-uri",
Usage: "Handle a 'web+stellar:' URI manually",
ArgumentHelp: "<uri>",
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "handle-uri", c)
},
}
}
func (c *CmdWalletHandleURI) ParseArgv(ctx *cli.Context) error {
c.uri = ctx.Args().First()
if c.uri == "" {
return errors.New("uri required")
}
return nil
}
func (c *CmdWalletHandleURI) Run() (err error) {
defer transformStellarCLIError(&err)
cli, err := GetWalletClient(c.G())
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewIdentifyUIProtocol(c.G()),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
ui := c.G().UI.GetTerminalUI()
ui.Printf(c.yellow("Validating URI...") + "\n")
arg := stellar1.ValidateStellarURILocalArg{
InputURI: c.uri,
}
v, err := cli.ValidateStellarURILocal(context.Background(), arg)
if err != nil {
return err
}
ui.Printf(c.green("URI validated.") + "\n\n")
ui.Printf("Keybase validated the URI you submitted. It was signed by\n\n")
ui.Printf("\t%s\n\n", ColorString(c.G(), "underline", v.OriginDomain))
if v.Message != "" {
ui.Printf("Message: %q\n\n", v.Message)
}
if v.Memo != "" {
ui.Printf("Public memo: %q\n\n", v.Memo)
}
switch v.Operation {
case "pay":
return c.payOp(v)
case "tx":
return c.txOp(v)
default:
// shouldn't happen since v.Operation was validated by the service, but in case:
ui.Printf("Sorry, Keybase doesn't handle URIs with operation %q.", v.Operation)
}
return nil
}
func (c *CmdWalletHandleURI) payOp(v stellar1.ValidateStellarURIResultLocal) error {
ui := c.G().UI.GetTerminalUI()
ui.Printf("The URI is requesting that you pay\n\n")
assetDisplay := "XLM"
if v.AssetCode != "" {
assetDisplay = fmt.Sprintf("%s/%s", v.AssetCode, v.AssetIssuer)
}
if v.Amount != "" {
ui.Printf("\t%s %s", c.green(v.Amount), c.green(assetDisplay))
ui.Printf(" to:\n")
}
ui.Printf("\t%s\n\n", v.Recipient)
if v.Amount == "" {
ui.Printf("There is no amount specified in the request.\n")
ui.Printf("The asset requested is\n\n\t%s\n\n", c.green(assetDisplay))
amount, err := ui.Prompt(PromptDescriptorStellarURIAmount, fmt.Sprintf("How much %s would you like the recipient to receive? ", assetDisplay))
if err != nil {
return err
}
if _, err := stellarnet.ParseStellarAmount(amount); err != nil {
return err
}
v.Amount = amount
}
if v.CallbackURL == "" {
ui.Printf("\nIf you confirm this request, Keybase will create a payment\n")
ui.Printf("transaction, sign it with your primary account, and submit it\n")
ui.Printf("to the Stellar network.\n\n")
if err := ui.PromptForConfirmation(fmt.Sprintf("Send %s %s to %s?", c.green(v.Amount), c.green(assetDisplay), c.yellow(v.Recipient))); err != nil {
return err
}
} else {
ui.Printf("\nIf you confirm this request, Keybase will create a payment\n")
ui.Printf("transaction, sign it with your primary account, and send it\n")
ui.Printf("to the following URL for processing:\n\n")
ui.Printf("\t%s\n\n", v.CallbackURL)
ui.Printf("Note: Keybase will NOT be submitting the transaction to the Stellar\n")
ui.Printf("network and does not know what will happen to it after sending\n")
ui.Printf("it to the URL above.\n")
if err := ui.PromptForConfirmation(fmt.Sprintf("Sign transaction to pay %s %s to %s and send it to the URL above?", c.green(v.Amount), c.green(assetDisplay), c.yellow(v.Recipient))); err != nil {
return err
}
}
// user approved the operation, so proceed
cli, err := GetWalletClient(c.G())
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewIdentifyUIProtocol(c.G()),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
if v.AssetCode == "" {
arg := stellar1.ApprovePayURILocalArg{
InputURI: c.uri,
Amount: v.Amount,
FromCLI: true,
}
txID, err := cli.ApprovePayURILocal(context.Background(), arg)
if err != nil {
return err
}
if v.CallbackURL == "" {
ui.Printf("Signed transaction sent to %s\n", v.CallbackURL)
}
ui.Printf("Sent!\nStellar Transaction ID: %v\n", txID)
} else {
// find a path payment so user can confirm it is ok
arg := stellar1.FindPaymentPathLocalArg{
To: v.Recipient,
Amount: v.Amount,
DestinationAsset: stellar1.Asset{
Code: v.AssetCode,
Issuer: v.AssetIssuer,
},
}
ui.Printf(c.yellow(fmt.Sprintf("Searching for payment path to %s...", arg.DestinationAsset)) + "\n")
path, err := cli.FindPaymentPathLocal(context.Background(), arg)
if err != nil {
return err
}
ui.Printf("Sending approximately %s of %s (at most %s)\n", path.FullPath.SourceAmount, path.FullPath.SourceAsset, path.FullPath.SourceAmountMax)
ui.Printf("Account %s will receive %s of %s\n\n", arg.To, path.FullPath.DestinationAmount, path.FullPath.DestinationAsset)
if err := ui.PromptForConfirmation("Proceed?"); err != nil {
return err
}
approveArg := stellar1.ApprovePathURILocalArg{
InputURI: c.uri,
FullPath: path.FullPath,
FromCLI: true,
}
txID, err := cli.ApprovePathURILocal(context.Background(), approveArg)
if err != nil {
return err
}
if v.CallbackURL == "" {
ui.Printf("Signed transaction sent to %s\n", v.CallbackURL)
}
ui.Printf("Sent!\nStellar Transaction ID: %v\n", txID)
}
return nil
}
func (c *CmdWalletHandleURI) txOp(v stellar1.ValidateStellarURIResultLocal) error {
ui := c.G().UI.GetTerminalUI()
ui.Printf("The URI is requesting that you sign a transaction.\n\n")
ui.Printf("Here is the base64-encoded transaction envelope:\n\n")
ui.Printf("\t%s\n\n", v.Xdr)
ui.Printf("Transaction summary:\n\n")
if v.Summary.Source != "" {
ui.Printf("\tSource account: %s\n", v.Summary.Source)
}
if v.Summary.Fee > 0 {
ui.Printf("\tFee: %d stroops\n", v.Summary.Fee)
}
if v.Summary.Memo != "" {
ui.Printf("\tMemo: %q (%s)\n", v.Summary.Memo, v.Summary.MemoType)
}
ui.Printf("\tOperations: %d\n", len(v.Summary.Operations))
for _, op := range v.Summary.Operations {
ui.Printf("\t\t%s\n", op)
}
ui.Printf("\n")
if v.CallbackURL == "" {
ui.Printf("If you confirm this request, Keybase will sign this transaction\n")
ui.Printf("with your primary account, and submit it to the Stellar network.\n\n")
if err := ui.PromptForConfirmation("Sign this transaction and submit it to the Stellar network?"); err != nil {
return err
}
} else {
ui.Printf("If you confirm this request, Keybase will sign this transaction\n")
ui.Printf("with your primary account, and send it to the following URL\n")
ui.Printf("for processing:\n\n")
ui.Printf("\t%s\n\n", v.CallbackURL)
ui.Printf("Note: Keybase will NOT be submitting the transaction to the Stellar\n")
ui.Printf("network and does not know what will happen to it after sending\n")
ui.Printf("it to the URL above.\n")
if err := ui.PromptForConfirmation("Sign this transaction and submit it to the URL above?"); err != nil {
return err
}
}
// user approved the operation, so proceed
cli, err := GetWalletClient(c.G())
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewIdentifyUIProtocol(c.G()),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
arg := stellar1.ApproveTxURILocalArg{
InputURI: c.uri,
}
txID, err := cli.ApproveTxURILocal(context.Background(), arg)
if err != nil {
return err
}
ui.Printf(c.green("Success!") + "\n")
if v.CallbackURL == "" {
ui.Printf("Transaction submitted to Stellar network, transaction ID: %s\n", txID)
} else {
ui.Printf("Transaction sent to %s, transaction ID: %s\n", v.CallbackURL, txID)
}
return nil
}
func (c *CmdWalletHandleURI) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}
func (c *CmdWalletHandleURI) green(s string) string {
return ColorString(c.G(), "green", s)
}
func (c *CmdWalletHandleURI) yellow(s string) string {
return ColorString(c.G(), "yellow", s)
}