prompt.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package utils contains internal helper functions for go-ethereum commands.
  17. package utils
  18. import (
  19. "fmt"
  20. "github.com/ethereum/go-ethereum/console/prompt"
  21. )
  22. // GetPassPhrase displays the given text(prompt) to the user and requests some textual
  23. // data to be entered, but one which must not be echoed out into the terminal.
  24. // The method returns the input provided by the user.
  25. func GetPassPhrase(text string, confirmation bool) string {
  26. if text != "" {
  27. fmt.Println(text)
  28. }
  29. password, err := prompt.Stdin.PromptPassword("Password: ")
  30. if err != nil {
  31. Fatalf("Failed to read password: %v", err)
  32. }
  33. if confirmation {
  34. confirm, err := prompt.Stdin.PromptPassword("Repeat password: ")
  35. if err != nil {
  36. Fatalf("Failed to read password confirmation: %v", err)
  37. }
  38. if password != confirm {
  39. Fatalf("Passwords do not match")
  40. }
  41. }
  42. return password
  43. }
  44. // GetPassPhraseWithList retrieves the password associated with an account, either fetched
  45. // from a list of preloaded passphrases, or requested interactively from the user.
  46. func GetPassPhraseWithList(text string, confirmation bool, index int, passwords []string) string {
  47. // If a list of passwords was supplied, retrieve from them
  48. if len(passwords) > 0 {
  49. if index < len(passwords) {
  50. return passwords[index]
  51. }
  52. return passwords[len(passwords)-1]
  53. }
  54. // Otherwise prompt the user for the password
  55. password := GetPassPhrase(text, confirmation)
  56. return password
  57. }