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.

94 lines
2.5 KiB

  1. // Copyright © 2017 Ethan Frey
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cmd
  15. import (
  16. "fmt"
  17. "github.com/pkg/errors"
  18. "github.com/tendermint/go-crypto/keys"
  19. "github.com/tendermint/go-wire/data"
  20. "github.com/tendermint/tmlibs/cli"
  21. "github.com/spf13/cobra"
  22. "github.com/spf13/viper"
  23. )
  24. const (
  25. flagType = "type"
  26. flagNoBackup = "no-backup"
  27. )
  28. // newCmd represents the new command
  29. var newCmd = &cobra.Command{
  30. Use: "new [name]",
  31. Short: "Create a new public/private key pair",
  32. Long: `Add a public/private key pair to the key store.
  33. The password muts be entered in the terminal and not
  34. passed as a command line argument for security.`,
  35. RunE: runNewCmd,
  36. }
  37. func init() {
  38. newCmd.Flags().StringP(flagType, "t", "ed25519", "Type of key (ed25519|secp256k1)")
  39. newCmd.Flags().Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)")
  40. }
  41. func runNewCmd(cmd *cobra.Command, args []string) error {
  42. if len(args) != 1 || len(args[0]) == 0 {
  43. return errors.New("You must provide a name for the key")
  44. }
  45. name := args[0]
  46. algo := viper.GetString(flagType)
  47. pass, err := getCheckPassword("Enter a passphrase:", "Repeat the passphrase:")
  48. if err != nil {
  49. return err
  50. }
  51. info, seed, err := GetKeyManager().Create(name, pass, algo)
  52. if err == nil {
  53. printCreate(info, seed)
  54. }
  55. return err
  56. }
  57. type NewOutput struct {
  58. Key keys.Info `json:"key"`
  59. Seed string `json:"seed"`
  60. }
  61. func printCreate(info keys.Info, seed string) {
  62. switch viper.Get(cli.OutputFlag) {
  63. case "text":
  64. printInfo(info)
  65. // print seed unless requested not to.
  66. if !viper.GetBool(flagNoBackup) {
  67. fmt.Println("**Important** write this seed phrase in a safe place.")
  68. fmt.Println("It is the only way to recover your account if you ever forget your password.\n")
  69. fmt.Println(seed)
  70. }
  71. case "json":
  72. out := NewOutput{Key: info}
  73. if !viper.GetBool(flagNoBackup) {
  74. out.Seed = seed
  75. }
  76. json, err := data.ToJSON(out)
  77. if err != nil {
  78. panic(err) // really shouldn't happen...
  79. }
  80. fmt.Println(string(json))
  81. }
  82. }