This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
keys.go
271 lines (235 loc) · 6.72 KB
/
keys.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
package cmd
import (
"encoding/json"
"fmt"
"log"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/lens/client"
)
const (
flagCoinType = "coin-type"
defaultCoinType uint32 = sdk.CoinType
)
// keysCmd represents the keys command
func keysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Aliases: []string{"k"},
Short: "manage keys held by the relayer for each chain",
}
cmd.AddCommand(keysAddCmd())
cmd.AddCommand(keysRestoreCmd())
cmd.AddCommand(keysDeleteCmd())
cmd.AddCommand(keysListCmd())
cmd.AddCommand(keysShowCmd())
cmd.AddCommand(keysExportCmd())
return cmd
}
// keysAddCmd respresents the `keys add` command
func keysAddCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add [name]",
Aliases: []string{"a"},
Short: "adds a key to the keychain associated with a particular chain",
Long: "if no name is passed, name in config is used",
Args: cobra.RangeArgs(0, 1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys add ibc-0
$ %s keys add ibc-1 key2
$ %s k a ibc-2 testkey`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
cl := client.GetChainClientFromContext(cmd.Context(), config.DefaultChain)
var keyName string
if len(args) == 0 {
keyName = cl.Config.Key
} else {
keyName = args[0]
}
if cl.KeyExists(keyName) {
return errKeyExists(keyName)
}
ko, err := cl.AddKey(keyName)
if err != nil {
return err
}
out, err := json.Marshal(&ko)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}
// TODO: wire this up
cmd.Flags().Uint32(flagCoinType, defaultCoinType, "coin type number for HD derivation")
return cmd
}
// keysRestoreCmd respresents the `keys add` command
func keysRestoreCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "restore [name] [mnemonic]",
Aliases: []string{"r"},
Short: "restores a mnemonic to the keychain associated with a particular chain",
Args: cobra.ExactArgs(2),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys restore ibc-0 testkey "[mnemonic-words]"
$ %s k r ibc-1 faucet-key "[mnemonic-words]"`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
cl := client.GetChainClientFromContext(cmd.Context(), config.DefaultChain)
keyName := args[1]
if cl.KeyExists(keyName) {
return errKeyExists(keyName)
}
address, err := cl.RestoreKey(keyName, args[2])
if err != nil {
return err
}
fmt.Println(address)
return nil
},
}
// TODO: wire this up
cmd.Flags().Uint32(flagCoinType, defaultCoinType, "coin type number for HD derivation")
return cmd
}
// keysDeleteCmd respresents the `keys delete` command
func keysDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [name]",
Aliases: []string{"d"},
Short: "deletes a key from the keychain associated with a particular chain",
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys delete ibc-0 -y
$ %s keys delete ibc-1 key2 -y
$ %s k d ibc-2 testkey`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
cl := client.GetChainClientFromContext(cmd.Context(), config.DefaultChain)
keyName := args[1]
if !cl.KeyExists(keyName) {
return errKeyDoesntExist(keyName)
}
if skip, _ := cmd.Flags().GetBool("skip"); !skip {
fmt.Printf("Are you sure you want to delete key(%s) from chain(%s)? (Y/n)\n", keyName, args[0])
if !askForConfirmation() {
return nil
}
}
if err := cl.DeleteKey(keyName); err != nil {
panic(err)
}
fmt.Printf("key %s deleted\n", keyName)
return nil
},
}
return skipConfirm(cmd)
}
func askForConfirmation() bool {
var response string
_, err := fmt.Scanln(&response)
if err != nil {
log.Fatal(err)
}
switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
fmt.Println("please type (y)es or (n)o and then press enter")
return askForConfirmation()
}
}
// keysListCmd respresents the `keys list` command
func keysListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "lists keys from the keychain associated with a particular chain",
Args: cobra.NoArgs,
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys list ibc-0
$ %s k l ibc-1`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
cl := client.GetChainClientFromContext(cmd.Context(), config.DefaultChain)
info, err := cl.ListAddresses()
if err != nil {
return err
}
for key, val := range info {
fmt.Printf("key(%s) -> %s\n", key, val)
}
return nil
},
}
return cmd
}
// keysShowCmd respresents the `keys show` command
func keysShowCmd() *cobra.Command {
// TODO: add in flags to allow for rendering addresses in different
// formats as well as for different chains (i.e. prefixes)
cmd := &cobra.Command{
Use: "show [name]",
Aliases: []string{"s"},
Short: "shows a key from the keychain associated with a particular chain",
Long: "if no name is passed, name in config is used",
Args: cobra.RangeArgs(0, 1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys show ibc-0
$ %s keys show ibc-1 key2
$ %s k s ibc-2 testkey`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
cl := client.GetChainClientFromContext(cmd.Context(), config.DefaultChain)
var keyName string
if len(args) == 0 {
keyName = cl.Config.Key
} else {
keyName = args[0]
}
if !cl.KeyExists(keyName) {
return errKeyDoesntExist(keyName)
}
address, err := cl.ShowAddress(keyName)
if err != nil {
return err
}
fmt.Println(address)
return nil
},
}
return cmd
}
// keysExportCmd respresents the `keys export` command
func keysExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export [name]",
Aliases: []string{"e"},
Short: "exports a privkey from the keychain associated with a particular chain",
Args: cobra.ExactArgs(1),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys export ibc-0 testkey
$ %s k e ibc-2 testkey`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
cl := client.GetChainClientFromContext(cmd.Context(), config.DefaultChain)
keyName := args[1]
if !cl.KeyExists(keyName) {
return errKeyDoesntExist(keyName)
}
info, err := cl.ExportPrivKeyArmor(keyName)
if err != nil {
return err
}
fmt.Println(info)
return nil
},
}
return cmd
}
func errKeyExists(name string) error {
return fmt.Errorf("a key with name %s already exists", name)
}
func errKeyDoesntExist(name string) error {
return fmt.Errorf("a key with name %s doesn't exist", name)
}