You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.6 KiB

  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/bgentry/speakeasy"
  5. "github.com/pkg/errors"
  6. "github.com/spf13/viper"
  7. keys "github.com/tendermint/go-crypto/keys"
  8. data "github.com/tendermint/go-wire/data"
  9. "github.com/tendermint/tmlibs/cli"
  10. )
  11. const PassLength = 10
  12. func getPassword(prompt string) (string, error) {
  13. pass, err := speakeasy.Ask(prompt)
  14. if err != nil {
  15. return "", err
  16. }
  17. if len(pass) < PassLength {
  18. return "", errors.Errorf("Password must be at least %d characters", PassLength)
  19. }
  20. return pass, nil
  21. }
  22. func getCheckPassword(prompt, prompt2 string) (string, error) {
  23. // TODO: own function???
  24. pass, err := getPassword(prompt)
  25. if err != nil {
  26. return "", err
  27. }
  28. pass2, err := getPassword(prompt2)
  29. if err != nil {
  30. return "", err
  31. }
  32. if pass != pass2 {
  33. return "", errors.New("Passphrases don't match")
  34. }
  35. return pass, nil
  36. }
  37. func printInfo(info keys.Info) {
  38. switch viper.Get(cli.OutputFlag) {
  39. case "text":
  40. addr, err := data.ToText(info.Address)
  41. if err != nil {
  42. panic(err) // really shouldn't happen...
  43. }
  44. sep := "\t\t"
  45. if len(info.Name) > 7 {
  46. sep = "\t"
  47. }
  48. fmt.Printf("%s%s%s\n", info.Name, sep, addr)
  49. case "json":
  50. json, err := data.ToJSON(info)
  51. if err != nil {
  52. panic(err) // really shouldn't happen...
  53. }
  54. fmt.Println(string(json))
  55. }
  56. }
  57. func printInfos(infos keys.Infos) {
  58. switch viper.Get(cli.OutputFlag) {
  59. case "text":
  60. fmt.Println("All keys:")
  61. for _, i := range infos {
  62. printInfo(i)
  63. }
  64. case "json":
  65. json, err := data.ToJSON(infos)
  66. if err != nil {
  67. panic(err) // really shouldn't happen...
  68. }
  69. fmt.Println(string(json))
  70. }
  71. }