[#749] neofs-adm: refactor password reading into a separate function

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-02-08 13:41:49 +03:00 committed by Alex Vanin
parent e21d054fe2
commit e288a0188d
2 changed files with 23 additions and 27 deletions

View file

@ -89,7 +89,7 @@ func TestGenerateAlphabet(t *testing.T) {
} }
t.Run("check contract group wallet", func(t *testing.T) { t.Run("check contract group wallet", func(t *testing.T) {
p := filepath.Join(walletDir, contractWalletName) p := filepath.Join(walletDir, contractWalletFilename)
w, err := wallet.NewWalletFromFile(p) w, err := wallet.NewWalletFromFile(p)
require.NoError(t, err, "contract wallet doesn't exist") require.NoError(t, err, "contract wallet doesn't exist")
require.Equal(t, 1, len(w.Accounts), "contract wallet must have 1 accout") require.Equal(t, 1, len(w.Accounts), "contract wallet must have 1 accout")

View file

@ -16,25 +16,18 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const contractWalletName = "contract.json" const (
contractWalletFilename = "contract.json"
contractWalletPasswordKey = "credentials.contract"
)
func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { func initializeContractWallet(walletDir string) (*wallet.Wallet, error) {
var ( password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ")
password string if err != nil {
err error return nil, err
)
if key := "credentials.contract"; viper.IsSet(key) {
password = viper.GetString(key)
} else {
prompt := "Password for contract wallet > "
password, err = input.ReadPassword(prompt)
if err != nil {
return nil, err
}
} }
w, err := wallet.NewWallet(path.Join(walletDir, contractWalletName)) w, err := wallet.NewWallet(path.Join(walletDir, contractWalletFilename))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -58,7 +51,7 @@ func initializeContractWallet(walletDir string) (*wallet.Wallet, error) {
} }
func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) { func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) {
p := path.Join(walletDir, contractWalletName) p := path.Join(walletDir, contractWalletFilename)
w, err := wallet.NewWalletFromFile(p) w, err := wallet.NewWalletFromFile(p)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
@ -66,22 +59,16 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e
} }
cmd.Printf("Contract group wallet is missing, initialize at %s\n", cmd.Printf("Contract group wallet is missing, initialize at %s\n",
filepath.Join(walletDir, contractWalletName)) filepath.Join(walletDir, contractWalletFilename))
w, err = initializeContractWallet(walletDir) w, err = initializeContractWallet(walletDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
var password string password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ")
if key := "credentials.contract"; viper.IsSet(key) { if err != nil {
password = viper.GetString(key) return nil, err
} else {
prompt := "Password for contract wallet > "
password, err = input.ReadPassword(prompt)
if err != nil {
return nil, fmt.Errorf("can't fetch password: %w", err)
}
} }
for i := range w.Accounts { for i := range w.Accounts {
@ -93,6 +80,15 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e
return w, nil return w, nil
} }
func getPassword(key string, promps string) (string, error) {
if viper.IsSet(key) {
return viper.GetString(key), nil
}
prompt := "Password for contract wallet > "
return input.ReadPassword(prompt)
}
func (c *initializeContext) addManifestGroup(h util.Uint160, cs *contractState) error { func (c *initializeContext) addManifestGroup(h util.Uint160, cs *contractState) error {
priv := c.ContractWallet.Accounts[0].PrivateKey() priv := c.ContractWallet.Accounts[0].PrivateKey()
pub := priv.PublicKey() pub := priv.PublicKey()