Browse Source

Freshen up existing cmd files

pull/1782/head
Ethan Frey 8 years ago
parent
commit
e9537b2da6
8 changed files with 80 additions and 71 deletions
  1. +12
    -14
      cmd/get.go
  2. +3
    -0
      cmd/keys/main.go
  3. +7
    -9
      cmd/list.go
  4. +8
    -5
      cmd/new.go
  5. +9
    -24
      cmd/root.go
  6. +13
    -9
      cmd/serve.go
  7. +2
    -7
      cmd/update.go
  8. +26
    -3
      cmd/utils.go

+ 12
- 14
cmd/get.go View File

@ -25,20 +25,18 @@ var getCmd = &cobra.Command{
Use: "get <name>",
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
}

+ 3
- 0
cmd/keys/main.go View File

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

+ 7
- 9
cmd/list.go View File

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

+ 8
- 5
cmd/new.go View File

@ -21,6 +21,10 @@ import (
"github.com/spf13/viper"
)
const (
flagType = "type"
)
// newCmd represents the new command
var newCmd = &cobra.Command{
Use: "new <name>",
@ -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 {


+ 9
- 24
cmd/root.go View File

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

+ 13
- 9
cmd/serve.go View File

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


+ 2
- 7
cmd/update.go View File

@ -26,15 +26,10 @@ import (
var updateCmd = &cobra.Command{
Use: "update <name>",
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")
}


+ 26
- 3
cmd/utils.go View File

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


Loading…
Cancel
Save