From b0c65f804570154e1310f21269d7253f58135348 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 22 Feb 2018 17:20:25 +0100 Subject: [PATCH] All keys logic works with new basecli --- client/keys/add.go | 5 +- client/keys/delete.go | 12 +++-- client/keys/list.go | 4 +- client/keys/recover.go | 66 --------------------------- client/keys/root.go | 37 ++++++++------- client/keys/{get.go => show.go} | 11 ++--- client/keys/update.go | 12 +++-- examples/basecoin/cmd/basecli/main.go | 5 +- 8 files changed, 49 insertions(+), 103 deletions(-) delete mode 100644 client/keys/recover.go rename client/keys/{get.go => show.go} (83%) diff --git a/client/keys/add.go b/client/keys/add.go index f6f1698b7b..88d998323f 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -105,7 +105,8 @@ type addOutput struct { } func printCreate(info keys.Info, seed string) { - switch viper.Get(cli.OutputFlag) { + output := viper.Get(cli.OutputFlag) + switch output { case "text": printInfo(info) // print seed unless requested not to. @@ -124,5 +125,7 @@ func printCreate(info keys.Info, seed string) { panic(err) // really shouldn't happen... } fmt.Println(string(json)) + default: + panic(fmt.Sprintf("I can't speak: %s", output)) } } diff --git a/client/keys/delete.go b/client/keys/delete.go index 91f39e6a11..ce5a03dd42 100644 --- a/client/keys/delete.go +++ b/client/keys/delete.go @@ -22,11 +22,13 @@ import ( "github.com/spf13/cobra" ) -// deleteCmd represents the delete command -var deleteCmd = &cobra.Command{ - Use: "delete [name]", - Short: "DANGER: Delete a private key from your system", - RunE: runDeleteCmd, +func deleteKeyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete ", + Short: "Delete the given key", + RunE: runDeleteCmd, + } + return cmd } func runDeleteCmd(cmd *cobra.Command, args []string) error { diff --git a/client/keys/list.go b/client/keys/list.go index 8654deca68..8e11d36afc 100644 --- a/client/keys/list.go +++ b/client/keys/list.go @@ -16,8 +16,8 @@ package keys import "github.com/spf13/cobra" -// listCmd represents the list command -var listCmd = &cobra.Command{ +// listKeysCmd represents the list command +var listKeysCmd = &cobra.Command{ Use: "list", Short: "List all keys", Long: `Return a list of all public keys stored by this key manager diff --git a/client/keys/recover.go b/client/keys/recover.go deleted file mode 100644 index cd78eab8e6..0000000000 --- a/client/keys/recover.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright © 2017 Ethan Frey -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package keys - -import ( - "github.com/pkg/errors" - - "github.com/spf13/cobra" -) - -// recoverCmd represents the recover command -var recoverCmd = &cobra.Command{ - Use: "recover [name]", - Short: "Recover a private key from a seed phrase", - Long: `Recover a private key from a seed phrase. - -I really hope you wrote this down when you created the new key. -The seed is only displayed on creation, never again. - -You can also use this to copy a key between multiple testnets, -simply by "recovering" the key in the other nets you want to copy -to. Of course, it has no coins on the other nets, just the same address.`, - RunE: runRecoverCmd, -} - -func runRecoverCmd(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] - - pass, err := getPassword("Enter the new passphrase:") - if err != nil { - return err - } - - // not really a password... huh? - seed, err := getSeed("Enter your recovery seed phrase:") - if err != nil { - return err - } - - kb, err := GetKeyBase() - if err != nil { - return err - } - - info, err := kb.Recover(name, pass, seed) - if err != nil { - return err - } - printInfo(info) - return nil -} diff --git a/client/keys/root.go b/client/keys/root.go index 1336272b35..0dcf050402 100644 --- a/client/keys/root.go +++ b/client/keys/root.go @@ -18,22 +18,27 @@ import ( "github.com/spf13/cobra" ) -// RootCmd represents the base command when called without any subcommands -var RootCmd = &cobra.Command{ - Use: "keys", - Short: "Key manager for tendermint clients", - Long: `Keys allows you to manage your local keystore for tendermint. +var lineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} -These keys may be in any format supported by go-crypto and can be -used by light-clients, full nodes, or any other application that -needs to sign with a private key.`, -} +// Commands registers a sub-tree of commands to interact with +// local private key storage. +func Commands() *cobra.Command { + cmd := &cobra.Command{ + Use: "keys", + Short: "Add or view local private keys", + Long: `Keys allows you to manage your local keystore for tendermint. -func init() { - RootCmd.AddCommand(getCmd) - RootCmd.AddCommand(listCmd) - RootCmd.AddCommand(newCmd) - RootCmd.AddCommand(updateCmd) - RootCmd.AddCommand(deleteCmd) - RootCmd.AddCommand(recoverCmd) + These keys may be in any format supported by go-crypto and can be + used by light-clients, full nodes, or any other application that + needs to sign with a private key.`, + } + cmd.AddCommand( + addKeyCommand(), + listKeysCmd, + showKeysCmd, + lineBreak, + deleteKeyCommand(), + updateKeyCommand(), + ) + return cmd } diff --git a/client/keys/get.go b/client/keys/show.go similarity index 83% rename from client/keys/get.go rename to client/keys/show.go index e247473b2e..e569f265de 100644 --- a/client/keys/get.go +++ b/client/keys/show.go @@ -20,15 +20,14 @@ import ( "github.com/spf13/cobra" ) -// getCmd represents the get command -var getCmd = &cobra.Command{ - Use: "get [name]", - Short: "Get details of one key", +var showKeysCmd = &cobra.Command{ + Use: "show ", + Short: "Show key info for the given name", Long: `Return public details of one local key.`, - RunE: runGetCmd, + RunE: runShowCmd, } -func runGetCmd(cmd *cobra.Command, args []string) error { +func runShowCmd(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/client/keys/update.go b/client/keys/update.go index de8d06a525..59010a6d3e 100644 --- a/client/keys/update.go +++ b/client/keys/update.go @@ -22,11 +22,13 @@ import ( "github.com/spf13/cobra" ) -// updateCmd represents the update command -var updateCmd = &cobra.Command{ - Use: "update [name]", - Short: "Change the password for a private key", - RunE: runUpdateCmd, +func updateKeyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "update ", + Short: "Change the password used to protect private key", + RunE: runUpdateCmd, + } + return cmd } func runUpdateCmd(cmd *cobra.Command, args []string) error { diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 1989d1b38d..3a785124a6 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -8,6 +8,7 @@ import ( "github.com/tendermint/tmlibs/cli" + "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/version" ) @@ -48,12 +49,12 @@ func main() { basecliCmd.AddCommand( lineBreak, serveCommand(), - KeyCommands(), + keys.Commands(), lineBreak, version.VersionCmd, ) // prepare and add flags - executor := cli.PrepareBaseCmd(basecliCmd, "GA", os.ExpandEnv("$HOME/.basecli")) + executor := cli.PrepareMainCmd(basecliCmd, "BC", os.ExpandEnv("$HOME/.basecli")) executor.Execute() }