All keys logic works with new basecli

This commit is contained in:
Ethan Frey 2018-02-22 17:20:25 +01:00 committed by rigelrozanski
parent 7361269eb6
commit b0c65f8045
8 changed files with 49 additions and 103 deletions

View File

@ -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))
}
}

View File

@ -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 <name>",
Short: "Delete the given key",
RunE: runDeleteCmd,
}
return cmd
}
func runDeleteCmd(cmd *cobra.Command, args []string) error {

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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 <name>",
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")
}

View File

@ -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 <name>",
Short: "Change the password used to protect private key",
RunE: runUpdateCmd,
}
return cmd
}
func runUpdateCmd(cmd *cobra.Command, args []string) error {

View File

@ -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()
}