From 2fc63759586e48d44ea088ac051e8d1d7977c2ce 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 01ba11014..1433c1fe6 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -117,12 +117,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) } @@ -137,12 +132,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) } @@ -200,12 +190,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) } @@ -244,12 +229,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) } @@ -270,11 +250,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) } @@ -348,6 +324,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