Skip to content

Commit

Permalink
[Feature] - add prompt (#21)
Browse files Browse the repository at this point in the history
* add prompt

* add prompt

* fix lint

* remove password mask

* refac huh option

* add cointype account index to prompt

---------

Co-authored-by: Tanut Lertwarachai <tanutlertwarachai@Tanuts-MacBook-Pro.local>
  • Loading branch information
tanut32039 and Tanut Lertwarachai authored Dec 12, 2024
1 parent 9515de5 commit 97c6db4
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 55 deletions.
9 changes: 2 additions & 7 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package cmd

const (
flagHome = "home"
flagFile = "file"
flagMnemonic = "mnemonic"
flagPrivateKey = "priv_key"
flagCoinType = "coin-type"
flagAccount = "account"
flagAccountIndex = "index"
flagHome = "home"
flagFile = "file"
)
173 changes: 137 additions & 36 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ 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{
Expand Down Expand Up @@ -42,41 +56,133 @@ $ %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 {
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
}

privateKey, err := cmd.Flags().GetString(flagPrivateKey)
if err != nil {
return err
}

if mnemonic != "" && privateKey != "" {
return fmt.Errorf("only one of mnemonic or private key should be provided, not both")
// 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
}
}

coinType, err := cmd.Flags().GetInt32(flagCoinType)
if err != nil {
return err
}

if coinType < 0 {
coinType = defaultCoinType
}

account, err := cmd.Flags().GetUint(flagAccount)
if err != nil {
return err
}

index, err := cmd.Flags().GetUint(flagAccountIndex)
if err != nil {
return err
}

keyOutput, err := app.AddKey(chainName, keyName, mnemonic, privateKey, uint32(coinType), account, index)
// 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
}
Expand All @@ -90,12 +196,7 @@ $ %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")
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

0 comments on commit 97c6db4

Please sign in to comment.