-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathkeys.go
316 lines (266 loc) · 8.01 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Package cmd includes relayer commands
Copyright © 2020 Jack Zampolin [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/json"
"fmt"
"io"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
const (
flagCoinType = "coin-type"
defaultCoinType uint32 = sdk.CoinType
)
// keysCmd represents the keys command
func keysCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Aliases: []string{"k"},
Short: "Manage keys held by the relayer for each chain",
}
cmd.AddCommand(
keysAddCmd(a),
keysRestoreCmd(a),
keysDeleteCmd(a),
keysListCmd(a),
keysShowCmd(a),
keysExportCmd(a),
)
return cmd
}
// keysAddCmd respresents the `keys add` command
func keysAddCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "add chain_name key_name",
Aliases: []string{"a"},
Short: "Adds a key to the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys add ibc-0
$ %s keys add ibc-1 key2
$ %s k a cosmoshub testkey`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chain, ok := a.Config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
keyName := args[1]
if chain.ChainProvider.KeyExists(keyName) {
return errKeyExists(keyName)
}
coinType, err := cmd.Flags().GetUint32(flagCoinType)
if err != nil {
return err
}
ko, err := chain.ChainProvider.AddKey(keyName, coinType)
if err != nil {
return fmt.Errorf("failed to add key: %w", err)
}
out, err := json.Marshal(&ko)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(out))
return nil
},
}
cmd.Flags().Uint32(flagCoinType, defaultCoinType, "coin type number for HD derivation")
return cmd
}
// keysRestoreCmd respresents the `keys add` command
func keysRestoreCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "restore chain_name key_name mnemonic",
Aliases: []string{"r"},
Short: "Restores a mnemonic to the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(3)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys restore ibc-0 testkey "[mnemonic-words]"
$ %s k r cosmoshub faucet-key "[mnemonic-words]"`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
keyName := args[1]
chainName := args[0]
fmt.Println("Restoring key for chain", chainName)
chain, ok := a.Config.Chains[chainName]
if !ok {
return errChainNotFound(chainName)
}
if chain.ChainProvider.KeyExists(keyName) {
return errKeyExists(keyName)
}
coinType, err := cmd.Flags().GetUint32(flagCoinType)
if err != nil {
return err
}
address, err := chain.ChainProvider.RestoreKey(keyName, args[2], coinType)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), address)
return nil
},
}
cmd.Flags().Uint32(flagCoinType, defaultCoinType, "coin type number for HD derivation")
return cmd
}
// keysDeleteCmd respresents the `keys delete` command
func keysDeleteCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "delete chain_name key_name",
Aliases: []string{"d"},
Short: "Deletes a key from the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys delete ibc-0 -y
$ %s keys delete ibc-1 key2 -y
$ %s k d cosmoshub default`, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chain, ok := a.Config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
keyName := args[1]
if !chain.ChainProvider.KeyExists(keyName) {
return errKeyDoesntExist(keyName)
}
if skip, _ := cmd.Flags().GetBool(flagSkip); !skip {
fmt.Fprintf(cmd.ErrOrStderr(), "Are you sure you want to delete key(%s) from chain(%s)? (Y/n)\n", keyName, args[0])
if !askForConfirmation(a, cmd.InOrStdin(), cmd.ErrOrStderr()) {
return nil
}
}
err := chain.ChainProvider.DeleteKey(keyName)
if err != nil {
return err
}
fmt.Fprintf(cmd.ErrOrStderr(), "key %s deleted\n", keyName)
return nil
},
}
return skipConfirm(a.Viper, cmd)
}
func askForConfirmation(a *appState, stdin io.Reader, stderr io.Writer) bool {
var response string
_, err := fmt.Fscanln(stdin, &response)
if err != nil {
a.Log.Fatal("Failed to read input", zap.Error(err))
}
switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
fmt.Fprintln(stderr, "please type (y)es or (n)o and then press enter")
return askForConfirmation(a, stdin, stderr)
}
}
// keysListCmd respresents the `keys list` command
func keysListCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "list chain_name",
Aliases: []string{"l"},
Short: "Lists keys from the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(1)),
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 {
chainName := args[0]
chain, ok := a.Config.Chains[chainName]
if !ok {
return errChainNotFound(chainName)
}
info, err := chain.ChainProvider.ListAddresses()
if err != nil {
return err
}
if len(info) == 0 {
fmt.Fprintf(cmd.ErrOrStderr(), "warning: no keys found for chain %s (do you need to run 'rly keys add %s'?)\n", chainName, chainName)
}
for key, val := range info {
fmt.Fprintf(cmd.OutOrStdout(), "key(%s) -> %s\n", key, val)
}
return nil
},
}
return cmd
}
// keysShowCmd respresents the `keys show` command
func keysShowCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "show chain_name [key_name]",
Aliases: []string{"s"},
Short: "Shows a key from the keychain associated with a particular chain",
Args: withUsage(cobra.RangeArgs(1, 2)),
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 {
chain, ok := a.Config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
var keyName string
if len(args) == 2 {
keyName = args[1]
} else {
keyName = chain.ChainProvider.Key()
}
if !chain.ChainProvider.KeyExists(keyName) {
return errKeyDoesntExist(keyName)
}
address, err := chain.ChainProvider.ShowAddress(keyName)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), address)
return nil
},
}
return cmd
}
// keysExportCmd respresents the `keys export` command
func keysExportCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "export chain_name key_name",
Aliases: []string{"e"},
Short: "Exports a privkey from the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys export ibc-0 testkey
$ %s k e cosmoshub testkey`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
keyName := args[1]
chain, ok := a.Config.Chains[args[0]]
if !ok {
return errChainNotFound(args[0])
}
if !chain.ChainProvider.KeyExists(keyName) {
return errKeyDoesntExist(keyName)
}
info, err := chain.ChainProvider.ExportPrivKeyArmor(keyName)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), info)
return nil
},
}
return cmd
}