[#1103] neofs-cli: allow to set password in config

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-26 10:50:32 +03:00 committed by LeL
parent 0c46827a08
commit 007158ebe1
2 changed files with 21 additions and 2 deletions

View file

@ -92,6 +92,16 @@ func Test_getKey(t *testing.T) {
in.WriteString("pass\r") in.WriteString("pass\r")
checkKey(t, nep2, nep2Key) 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) { t.Run("raw key", func(t *testing.T) {

View file

@ -54,6 +54,8 @@ const (
addressDefault = "" addressDefault = ""
addressUsage = "address of wallet account" addressUsage = "address of wallet account"
password = "password"
rpc = "rpc-endpoint" rpc = "rpc-endpoint"
rpcShorthand = "r" rpcShorthand = "r"
rpcDefault = "" rpcDefault = ""
@ -203,6 +205,13 @@ func getKey() (*ecdsa.PrivateKey, error) {
return nil, errInvalidKey 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) { func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) {
data, err := os.ReadFile(keyPath) data, err := os.ReadFile(keyPath)
if err != nil { if err != nil {
@ -218,7 +227,7 @@ func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) {
} }
func getKeyFromNEP2(encryptedWif string) (*ecdsa.PrivateKey, error) { func getKeyFromNEP2(encryptedWif string) (*ecdsa.PrivateKey, error) {
pass, err := input.ReadPassword("Enter password > ") pass, err := getPassword()
if err != nil { if err != nil {
printVerbose("Can't read password: %v", err) printVerbose("Can't read password: %v", err)
return nil, errInvalidPassword return nil, errInvalidPassword
@ -256,7 +265,7 @@ func getKeyFromWallet(w *wallet.Wallet, addrStr string) (*ecdsa.PrivateKey, erro
return nil, errInvalidAddress return nil, errInvalidAddress
} }
pass, err := input.ReadPassword("Enter password > ") pass, err := getPassword()
if err != nil { if err != nil {
printVerbose("Can't read password: %v", err) printVerbose("Can't read password: %v", err)
return nil, errInvalidPassword return nil, errInvalidPassword