-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_wallet_request.go
107 lines (94 loc) · 2.43 KB
/
cmd_wallet_request.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
package client
import (
"errors"
"strings"
"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"
"golang.org/x/net/context"
)
type CmdWalletRequest struct {
libkb.Contextified
Recipient string
TLFName string
Amount string
LocalCurrency string
Note string
}
func newCmdWalletRequest(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := []cli.Flag{
cli.StringFlag{
Name: "m, message",
Usage: "Include a message with the request.",
},
}
cmd := &CmdWalletRequest{
Contextified: libkb.NewContextified(g),
}
return cli.Command{
Name: "request",
Usage: "Request XLM from a Keybase user",
ArgumentHelp: "<recipient> <amount> <local currency> [-m message]",
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "request", c)
},
Flags: flags,
}
}
func (c *CmdWalletRequest) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) > 3 {
return errors.New("request expects at most three arguments")
} else if len(ctx.Args()) < 2 {
return errors.New("request 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")
return nil
}
func (c *CmdWalletRequest) 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
}
arg := stellar1.MakeRequestCLILocalArg{
Recipient: c.Recipient,
Note: c.Note,
Amount: c.Amount,
}
if c.LocalCurrency != "" && c.LocalCurrency != "XLM" {
currency := stellar1.OutsideCurrencyCode(c.LocalCurrency)
arg.Currency = ¤cy
} else {
xlm := stellar1.AssetNative()
arg.Asset = &xlm
}
_, err = cli.MakeRequestCLILocal(context.Background(), arg)
if err != nil {
return err
}
return nil
}
func (c *CmdWalletRequest) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}