[#722] neofs-adm: Unify pasword retrieval functions

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-04-08 17:58:21 +03:00 committed by LeL
parent b685af487f
commit 1c7195666c
4 changed files with 21 additions and 29 deletions

View file

@ -144,13 +144,12 @@ func generateConfigExample(appDir string, credSize int) (string, error) {
return buf.String(), nil return buf.String(), nil
} }
func AlphabetPassword(v *viper.Viper, index int) (string, error) { func GetPassword(v *viper.Viper, name string) (string, error) {
letter := innerring.GlagoliticLetter(index) key := "credentials." + name
key := "credentials." + letter.String()
if v.IsSet(key) { if v.IsSet(key) {
return v.GetString(key), nil return v.GetString(key), nil
} }
prompt := "Password for " + letter.String() + " wallet > " prompt := "Password for " + name + " wallet > "
return input.ReadPassword(prompt) return input.ReadPassword(prompt)
} }

View file

@ -38,13 +38,14 @@ func generateAlphabetCreds(cmd *cobra.Command, args []string) error {
return errors.New("size must be > 0") return errors.New("size must be > 0")
} }
v := viper.GetViper()
walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag)) walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag))
pwds, err := initializeWallets(walletDir, int(size)) pwds, err := initializeWallets(v, walletDir, int(size))
if err != nil { if err != nil {
return err return err
} }
w, err := initializeContractWallet(walletDir) w, err := initializeContractWallet(v, walletDir)
if err != nil { if err != nil {
return err return err
} }
@ -59,13 +60,13 @@ func generateAlphabetCreds(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func initializeWallets(walletDir string, size int) ([]string, error) { func initializeWallets(v *viper.Viper, walletDir string, size int) ([]string, error) {
wallets := make([]*wallet.Wallet, size) wallets := make([]*wallet.Wallet, size)
pubs := make(keys.PublicKeys, size) pubs := make(keys.PublicKeys, size)
passwords := make([]string, size) passwords := make([]string, size)
for i := range wallets { for i := range wallets {
password, err := config.AlphabetPassword(viper.GetViper(), i) password, err := config.GetPassword(v, innerring.GlagoliticLetter(i).String())
if err != nil { if err != nil {
return nil, fmt.Errorf("can't fetch password: %w", err) return nil, fmt.Errorf("can't fetch password: %w", err)
} }

View file

@ -6,22 +6,22 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/nspcc-dev/neo-go/cli/input"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const ( const (
contractWalletFilename = "contract.json" contractWalletFilename = "contract.json"
contractWalletPasswordKey = "credentials.contract" contractWalletPasswordKey = "contract"
) )
func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { func initializeContractWallet(v *viper.Viper, walletDir string) (*wallet.Wallet, error) {
password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") password, err := config.GetPassword(v, contractWalletPasswordKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -49,7 +49,7 @@ func initializeContractWallet(walletDir string) (*wallet.Wallet, error) {
return w, nil return w, nil
} }
func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) { func openContractWallet(v *viper.Viper, cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) {
p := filepath.Join(walletDir, contractWalletFilename) p := filepath.Join(walletDir, contractWalletFilename)
w, err := wallet.NewWalletFromFile(p) w, err := wallet.NewWalletFromFile(p)
if err != nil { if err != nil {
@ -58,10 +58,10 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e
} }
cmd.Printf("Contract group wallet is missing, initialize at %s\n", p) cmd.Printf("Contract group wallet is missing, initialize at %s\n", p)
return initializeContractWallet(walletDir) return initializeContractWallet(v, walletDir)
} }
password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") password, err := config.GetPassword(v, contractWalletPasswordKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -75,15 +75,6 @@ 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()

View file

@ -111,12 +111,12 @@ func (c *initializeContext) close() {
func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) { func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) {
walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag)) walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag))
wallets, err := openAlphabetWallets(walletDir) wallets, err := openAlphabetWallets(v, walletDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
w, err := openContractWallet(cmd, walletDir) w, err := openContractWallet(v, cmd, walletDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -207,7 +207,7 @@ func (c *initializeContext) nativeHash(name string) util.Uint160 {
return c.Natives[name] return c.Natives[name]
} }
func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) { func openAlphabetWallets(v *viper.Viper, walletDir string) ([]*wallet.Wallet, error) {
walletFiles, err := os.ReadDir(walletDir) walletFiles, err := os.ReadDir(walletDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err) return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err)
@ -231,13 +231,14 @@ loop:
wallets := make([]*wallet.Wallet, size) wallets := make([]*wallet.Wallet, size)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
p := filepath.Join(walletDir, innerring.GlagoliticLetter(i).String()+".json") letter := innerring.GlagoliticLetter(i).String()
p := filepath.Join(walletDir, letter+".json")
w, err := wallet.NewWalletFromFile(p) w, err := wallet.NewWalletFromFile(p)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't open wallet: %w", err) return nil, fmt.Errorf("can't open wallet: %w", err)
} }
password, err := config.AlphabetPassword(viper.GetViper(), i) password, err := config.GetPassword(v, letter)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't fetch password: %w", err) return nil, fmt.Errorf("can't fetch password: %w", err)
} }