-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
306 lines (264 loc) · 7.57 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
package cmd
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/charmbracelet/huh"
"github.com/spf13/cobra"
"github.com/bandprotocol/falcon/relayer"
)
const (
privateKeyLabel = "Private key (provide an existing private key)"
mnemonicLabel = "Mnemonic (recover from an existing mnemonic phrase)"
defaultLabel = "Generate new address (no private key or mnemonic needed)"
)
const (
privateKeyResult = iota
mnemonicResult
defaultResult
)
// keysCmd represents the keys command
func keysCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Aliases: []string{"k"},
Short: "Manage keys held by the relayer for each chain",
}
cmd.AddCommand(
keysAddCmd(app),
keysDeleteCmd(app),
keysListCmd(app),
keysExportCmd(app),
keysShowCmd(app),
)
return cmd
}
// keysAddCmd returns a command that adds a key to the keychain.
func keysAddCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "add [chain_name] [key_name]",
Aliases: []string{"a"},
Short: "Add a key to the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys add eth test-key
$ %s k a eth test-key`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chainName := args[0]
keyName := args[1]
mnemonic := ""
privateKey := ""
var (
coinType, account, index uint64
coinTypeStr, accountStr, indexStr string
)
// Use huh to create a form for user input
selection := 0
selectionPrompt := huh.NewGroup(huh.NewSelect[int]().
Title("Choose how to add a key").
Options(
huh.NewOption(privateKeyLabel, privateKeyResult),
huh.NewOption(mnemonicLabel, mnemonicResult),
huh.NewOption(defaultLabel, defaultResult),
).
Value(&selection))
form := huh.NewForm(selectionPrompt)
if err := form.WithTheme(huh.ThemeBase()).Run(); err != nil {
return err
}
// Coin type input
coinTypeInput := huh.NewInput().
Title("Enter a coin type").
Description("Coin type number for HD derivation (default: 60; leave empty to use default)").
Value(&coinTypeStr).Validate(
func(s string) error {
if s == "" {
coinType = defaultCoinType
return nil
}
var err error
coinType, err = strconv.ParseUint(s, 10, 32)
if err != nil {
return fmt.Errorf("invalid coin type input (should be uint32)")
}
return nil
},
)
// Account type input
accountInput := huh.NewInput().
Title("Enter an account").
Description("Account number in the HD derivation path (default: 0; leave empty to use default)").
Value(&accountStr).Validate(
func(s string) error {
if s == "" {
account = 0
return nil
}
var err error
account, err = strconv.ParseUint(s, 10, 32)
if err != nil {
return fmt.Errorf("invalid account input (should be uint32)")
}
return nil
},
)
// Index type input
indexInput := huh.NewInput().
Title("Enter an index").
Description("Index number for the specific address within an account in the HD derivation path (default: 0; leave empty to use default)").
Value(&indexStr).Validate(
func(s string) error {
if s == "" {
index = 0
return nil
}
var err error
index, err = strconv.ParseUint(s, 10, 32)
if err != nil {
return fmt.Errorf("invalid index input (should be uint32)")
}
return nil
},
)
// Handle the selected option
switch selection {
case privateKeyResult:
privateKeyPrompt := huh.NewGroup(huh.NewInput().
Title("Enter your private key").
Value(&privateKey))
form := huh.NewForm(privateKeyPrompt)
if err := form.WithTheme(huh.ThemeBase()).Run(); err != nil {
return err
}
case mnemonicResult:
mnemonicPrompt := huh.NewGroup(huh.NewInput().
Title("Enter your mnemonic").
Value(&mnemonic),
coinTypeInput,
accountInput,
indexInput,
)
form := huh.NewForm(mnemonicPrompt)
if err := form.WithTheme(huh.ThemeBase()).Run(); err != nil {
return err
}
case defaultResult:
defaultPrompt := huh.NewGroup(coinTypeInput, accountInput, indexInput)
form := huh.NewForm(defaultPrompt)
if err := form.WithTheme(huh.ThemeBase()).Run(); err != nil {
return err
}
}
// Add the key to the app
keyOutput, err := app.AddKey(
chainName,
keyName,
mnemonic,
privateKey,
uint32(coinType),
uint(account),
uint(index),
)
if err != nil {
return err
}
out, err := json.MarshalIndent(keyOutput, "", " ")
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(out))
return nil
},
}
return cmd
}
// keysDeleteCmd returns a command that delete the key from the keychain.
func keysDeleteCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "delete [chain_name] [key_name]",
Aliases: []string{"d"},
Short: "Delete a key from the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys delete eth test-key
$ %s k d eth test-key`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chainName := args[0]
keyName := args[1]
return app.DeleteKey(chainName, keyName)
},
}
return cmd
}
// keysListCmd returns a command that list keys associated with a particular chain from the keychain.
func keysListCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "list [chain_name]",
Aliases: []string{"l"},
Short: "List keys from the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(1)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys list eth
$ %s k l eth`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chainName := args[0]
keys, err := app.ListKeys(chainName)
if err != nil {
return err
}
out := "key(%s) -> %s"
for _, key := range keys {
fmt.Fprintln(cmd.OutOrStdout(), fmt.Sprintf(out, key.KeyName, key.Address))
}
return nil
},
}
return cmd
}
// keysExportCmd returns a command that export the private key from the keychain.
func keysExportCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "export [chain_name] [key_name]",
Aliases: []string{"e"},
Short: "Export a private key from the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys export eth test-key
$ %s k e eth test-key`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chainName := args[0]
keyName := args[1]
privateKey, err := app.ExportKey(chainName, keyName)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), privateKey)
return nil
},
}
return cmd
}
// keysShowCmd a command that show the key information.
func keysShowCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "show [chain_name] [key_name]",
Aliases: []string{"s"},
Short: "Show a key from the keychain associated with a particular chain",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s keys show eth test-key
$ %s k s eth test-key`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chainName := args[0]
keyName := args[1]
address, err := app.ShowKey(chainName, keyName)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), address)
return nil
},
}
return cmd
}