From 5c837e71ba2bcef1a00e83db84fb47f59a4564e8 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 21 Feb 2020 11:28:49 +0300 Subject: [PATCH] cli: provide separate function for opening wallet --- cli/wallet/wallet.go | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 6184490a43..7a29177589 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -114,12 +114,7 @@ func NewCommands() []cli.Command { } func addAccount(ctx *cli.Context) error { - path := ctx.String("path") - if len(path) == 0 { - return cli.NewExitError(errNoPath, 1) - } - - wall, err := wallet.NewWalletFromFile(path) + wall, err := openWallet(ctx.String("path")) if err != nil { return cli.NewExitError(err, 1) } @@ -134,12 +129,7 @@ func addAccount(ctx *cli.Context) error { } func exportKeys(ctx *cli.Context) error { - path := ctx.String("path") - if len(path) == 0 { - return cli.NewExitError(errNoPath, 1) - } - - wall, err := wallet.NewWalletFromFile(path) + wall, err := openWallet(ctx.String("path")) if err != nil { return cli.NewExitError(err, 1) } @@ -197,12 +187,7 @@ loop: } func importMultisig(ctx *cli.Context) error { - path := ctx.String("path") - if len(path) == 0 { - return cli.NewExitError(errNoPath, 1) - } - - wall, err := wallet.NewWalletFromFile(path) + wall, err := openWallet(ctx.String("path")) if err != nil { return cli.NewExitError(err, 1) } @@ -241,12 +226,7 @@ func importMultisig(ctx *cli.Context) error { } func importWallet(ctx *cli.Context) error { - path := ctx.String("path") - if len(path) == 0 { - return cli.NewExitError(errNoPath, 1) - } - - wall, err := wallet.NewWalletFromFile(path) + wall, err := openWallet(ctx.String("path")) if err != nil { return cli.NewExitError(err, 1) } @@ -267,11 +247,7 @@ func importWallet(ctx *cli.Context) error { } func dumpWallet(ctx *cli.Context) error { - path := ctx.String("path") - if len(path) == 0 { - return cli.NewExitError(errNoPath, 1) - } - wall, err := wallet.NewWalletFromFile(path) + wall, err := openWallet(ctx.String("path")) if err != nil { return cli.NewExitError(err, 1) } @@ -345,6 +321,13 @@ func createAccount(ctx *cli.Context, wall *wallet.Wallet) error { return wall.CreateAccount(name, phrase) } +func openWallet(path string) (*wallet.Wallet, error) { + if len(path) == 0 { + return nil, errNoPath + } + return wallet.NewWalletFromFile(path) +} + func newAccountFromWIF(wif string) (*wallet.Account, error) { // note: NEP2 strings always have length of 58 even though // base58 strings can have different lengths even if slice lengths are equal