/
cmd_wallet_send.go
181 lines (159 loc) · 4.63 KB
/
cmd_wallet_send.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
package client
import (
"errors"
"fmt"
"strings"
"github.com/keybase/cli"
"github.com/adamwalz/keybase-client/go/libcmdline"
"github.com/adamwalz/keybase-client/go/libkb"
"github.com/adamwalz/keybase-client/go/protocol/stellar1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"github.com/keybase/stellarnet"
"golang.org/x/net/context"
)
type CmdWalletSend struct {
libkb.Contextified
Recipient string
Amount string
Note string
LocalCurrency string
ForceRelay bool
FromAccountID stellar1.AccountID
Memo string
MemoType stellar1.PublicNoteType
}
func newCmdWalletSend(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := []cli.Flag{
cli.StringFlag{
Name: "m, message",
Usage: "Include an encrypted message with the payment.",
},
cli.StringFlag{
Name: "from",
Usage: "Specify the source account for the payment.",
},
cli.StringFlag{
Name: "memo",
Usage: "Include a public memo in the stellar transaction.",
},
cli.StringFlag{
Name: "memo_type",
Usage: "Specify the type of memo (text, id, hash, return). hash and return should be hex-encoded.",
},
}
if develUsage {
flags = append(flags, cli.BoolFlag{
Name: "relay",
Usage: "Force a relay transfer (dev-only)",
})
}
cmd := &CmdWalletSend{
Contextified: libkb.NewContextified(g),
}
return cli.Command{
Name: "send",
Usage: "Send XLM to a keybase user or stellar address",
ArgumentHelp: "<recipient> <amount> <local currency> [-m message]",
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "send", c)
},
Flags: flags,
}
}
func (c *CmdWalletSend) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) > 3 {
return errors.New("send expects at most three arguments")
} else if len(ctx.Args()) < 2 {
return errors.New("send expects at least two arguments (recipient and amount)")
}
c.Recipient = ctx.Args()[0]
// TODO ensure amount is numeric and does not contain escape characters
c.Amount = ctx.Args()[1]
if len(ctx.Args()) == 3 {
c.LocalCurrency = strings.ToUpper(ctx.Args()[2])
if len(c.LocalCurrency) != 3 {
return errors.New("Invalid currency code")
}
}
c.Note = ctx.String("message")
c.Memo = ctx.String("memo")
memoType := ctx.String("memo_type")
if memoType == "" {
if c.Memo != "" {
memoType = "text"
} else {
memoType = "none"
}
}
var ok bool
c.MemoType, ok = stellar1.PublicNoteTypeMap[strings.ToUpper(memoType)]
if !ok {
return errors.New("invalid memo type")
}
c.ForceRelay = ctx.Bool("relay")
c.FromAccountID = stellar1.AccountID(ctx.String("from"))
return nil
}
func (c *CmdWalletSend) 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()
amount := c.Amount
amountDesc := fmt.Sprintf("%s XLM", amount)
var displayAmount, displayCurrency string
if c.LocalCurrency != "" && c.LocalCurrency != "XLM" {
exchangeRate, err := cli.ExchangeRateLocal(context.Background(), stellar1.OutsideCurrencyCode(c.LocalCurrency))
if err != nil {
return fmt.Errorf("Unable to get exchange rate for %q: %s", c.LocalCurrency, err)
}
amount, err = stellarnet.ConvertOutsideToXLM(c.Amount, exchangeRate.Rate)
if err != nil {
return err
}
ui.Printf("Current exchange rate: ~ %s %s / XLM\n", exchangeRate.Rate, c.LocalCurrency)
amountDesc = fmt.Sprintf("%s XLM (~%s %s)", amount, c.Amount, c.LocalCurrency)
displayAmount = c.Amount
displayCurrency = c.LocalCurrency
}
_, err = stellarnet.ParseStellarAmount(amount)
if err != nil {
return fmt.Errorf("invalid amount of XLM: %q", amount)
}
if err := ui.PromptForConfirmation(fmt.Sprintf("Send %s to %s?", ColorString(c.G(), "green", amountDesc), ColorString(c.G(), "yellow", c.Recipient))); err != nil {
return err
}
arg := stellar1.SendCLILocalArg{
Recipient: c.Recipient,
Amount: amount,
Asset: stellar1.AssetNative(),
Note: c.Note,
DisplayAmount: displayAmount,
DisplayCurrency: displayCurrency,
ForceRelay: c.ForceRelay,
FromAccountID: c.FromAccountID,
PublicNote: c.Memo,
PublicNoteType: c.MemoType,
}
res, err := cli.SendCLILocal(context.Background(), arg)
if err != nil {
return err
}
ui.Printf("Sent!\nKeybase Transaction ID: %v\nStellar Transaction ID: %v\n", res.KbTxID, res.TxID)
return nil
}
func (c *CmdWalletSend) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}