diff --git a/cmd/neofs-adm/internal/modules/config/config.go b/cmd/neofs-adm/internal/modules/config/config.go index 53b18602..1a9ba65c 100644 --- a/cmd/neofs-adm/internal/modules/config/config.go +++ b/cmd/neofs-adm/internal/modules/config/config.go @@ -144,13 +144,12 @@ func generateConfigExample(appDir string, credSize int) (string, error) { return buf.String(), nil } -func AlphabetPassword(v *viper.Viper, index int) (string, error) { - letter := innerring.GlagoliticLetter(index) - key := "credentials." + letter.String() +func GetPassword(v *viper.Viper, name string) (string, error) { + key := "credentials." + name if v.IsSet(key) { return v.GetString(key), nil } - prompt := "Password for " + letter.String() + " wallet > " + prompt := "Password for " + name + " wallet > " return input.ReadPassword(prompt) } diff --git a/cmd/neofs-adm/internal/modules/morph/generate.go b/cmd/neofs-adm/internal/modules/morph/generate.go index 7c7bd758..7b2b5ec6 100644 --- a/cmd/neofs-adm/internal/modules/morph/generate.go +++ b/cmd/neofs-adm/internal/modules/morph/generate.go @@ -38,13 +38,14 @@ func generateAlphabetCreds(cmd *cobra.Command, args []string) error { return errors.New("size must be > 0") } + v := viper.GetViper() walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag)) - pwds, err := initializeWallets(walletDir, int(size)) + pwds, err := initializeWallets(v, walletDir, int(size)) if err != nil { return err } - w, err := initializeContractWallet(walletDir) + w, err := initializeContractWallet(v, walletDir) if err != nil { return err } @@ -59,13 +60,13 @@ func generateAlphabetCreds(cmd *cobra.Command, args []string) error { 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) pubs := make(keys.PublicKeys, size) passwords := make([]string, size) for i := range wallets { - password, err := config.AlphabetPassword(viper.GetViper(), i) + password, err := config.GetPassword(v, innerring.GlagoliticLetter(i).String()) if err != nil { return nil, fmt.Errorf("can't fetch password: %w", err) } diff --git a/cmd/neofs-adm/internal/modules/morph/group.go b/cmd/neofs-adm/internal/modules/morph/group.go index 3f48eb3d..99ddd38f 100644 --- a/cmd/neofs-adm/internal/modules/morph/group.go +++ b/cmd/neofs-adm/internal/modules/morph/group.go @@ -6,22 +6,22 @@ import ( "os" "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/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/util" "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/viper" ) const ( contractWalletFilename = "contract.json" - contractWalletPasswordKey = "credentials.contract" + contractWalletPasswordKey = "contract" ) -func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { - password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") +func initializeContractWallet(v *viper.Viper, walletDir string) (*wallet.Wallet, error) { + password, err := config.GetPassword(v, contractWalletPasswordKey) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { 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) w, err := wallet.NewWalletFromFile(p) 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) - return initializeContractWallet(walletDir) + return initializeContractWallet(v, walletDir) } - password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") + password, err := config.GetPassword(v, contractWalletPasswordKey) if err != nil { return nil, err } @@ -75,15 +75,6 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e 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 { priv := c.ContractWallet.Accounts[0].PrivateKey() pub := priv.PublicKey() diff --git a/cmd/neofs-adm/internal/modules/morph/initialize.go b/cmd/neofs-adm/internal/modules/morph/initialize.go index 6fed2df9..a325640a 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize.go @@ -111,12 +111,12 @@ func (c *initializeContext) close() { func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) { walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag)) - wallets, err := openAlphabetWallets(walletDir) + wallets, err := openAlphabetWallets(v, walletDir) if err != nil { return nil, err } - w, err := openContractWallet(cmd, walletDir) + w, err := openContractWallet(v, cmd, walletDir) if err != nil { return nil, err } @@ -207,7 +207,7 @@ func (c *initializeContext) nativeHash(name string) util.Uint160 { 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) if err != nil { return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err) @@ -231,13 +231,14 @@ loop: wallets := make([]*wallet.Wallet, size) 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) if err != nil { 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 { return nil, fmt.Errorf("can't fetch password: %w", err) }