From 69dd95d7483b73b06b107ae67ec1a4188b6fb66b Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 12 Oct 2020 17:35:04 +0300 Subject: [PATCH] [#78] Generate random key in CLI with `--key new` argument Signed-off-by: Alex Vanin --- cmd/neofs-cli/modules/root.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index c186e084d..54e4377bd 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -2,6 +2,8 @@ package cmd import ( "crypto/ecdsa" + "crypto/rand" + "encoding/hex" "errors" "fmt" "os" @@ -16,6 +18,8 @@ import ( "github.com/spf13/viper" ) +const generateKeyConst = "new" + // Global scope flags. var ( cfgFile string @@ -40,6 +44,7 @@ and much more!`, var ( errInvalidKey = errors.New("provided key is incorrect") errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect") + errCantGenerateKey = errors.New("can't generate new private key") ) // Execute adds all child commands to the root command and sets flags appropriately. @@ -58,7 +63,7 @@ func init() { // will be global for your application. rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)") - rootCmd.PersistentFlags().StringVarP(&privateKey, "key", "k", "", "private key in hex, WIF or filepath") + rootCmd.PersistentFlags().StringVarP(&privateKey, "key", "k", "", "private key in hex, WIF or filepath (use `--key new` to generate key for request)") rootCmd.PersistentFlags().StringVarP(&endpoint, "rpc-endpoint", "r", "", "remote node address (as 'multiaddr' or ':')") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") @@ -95,6 +100,21 @@ func initConfig() { // getKey returns private key that was provided in global arguments. func getKey() (*ecdsa.PrivateKey, error) { + if privateKey == generateKeyConst { + buf := make([]byte, crypto.PrivateKeyCompressedSize) + + _, err := rand.Read(buf) + if err != nil { + return nil, errCantGenerateKey + } + + if verbose { + fmt.Println("Generating private key:", hex.EncodeToString(buf)) + } + + return crypto.UnmarshalPrivateKey(buf) + } + key, err := crypto.LoadPrivateKey(privateKey) if err != nil { return nil, errInvalidKey