forked from keybase/client
/
cmd_currency.go
147 lines (128 loc) · 3.39 KB
/
cmd_currency.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"fmt"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"golang.org/x/net/context"
)
type CmdCurrencyAdd struct {
libkb.Contextified
address string
force bool
wanted string
}
func NewCmdCurrency(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "currency",
Usage: "Manage cryptocurrency addresses",
ArgumentHelp: "[arguments...]",
Subcommands: []cli.Command{
NewCmdCurrencyAdd(cl, g),
},
}
}
func NewCmdBTC(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "btc",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdBTCRunner(g), "btc", c)
},
}
}
type CmdBTC struct {
libkb.Contextified
}
func NewCmdBTCRunner(g *libkb.GlobalContext) *CmdBTC {
return &CmdBTC{
Contextified: libkb.NewContextified(g),
}
}
func (c *CmdBTC) Run() (err error) {
return errors.New("this command is deprecated; use `keybase currency add` instead")
}
func (c *CmdBTC) GetUsage() libkb.Usage {
return libkb.Usage{}
}
func (c *CmdBTC) ParseArgv(ctx *cli.Context) error {
return nil
}
func (c *CmdCurrencyAdd) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 1 {
return fmt.Errorf("Must provide exactly one address.")
}
c.address = ctx.Args()[0]
c.force = ctx.Bool("force")
w := ctx.String("type")
if !(w == "bitcoin" || w == "zcash" || w == "") {
return fmt.Errorf("Bad address type; can only handle 'zcash' or 'bitcoin")
}
c.wanted = w
return nil
}
func (c *CmdCurrencyAdd) SetAddress(s string) {
c.address = s
}
func (c *CmdCurrencyAdd) Run() (err error) {
cli, err := GetCryptocurrencyClient(c.G())
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewSecretUIProtocol(c.G()),
}
if err = RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
res, err := cli.RegisterAddress(context.TODO(), keybase1.RegisterAddressArg{
Address: c.address,
Force: c.force,
WantedFamily: c.wanted,
})
if err != nil {
if _, ok := err.(libkb.ExistsError); ok {
err = fmt.Errorf("You already have a %s address; use --force to overwrite", err.Error())
}
return err
}
c.G().UI.GetTerminalUI().Printf("Added %s address %s\n", res.Family, c.address)
return nil
}
func NewCmdCurrencyAdd(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "add",
Usage: "Sign a cryptocurrency (bitcoin or zcash) address into your identity",
ArgumentHelp: "<address>",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "f, force",
Usage: "Overwrite an existing address.",
},
cli.StringFlag{
Name: "t, type",
Usage: "assert a type of address ('bitcoin' or 'zcash')",
},
},
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdCurrencyAddRunner(g), "add", c)
},
}
}
func NewCmdCurrencyAddRunner(g *libkb.GlobalContext) *CmdCurrencyAdd {
return &CmdCurrencyAdd{
Contextified: libkb.NewContextified(g),
}
}
func (c *CmdCurrencyAdd) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
GpgKeyring: true,
KbKeyring: true,
API: true,
}
}