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.

58 lines
1.7 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. "path/filepath"
  17. "github.com/spf13/cobra"
  18. "github.com/spf13/viper"
  19. keys "github.com/tendermint/go-crypto/keys"
  20. "github.com/tendermint/go-crypto/keys/cryptostore"
  21. "github.com/tendermint/go-crypto/keys/storage/filestorage"
  22. "github.com/tendermint/tmlibs/cli"
  23. )
  24. const KeySubdir = "keys"
  25. var (
  26. manager keys.Manager
  27. )
  28. // RootCmd represents the base command when called without any subcommands
  29. var RootCmd = &cobra.Command{
  30. Use: "keys",
  31. Short: "Key manager for tendermint clients",
  32. Long: `Keys allows you to manage your local keystore for tendermint.
  33. These keys may be in any format supported by go-crypto and can be
  34. used by light-clients, full nodes, or any other application that
  35. needs to sign with a private key.`,
  36. }
  37. // GetKeyManager initializes a key manager based on the configuration
  38. func GetKeyManager() keys.Manager {
  39. if manager == nil {
  40. // store the keys directory
  41. rootDir := viper.GetString(cli.HomeFlag)
  42. keyDir := filepath.Join(rootDir, KeySubdir)
  43. // and construct the key manager
  44. manager = cryptostore.New(
  45. cryptostore.SecretBox,
  46. filestorage.New(keyDir),
  47. )
  48. }
  49. return manager
  50. }