mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-03 14:41:37 +00:00
Freshen up existing cmd files
This commit is contained in:
26
cmd/get.go
26
cmd/get.go
@ -25,20 +25,18 @@ var getCmd = &cobra.Command{
|
|||||||
Use: "get <name>",
|
Use: "get <name>",
|
||||||
Short: "Get details of one key",
|
Short: "Get details of one key",
|
||||||
Long: `Return public details of one local key.`,
|
Long: `Return public details of one local key.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: runGetCmd,
|
||||||
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
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func runGetCmd(cmd *cobra.Command, args []string) error {
|
||||||
RootCmd.AddCommand(getCmd)
|
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
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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 := cli.PrepareMainCmd(cmd.RootCmd, "TM", os.ExpandEnv("$HOME/.tlc"))
|
||||||
root.Execute()
|
root.Execute()
|
||||||
}
|
}
|
||||||
|
16
cmd/list.go
16
cmd/list.go
@ -22,15 +22,13 @@ var listCmd = &cobra.Command{
|
|||||||
Short: "List all keys",
|
Short: "List all keys",
|
||||||
Long: `Return a list of all public keys stored by this key manager
|
Long: `Return a list of all public keys stored by this key manager
|
||||||
along with their associated name and address.`,
|
along with their associated name and address.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: runListCmd,
|
||||||
infos, err := GetKeyManager().List()
|
|
||||||
if err == nil {
|
|
||||||
printInfos(infos)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func runListCmd(cmd *cobra.Command, args []string) error {
|
||||||
RootCmd.AddCommand(listCmd)
|
infos, err := GetKeyManager().List()
|
||||||
|
if err == nil {
|
||||||
|
printInfos(infos)
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
13
cmd/new.go
13
cmd/new.go
@ -21,6 +21,10 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
flagType = "type"
|
||||||
|
)
|
||||||
|
|
||||||
// newCmd represents the new command
|
// newCmd represents the new command
|
||||||
var newCmd = &cobra.Command{
|
var newCmd = &cobra.Command{
|
||||||
Use: "new <name>",
|
Use: "new <name>",
|
||||||
@ -28,20 +32,19 @@ var newCmd = &cobra.Command{
|
|||||||
Long: `Add a public/private key pair to the key store.
|
Long: `Add a public/private key pair to the key store.
|
||||||
The password muts be entered in the terminal and not
|
The password muts be entered in the terminal and not
|
||||||
passed as a command line argument for security.`,
|
passed as a command line argument for security.`,
|
||||||
RunE: newPassword,
|
RunE: runNewCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.AddCommand(newCmd)
|
newCmd.Flags().StringP(flagType, "t", "ed25519", "Type of key (ed25519|secp256k1)")
|
||||||
newCmd.Flags().StringP("type", "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 {
|
if len(args) != 1 || len(args[0]) == 0 {
|
||||||
return errors.New("You must provide a name for the key")
|
return errors.New("You must provide a name for the key")
|
||||||
}
|
}
|
||||||
name := args[0]
|
name := args[0]
|
||||||
algo := viper.GetString("type")
|
algo := viper.GetString(flagType)
|
||||||
|
|
||||||
pass, err := getCheckPassword("Enter a passphrase:", "Repeat the passphrase:")
|
pass, err := getCheckPassword("Enter a passphrase:", "Repeat the passphrase:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
33
cmd/root.go
33
cmd/root.go
@ -15,14 +15,8 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
|
||||||
keys "github.com/tendermint/go-crypto/keys"
|
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"
|
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.`,
|
needs to sign with a private key.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetKeyManager initializes a key manager based on the configuration
|
func init() {
|
||||||
func GetKeyManager() keys.Manager {
|
RootCmd.AddCommand(getCmd)
|
||||||
if manager == nil {
|
RootCmd.AddCommand(listCmd)
|
||||||
// store the keys directory
|
RootCmd.AddCommand(newCmd)
|
||||||
rootDir := viper.GetString(cli.HomeFlag)
|
RootCmd.AddCommand(updateCmd)
|
||||||
keyDir := filepath.Join(rootDir, KeySubdir)
|
}
|
||||||
|
|
||||||
// TODO: smarter loading??? with language and fallback?
|
func RegisterServer() {
|
||||||
codec := keys.MustLoadCodec("english")
|
RootCmd.AddCommand(serveCmd)
|
||||||
|
|
||||||
// and construct the key manager
|
|
||||||
manager = cryptostore.New(
|
|
||||||
cryptostore.SecretBox,
|
|
||||||
filestorage.New(keyDir),
|
|
||||||
codec,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return manager
|
|
||||||
}
|
}
|
||||||
|
22
cmd/serve.go
22
cmd/serve.go
@ -28,6 +28,11 @@ import (
|
|||||||
"github.com/tendermint/go-crypto/keys/server"
|
"github.com/tendermint/go-crypto/keys/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
flagPort = "port"
|
||||||
|
flagSocket = "socket"
|
||||||
|
)
|
||||||
|
|
||||||
// serveCmd represents the serve command
|
// serveCmd represents the serve command
|
||||||
var serveCmd = &cobra.Command{
|
var serveCmd = &cobra.Command{
|
||||||
Use: "serve",
|
Use: "serve",
|
||||||
@ -36,27 +41,26 @@ var serveCmd = &cobra.Command{
|
|||||||
private keys much more in depth than the cli can perform.
|
private keys much more in depth than the cli can perform.
|
||||||
In particular, this will allow you to sign transactions with
|
In particular, this will allow you to sign transactions with
|
||||||
the private keys in the store.`,
|
the private keys in the store.`,
|
||||||
RunE: serveHTTP,
|
RunE: runServeCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.AddCommand(serveCmd)
|
serveCmd.Flags().IntP(flagPort, "p", 8118, "TCP Port for listen for http server")
|
||||||
serveCmd.Flags().IntP("port", "p", 8118, "TCP Port for listen for http server")
|
serveCmd.Flags().StringP(flagSocket, "s", "", "UNIX socket for more secure http server")
|
||||||
serveCmd.Flags().StringP("socket", "s", "", "UNIX socket for more secure http server")
|
serveCmd.Flags().StringP(flagType, "t", "ed25519", "Default key type (ed25519|secp256k1)")
|
||||||
serveCmd.Flags().StringP("type", "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 l net.Listener
|
||||||
var err error
|
var err error
|
||||||
socket := viper.GetString("socket")
|
socket := viper.GetString(flagSocket)
|
||||||
if socket != "" {
|
if socket != "" {
|
||||||
l, err = createSocket(socket)
|
l, err = createSocket(socket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Cannot create socket")
|
return errors.Wrap(err, "Cannot create socket")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
port := viper.GetInt("port")
|
port := viper.GetInt(flagPort)
|
||||||
l, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
|
l, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Errorf("Cannot listen on port %d", port)
|
return errors.Errorf("Cannot listen on port %d", port)
|
||||||
@ -64,7 +68,7 @@ func serveHTTP(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
ks := server.New(GetKeyManager(), viper.GetString("type"))
|
ks := server.New(GetKeyManager(), viper.GetString(flagType))
|
||||||
ks.Register(router)
|
ks.Register(router)
|
||||||
|
|
||||||
// only set cors for tcp listener
|
// only set cors for tcp listener
|
||||||
|
@ -26,15 +26,10 @@ import (
|
|||||||
var updateCmd = &cobra.Command{
|
var updateCmd = &cobra.Command{
|
||||||
Use: "update <name>",
|
Use: "update <name>",
|
||||||
Short: "Change the password for a private key",
|
Short: "Change the password for a private key",
|
||||||
Long: `Change the password for a private key.`,
|
RunE: runUpdateCmd,
|
||||||
RunE: updatePassword,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func runUpdateCmd(cmd *cobra.Command, args []string) error {
|
||||||
RootCmd.AddCommand(updateCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func updatePassword(cmd *cobra.Command, args []string) error {
|
|
||||||
if len(args) != 1 || len(args[0]) == 0 {
|
if len(args) != 1 || len(args[0]) == 0 {
|
||||||
return errors.New("You must provide a name for the key")
|
return errors.New("You must provide a name for the key")
|
||||||
}
|
}
|
||||||
|
29
cmd/utils.go
29
cmd/utils.go
@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bgentry/speakeasy"
|
"github.com/bgentry/speakeasy"
|
||||||
@ -15,9 +16,31 @@ import (
|
|||||||
"github.com/tendermint/tmlibs/cli"
|
"github.com/tendermint/tmlibs/cli"
|
||||||
|
|
||||||
keys "github.com/tendermint/go-crypto/keys"
|
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,
|
// 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)
|
// 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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if len(pass) < PassLength {
|
if len(pass) < MinPassLength {
|
||||||
return "", errors.Errorf("Password must be at least %d characters", PassLength)
|
return "", errors.Errorf("Password must be at least %d characters", MinPassLength)
|
||||||
}
|
}
|
||||||
return pass, nil
|
return pass, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user