Skip to content
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 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
const (
flagHome = "home"
flagFile = "file"
flagMnemonic = "mnemonic"
flagPrivateKey = "priv_key"
flagCoinType = "coin-type"
flagAccount = "account"
flagAccountIndex = "index"
Expand Down
65 changes: 50 additions & 15 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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),
Copy link
Collaborator

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)?

).
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
}
Expand All @@ -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
Expand All @@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down
25 changes: 22 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cosmossdk.io/math v1.4.0
cosmossdk.io/x/tx v0.13.5
github.com/bandprotocol/chain/v3 v3.0.0-20241202095241-21710ba55161
github.com/charmbracelet/huh v0.6.0
github.com/cometbft/cometbft v0.38.12
github.com/cosmos/cosmos-sdk v0.50.10
github.com/cosmos/gogoproto v1.7.0
Expand Down Expand Up @@ -41,6 +42,8 @@ require (
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/bandprotocol/go-owasm v0.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
Expand All @@ -49,9 +52,16 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charmbracelet/bubbles v0.20.0 // indirect
github.com/charmbracelet/bubbletea v1.2.4 // indirect
github.com/charmbracelet/lipgloss v1.0.0 // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
Expand Down Expand Up @@ -84,6 +94,7 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
github.com/emicklei/dot v1.6.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
github.com/fatih/color v1.16.0 // indirect
Expand Down Expand Up @@ -127,12 +138,19 @@ require (
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/linxGnu/grocksdb v1.8.14 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
github.com/oklog/run v1.1.0 // indirect
Expand All @@ -144,6 +162,7 @@ require (
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/rs/zerolog v1.33.0 // indirect
Expand All @@ -170,10 +189,10 @@ require (
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241021214115-324edc3d5d38 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect
Expand Down
Loading
Loading