From b2f188f8f0707866c0c1403288f12702fde46b67 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 4 Jul 2022 13:27:45 +0300 Subject: [PATCH] cli: refactor code that opens wallet Reduce code duplications, no functional changes. --- cli/wallet/nep17.go | 4 +-- cli/wallet/wallet.go | 74 +++++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/cli/wallet/nep17.go b/cli/wallet/nep17.go index a7135fffe..bd45db8f7 100644 --- a/cli/wallet/nep17.go +++ b/cli/wallet/nep17.go @@ -350,7 +350,7 @@ func importNEP17Token(ctx *cli.Context) error { } func importNEPToken(ctx *cli.Context, standard string) error { - wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config")) + wall, _, err := openWallet(ctx, true) if err != nil { return cli.NewExitError(err, 1) } @@ -446,7 +446,7 @@ func removeNEP17Token(ctx *cli.Context) error { } func removeNEPToken(ctx *cli.Context, standard string) error { - wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config")) + wall, _, err := openWallet(ctx, true) if err != nil { return cli.NewExitError(err, 1) } diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 6b52b0e1d..6cf9aba47 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -333,7 +333,7 @@ func claimGas(ctx *cli.Context) error { } func changePassword(ctx *cli.Context) error { - wall, _, err := openWallet(ctx.String("wallet"), "") + wall, _, err := openWallet(ctx, false) if err != nil { return cli.NewExitError(err, 1) } @@ -423,7 +423,7 @@ func convertWallet(ctx *cli.Context) error { } func addAccount(ctx *cli.Context) error { - wall, pass, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config")) + wall, pass, err := openWallet(ctx, true) if err != nil { return cli.NewExitError(err, 1) } @@ -499,7 +499,7 @@ loop: } func importMultisig(ctx *cli.Context) error { - wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config")) + wall, _, err := openWallet(ctx, true) if err != nil { return cli.NewExitError(err, 1) } @@ -541,7 +541,7 @@ func importMultisig(ctx *cli.Context) error { } func importDeployed(ctx *cli.Context) error { - wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config")) + wall, _, err := openWallet(ctx, true) if err != nil { return cli.NewExitError(err, 1) } @@ -596,7 +596,7 @@ func importDeployed(ctx *cli.Context) error { } func importWallet(ctx *cli.Context) error { - wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config")) + wall, _, err := openWallet(ctx, true) if err != nil { return cli.NewExitError(err, 1) } @@ -626,7 +626,7 @@ func importWallet(ctx *cli.Context) error { } func removeAccount(ctx *cli.Context) error { - wall, _, err := openWallet(ctx.String("wallet"), ctx.String("wallet-config")) + wall, _, err := openWallet(ctx, true) if err != nil { return cli.NewExitError(err, 1) } @@ -823,25 +823,14 @@ func createAccount(wall *wallet.Wallet, pass *string) error { return wall.CreateAccount(name, phrase) } -func openWallet(path string, configPath string) (*wallet.Wallet, *string, error) { - if len(path) != 0 && len(configPath) != 0 { - return nil, nil, errConflictingWalletFlags - } - if len(path) == 0 && len(configPath) == 0 { - return nil, nil, errNoPath +func openWallet(ctx *cli.Context, canUseWalletConfig bool) (*wallet.Wallet, *string, error) { + path, pass, err := getWalletPathAndPass(ctx, canUseWalletConfig) + if err != nil { + return nil, nil, err } if path == "-" { return nil, nil, errNoStdin } - var pass *string - if len(configPath) != 0 { - cfg, err := ReadWalletConfig(configPath) - if err != nil { - return nil, nil, err - } - path = cfg.Path - pass = &cfg.Password - } w, err := wallet.NewWalletFromFile(path) if err != nil { return nil, nil, err @@ -850,21 +839,9 @@ func openWallet(path string, configPath string) (*wallet.Wallet, *string, error) } func readWallet(ctx *cli.Context) (*wallet.Wallet, *string, error) { - path, configPath := ctx.String("wallet"), ctx.String("wallet-config") - if len(path) != 0 && len(configPath) != 0 { - return nil, nil, errConflictingWalletFlags - } - if len(path) == 0 && len(configPath) == 0 { - return nil, nil, errNoPath - } - var pass *string - if len(configPath) != 0 { - cfg, err := ReadWalletConfig(configPath) - if err != nil { - return nil, nil, err - } - path = cfg.Path - pass = &cfg.Password + path, pass, err := getWalletPathAndPass(ctx, true) + if err != nil { + return nil, nil, err } if path == "-" { w := &wallet.Wallet{} @@ -880,6 +857,31 @@ func readWallet(ctx *cli.Context) (*wallet.Wallet, *string, error) { return w, pass, nil } +// getWalletPathAndPass retrieves wallet path from context or from wallet configuration file. +// If wallet configuration file is specified, then account password is returned. +func getWalletPathAndPass(ctx *cli.Context, canUseWalletConfig bool) (string, *string, error) { + path, configPath := ctx.String("wallet"), ctx.String("wallet-config") + if !canUseWalletConfig && len(configPath) != 0 { + return "", nil, errors.New("can't use wallet configuration file for this command") + } + if len(path) != 0 && len(configPath) != 0 { + return "", nil, errConflictingWalletFlags + } + if len(path) == 0 && len(configPath) == 0 { + return "", nil, errNoPath + } + var pass *string + if len(configPath) != 0 { + cfg, err := ReadWalletConfig(configPath) + if err != nil { + return "", nil, err + } + path = cfg.Path + pass = &cfg.Password + } + return path, pass, nil +} + func ReadWalletConfig(configPath string) (*config.Wallet, error) { file, err := os.Open(configPath) if err != nil {