Skip to content

Commit

Permalink
Merge pull request #156 from SiaFoundation/nate/more-verbose-input
Browse files Browse the repository at this point in the history
Add additional context to CLI prompts
  • Loading branch information
n8maninger committed Aug 25, 2023
2 parents 2290191 + 5a57ca9 commit 98786ec
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions cmd/hostd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ var (
disableStdin bool
)

func readPasswordInput(context string) (string, error) {
fmt.Printf("%s: ", context)
input, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
return string(input), err
}

// mustSetAPIPassword prompts the user to enter an API password if one is not
// already set via environment variable or config file.
func mustSetAPIPassword(log *zap.Logger) {
Expand All @@ -65,15 +72,16 @@ func mustSetAPIPassword(log *zap.Logger) {
log.Fatal("API password must be set via environment variable or config file when --env flag is set")
}

fmt.Print("Enter API password: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
fmt.Println("Please choose a password to unlock the UI and API.")
fmt.Println("(The password must be at least 4 characters.)")
pass, err := readPasswordInput("Enter password")
if err != nil {
log.Fatal("Could not read API password", zap.Error(err))
} else if len(pw) == 0 {
log.Fatal("API password cannot be empty")
log.Fatal("Could not read password", zap.Error(err))
} else if len(pass) < 4 {
log.Fatal("Password must be at least 4 characters")
}
cfg.HTTP.Password = string(pw)
fmt.Println("")
cfg.HTTP.Password = pass
}

// mustSetWalletkey prompts the user to enter a wallet seed phrase if one is not
Expand All @@ -86,15 +94,46 @@ func mustSetWalletkey(log *zap.Logger) {
os.Exit(1)
}

fmt.Print("Enter wallet seed: ")
phrase, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println("Type in your 12-word seed phrase and press enter. If you do not have a seed phrase yet, type 'seed' to generate one.")
phrase, err := readPasswordInput("Enter seed phrase")
if err != nil {
log.Fatal("Could not read seed phrase", zap.Error(err))
} else if len(phrase) == 0 || len(strings.Fields(string(phrase))) != 12 {
}

if strings.ToLower(strings.TrimSpace(phrase)) == "seed" {
var seed [32]byte
phrase = wallet.NewSeedPhrase()
if err := wallet.SeedFromPhrase(&seed, phrase); err != nil {
panic(err)
}
key := wallet.KeyFromSeed(&seed, 0)
fmt.Println("")
fmt.Println("A new seed phrase has been generated below. Write it down and keep it safe.")
fmt.Println("Your seed phrase is the only way to recover your Siacoin. If you lose your seed phrase, you will also lose your Siacoin.")
fmt.Println("You will need to re-enter this seed phrase every time you start hostd.")
fmt.Println("")
fmt.Println("Seed Phrase:", phrase)
fmt.Println("Wallet Address:", key.PublicKey().StandardAddress())

// confirm seed phrase
for {
fmt.Println("")
fmt.Println("Please confirm your seed phrase to continue.")
confirmPhrase, err := readPasswordInput("Enter seed phrase")
if err != nil {
log.Fatal("Could not read seed phrase", zap.Error(err))
} else if confirmPhrase == phrase {
break
}

fmt.Println("Seed phrases do not match!")
fmt.Println("You entered:", confirmPhrase)
fmt.Println("Actual phrase:", phrase)
}
} else if phrase == "" || len(strings.Fields(phrase)) != 12 {
log.Fatal("Seed phrase must be 12 words")
}
fmt.Println()
cfg.RecoveryPhrase = string(phrase)
cfg.RecoveryPhrase = phrase
}

// tryLoadConfig loads the config file specified by the HOSTD_CONFIG_PATH. If
Expand Down

0 comments on commit 98786ec

Please sign in to comment.