From 007158ebe19a85f1e4303f321cc295fecda9b63a Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 26 Jan 2022 10:50:32 +0300 Subject: [PATCH] [#1103] neofs-cli: allow to set password in config Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/modules/key_test.go | 10 ++++++++++ cmd/neofs-cli/modules/root.go | 13 +++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/neofs-cli/modules/key_test.go b/cmd/neofs-cli/modules/key_test.go index 5b35cef0e..ebb91730f 100644 --- a/cmd/neofs-cli/modules/key_test.go +++ b/cmd/neofs-cli/modules/key_test.go @@ -92,6 +92,16 @@ func Test_getKey(t *testing.T) { in.WriteString("pass\r") checkKey(t, nep2, nep2Key) + + t.Run("password from config", func(t *testing.T) { + viper.Set(password, "invalid") + in.WriteString("pass\r") + checkKeyError(t, nep2, errInvalidPassword) + + viper.Set(password, "pass") + in.WriteString("invalid\r") + checkKey(t, nep2, nep2Key) + }) }) t.Run("raw key", func(t *testing.T) { diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 0979a25af..fffeb3f7f 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -54,6 +54,8 @@ const ( addressDefault = "" addressUsage = "address of wallet account" + password = "password" + rpc = "rpc-endpoint" rpcShorthand = "r" rpcDefault = "" @@ -203,6 +205,13 @@ func getKey() (*ecdsa.PrivateKey, error) { return nil, errInvalidKey } +func getPassword() (string, error) { + if pass := viper.GetString(password); pass != "" { + return pass, nil + } + return input.ReadPassword("Enter password > ") +} + func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) { data, err := os.ReadFile(keyPath) if err != nil { @@ -218,7 +227,7 @@ func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) { } func getKeyFromNEP2(encryptedWif string) (*ecdsa.PrivateKey, error) { - pass, err := input.ReadPassword("Enter password > ") + pass, err := getPassword() if err != nil { printVerbose("Can't read password: %v", err) return nil, errInvalidPassword @@ -256,7 +265,7 @@ func getKeyFromWallet(w *wallet.Wallet, addrStr string) (*ecdsa.PrivateKey, erro return nil, errInvalidAddress } - pass, err := input.ReadPassword("Enter password > ") + pass, err := getPassword() if err != nil { printVerbose("Can't read password: %v", err) return nil, errInvalidPassword