-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] - add prompt #21
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ebb5675
add prompt
0874b0e
Merge remote-tracking branch 'origin/main' into add-key/mnemonic-with…
92ba9f0
add prompt
397d5e0
fix lint
32d4b50
remove password mask
ef2ef8f
refac huh option
95fb15e
add cointype account index to prompt
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,18 @@ import ( | |
"fmt" | ||
"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)" | ||
) | ||
|
||
// keysCmd represents the keys command | ||
func keysCmd(app *relayer.App) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
|
@@ -42,26 +49,53 @@ $ %s k a eth test-key`, appName, appName)), | |
RunE: func(cmd *cobra.Command, args []string) error { | ||
chainName := args[0] | ||
keyName := args[1] | ||
|
||
mnemonic, err := cmd.Flags().GetString(flagMnemonic) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
privateKey, err := cmd.Flags().GetString(flagPrivateKey) | ||
if err != nil { | ||
mnemonic := "" | ||
privateKey := "" | ||
|
||
// Use huh to create a form for user input | ||
selection := "" | ||
selectionPrompt := huh.NewGroup(huh.NewSelect[string](). | ||
Title("Choose how to add a key"). | ||
Options( | ||
huh.NewOption(privateKeyLabel, privateKeyLabel), | ||
huh.NewOption(mnemonicLabel, mnemonicLabel), | ||
huh.NewOption(defaultLabel, defaultLabel), | ||
). | ||
Value(&selection)) | ||
|
||
form := huh.NewForm(selectionPrompt) | ||
if err := form.WithTheme(huh.ThemeBase()).Run(); err != nil { | ||
return err | ||
} | ||
|
||
if mnemonic != "" && privateKey != "" { | ||
return fmt.Errorf("only one of mnemonic or private key should be provided, not both") | ||
// Handle the selected option | ||
switch selection { | ||
case privateKeyLabel: | ||
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 mnemonicLabel: | ||
mnemonicPrompt := huh.NewGroup(huh.NewInput(). | ||
Title("Enter your mnemonic"). | ||
Value(&mnemonic)) | ||
|
||
form := huh.NewForm(mnemonicPrompt) | ||
if err := form.WithTheme(huh.ThemeBase()).Run(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Get additional flags | ||
coinType, err := cmd.Flags().GetInt32(flagCoinType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if coinType < 0 { | ||
coinType = defaultCoinType | ||
} | ||
|
@@ -76,6 +110,7 @@ $ %s k a eth test-key`, appName, appName)), | |
return err | ||
} | ||
|
||
// Add the key to the app | ||
keyOutput, err := app.AddKey(chainName, keyName, mnemonic, privateKey, uint32(coinType), account, index) | ||
if err != nil { | ||
return err | ||
|
@@ -90,10 +125,10 @@ $ %s k a eth test-key`, appName, appName)), | |
return nil | ||
}, | ||
} | ||
cmd.Flags().StringP(flagMnemonic, "m", "", "add new key from specified mnemonic") | ||
cmd.Flags().StringP(flagPrivateKey, "p", "", "add new key from specified private key") | ||
cmd.Flags().Int32(flagCoinType, -1, "coin type number for HD derivation") | ||
cmd.Flags().Uint(flagAccount, 0, "account number within the HD derivation path") | ||
|
||
// Command flags | ||
cmd.Flags().Int32(flagCoinType, -1, "Coin type number for HD derivation") | ||
cmd.Flags().Uint(flagAccount, 0, "Account number within the HD derivation path") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are used only in creating new keys? should we add it in the default section instead of flag; ask @RogerKSI |
||
cmd.Flags(). | ||
Uint(flagAccountIndex, 0, "Index number for the specific address within an account in the HD derivation path") | ||
return cmd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should key and value be the same (long string)?