Skip to content

Commit

Permalink
[#722] neofs-adm: Unify pasword retrieval functions
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
  • Loading branch information
fyrchik authored and cthulhu-rider committed May 16, 2022
1 parent b685af4 commit 1c71956
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
7 changes: 3 additions & 4 deletions cmd/neofs-adm/internal/modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 5 additions & 4 deletions cmd/neofs-adm/internal/modules/morph/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
23 changes: 7 additions & 16 deletions cmd/neofs-adm/internal/modules/morph/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions cmd/neofs-adm/internal/modules/morph/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit 1c71956

Please sign in to comment.