From e9537b2da6554795d52eac68c5085f73ab6721cf Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 20 Jun 2017 19:50:39 +0200 Subject: [PATCH] Freshen up existing cmd files --- cmd/get.go | 26 ++++++++++++-------------- cmd/keys/main.go | 3 +++ cmd/list.go | 16 +++++++--------- cmd/new.go | 13 ++++++++----- cmd/root.go | 33 +++++++++------------------------ cmd/serve.go | 22 +++++++++++++--------- cmd/update.go | 9 ++------- cmd/utils.go | 29 ++++++++++++++++++++++++++--- 8 files changed, 80 insertions(+), 71 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index 6e8c620d0..9d30f49b8 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -25,20 +25,18 @@ var getCmd = &cobra.Command{ Use: "get ", Short: "Get details of one key", Long: `Return public details of one local key.`, - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 || len(args[0]) == 0 { - return errors.New("You must provide a name for the key") - } - name := args[0] - - info, err := GetKeyManager().Get(name) - if err == nil { - printInfo(info) - } - return err - }, + RunE: runGetCmd, } -func init() { - RootCmd.AddCommand(getCmd) +func runGetCmd(cmd *cobra.Command, args []string) error { + if len(args) != 1 || len(args[0]) == 0 { + return errors.New("You must provide a name for the key") + } + name := args[0] + + info, err := GetKeyManager().Get(name) + if err == nil { + printInfo(info) + } + return err } diff --git a/cmd/keys/main.go b/cmd/keys/main.go index 82b355346..8780f7bbc 100644 --- a/cmd/keys/main.go +++ b/cmd/keys/main.go @@ -22,6 +22,9 @@ import ( ) func main() { + // for demos, we enable the key server, probably don't want this + // in most binaries we embed the key management into + cmd.RegisterServer() root := cli.PrepareMainCmd(cmd.RootCmd, "TM", os.ExpandEnv("$HOME/.tlc")) root.Execute() } diff --git a/cmd/list.go b/cmd/list.go index b0419a1e4..51eeeda12 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -22,15 +22,13 @@ var listCmd = &cobra.Command{ Short: "List all keys", Long: `Return a list of all public keys stored by this key manager along with their associated name and address.`, - RunE: func(cmd *cobra.Command, args []string) error { - infos, err := GetKeyManager().List() - if err == nil { - printInfos(infos) - } - return err - }, + RunE: runListCmd, } -func init() { - RootCmd.AddCommand(listCmd) +func runListCmd(cmd *cobra.Command, args []string) error { + infos, err := GetKeyManager().List() + if err == nil { + printInfos(infos) + } + return err } diff --git a/cmd/new.go b/cmd/new.go index 0dd481e01..b48f9727a 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -21,6 +21,10 @@ import ( "github.com/spf13/viper" ) +const ( + flagType = "type" +) + // newCmd represents the new command var newCmd = &cobra.Command{ Use: "new ", @@ -28,20 +32,19 @@ var newCmd = &cobra.Command{ Long: `Add a public/private key pair to the key store. The password muts be entered in the terminal and not passed as a command line argument for security.`, - RunE: newPassword, + RunE: runNewCmd, } func init() { - RootCmd.AddCommand(newCmd) - newCmd.Flags().StringP("type", "t", "ed25519", "Type of key (ed25519|secp256k1)") + newCmd.Flags().StringP(flagType, "t", "ed25519", "Type of key (ed25519|secp256k1)") } -func newPassword(cmd *cobra.Command, args []string) error { +func runNewCmd(cmd *cobra.Command, args []string) error { if len(args) != 1 || len(args[0]) == 0 { return errors.New("You must provide a name for the key") } name := args[0] - algo := viper.GetString("type") + algo := viper.GetString(flagType) pass, err := getCheckPassword("Enter a passphrase:", "Repeat the passphrase:") if err != nil { diff --git a/cmd/root.go b/cmd/root.go index cd1913273..ad5a633fb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,14 +15,8 @@ package cmd import ( - "path/filepath" - "github.com/spf13/cobra" - "github.com/spf13/viper" keys "github.com/tendermint/go-crypto/keys" - "github.com/tendermint/go-crypto/keys/cryptostore" - "github.com/tendermint/go-crypto/keys/storage/filestorage" - "github.com/tendermint/tmlibs/cli" ) const KeySubdir = "keys" @@ -42,22 +36,13 @@ used by light-clients, full nodes, or any other application that needs to sign with a private key.`, } -// GetKeyManager initializes a key manager based on the configuration -func GetKeyManager() keys.Manager { - if manager == nil { - // store the keys directory - rootDir := viper.GetString(cli.HomeFlag) - keyDir := filepath.Join(rootDir, KeySubdir) - - // TODO: smarter loading??? with language and fallback? - codec := keys.MustLoadCodec("english") - - // and construct the key manager - manager = cryptostore.New( - cryptostore.SecretBox, - filestorage.New(keyDir), - codec, - ) - } - return manager +func init() { + RootCmd.AddCommand(getCmd) + RootCmd.AddCommand(listCmd) + RootCmd.AddCommand(newCmd) + RootCmd.AddCommand(updateCmd) +} + +func RegisterServer() { + RootCmd.AddCommand(serveCmd) } diff --git a/cmd/serve.go b/cmd/serve.go index 5ea96db3f..8cf7d12d4 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -28,6 +28,11 @@ import ( "github.com/tendermint/go-crypto/keys/server" ) +const ( + flagPort = "port" + flagSocket = "socket" +) + // serveCmd represents the serve command var serveCmd = &cobra.Command{ Use: "serve", @@ -36,27 +41,26 @@ var serveCmd = &cobra.Command{ private keys much more in depth than the cli can perform. In particular, this will allow you to sign transactions with the private keys in the store.`, - RunE: serveHTTP, + RunE: runServeCmd, } func init() { - RootCmd.AddCommand(serveCmd) - serveCmd.Flags().IntP("port", "p", 8118, "TCP Port for listen for http server") - serveCmd.Flags().StringP("socket", "s", "", "UNIX socket for more secure http server") - serveCmd.Flags().StringP("type", "t", "ed25519", "Default key type (ed25519|secp256k1)") + serveCmd.Flags().IntP(flagPort, "p", 8118, "TCP Port for listen for http server") + serveCmd.Flags().StringP(flagSocket, "s", "", "UNIX socket for more secure http server") + serveCmd.Flags().StringP(flagType, "t", "ed25519", "Default key type (ed25519|secp256k1)") } -func serveHTTP(cmd *cobra.Command, args []string) error { +func runServeCmd(cmd *cobra.Command, args []string) error { var l net.Listener var err error - socket := viper.GetString("socket") + socket := viper.GetString(flagSocket) if socket != "" { l, err = createSocket(socket) if err != nil { return errors.Wrap(err, "Cannot create socket") } } else { - port := viper.GetInt("port") + port := viper.GetInt(flagPort) l, err = net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { return errors.Errorf("Cannot listen on port %d", port) @@ -64,7 +68,7 @@ func serveHTTP(cmd *cobra.Command, args []string) error { } router := mux.NewRouter() - ks := server.New(GetKeyManager(), viper.GetString("type")) + ks := server.New(GetKeyManager(), viper.GetString(flagType)) ks.Register(router) // only set cors for tcp listener diff --git a/cmd/update.go b/cmd/update.go index c046af126..ec2bd2083 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -26,15 +26,10 @@ import ( var updateCmd = &cobra.Command{ Use: "update ", Short: "Change the password for a private key", - Long: `Change the password for a private key.`, - RunE: updatePassword, + RunE: runUpdateCmd, } -func init() { - RootCmd.AddCommand(updateCmd) -} - -func updatePassword(cmd *cobra.Command, args []string) error { +func runUpdateCmd(cmd *cobra.Command, args []string) error { if len(args) != 1 || len(args[0]) == 0 { return errors.New("You must provide a name for the key") } diff --git a/cmd/utils.go b/cmd/utils.go index d082bbbc9..e54343b7a 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "os" + "path/filepath" "strings" "github.com/bgentry/speakeasy" @@ -15,9 +16,31 @@ import ( "github.com/tendermint/tmlibs/cli" keys "github.com/tendermint/go-crypto/keys" + "github.com/tendermint/go-crypto/keys/cryptostore" + "github.com/tendermint/go-crypto/keys/storage/filestorage" ) -const PassLength = 10 +const MinPassLength = 10 + +// GetKeyManager initializes a key manager based on the configuration +func GetKeyManager() keys.Manager { + if manager == nil { + // store the keys directory + rootDir := viper.GetString(cli.HomeFlag) + keyDir := filepath.Join(rootDir, KeySubdir) + + // TODO: smarter loading??? with language and fallback? + codec := keys.MustLoadCodec("english") + + // and construct the key manager + manager = cryptostore.New( + cryptostore.SecretBox, + filestorage.New(keyDir), + codec, + ) + } + return manager +} // if we read from non-tty, we just need to init the buffer reader once, // in case we try to read multiple passwords (eg. update) @@ -47,8 +70,8 @@ func getPassword(prompt string) (pass string, err error) { if err != nil { return "", err } - if len(pass) < PassLength { - return "", errors.Errorf("Password must be at least %d characters", PassLength) + if len(pass) < MinPassLength { + return "", errors.Errorf("Password must be at least %d characters", MinPassLength) } return pass, nil }