From 7e709313d30ef27fd7be0c4d53fb51504dc4f1be Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 26 Jan 2023 07:11:57 +0300 Subject: [PATCH] cli: ask for NEP2 account label if not provided If NEP2 account label is not provided as a flag for import-related CLI commands then ask it in the interactive mode. This change breaks the compatibility with the old behaviour (add the unnamed account if the name wasn't specified). --- cli/wallet/wallet.go | 13 ++++++------- cli/wallet/wallet_test.go | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 212092f47..f5051efb8 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -985,7 +985,12 @@ func newAccountFromWIF(w io.Writer, wif string, scrypt keys.ScryptParams, label if pass != nil { phrase = *pass } - if label != nil { + if label == nil { + name, err = readAccountName() + if err != nil { + return nil, fmt.Errorf("failed to read account label: %w", err) + } + } else { name = *label } // note: NEP2 strings always have length of 58 even though @@ -1024,12 +1029,6 @@ func newAccountFromWIF(w io.Writer, wif string, scrypt keys.ScryptParams, label } fmt.Fprintln(w, "Provided WIF was unencrypted. Wallet can contain only encrypted keys.") - if label == nil { - name, err = readAccountName() - if err != nil { - return nil, fmt.Errorf("failed to read account label: %w", err) - } - } if pass == nil { phrase, err = readNewPassword() if err != nil { diff --git a/cli/wallet/wallet_test.go b/cli/wallet/wallet_test.go index 7425237e9..f3387a336 100644 --- a/cli/wallet/wallet_test.go +++ b/cli/wallet/wallet_test.go @@ -356,9 +356,26 @@ func TestWalletInit(t *testing.T) { t.Run("InvalidPassword", func(t *testing.T) { e.In.WriteString("password1\r") e.RunWithError(t, "neo-go", "wallet", "import", "--wallet", walletPath, - "--wif", acc.EncryptedWIF) + "--wif", acc.EncryptedWIF, "--name", "acc1") }) + e.In.WriteString("somepass\r") + e.Run(t, "neo-go", "wallet", "import", "--wallet", walletPath, + "--wif", acc.EncryptedWIF, "--name", "acc1") + + w, err := wallet.NewWalletFromFile(walletPath) + require.NoError(t, err) + actual := w.GetAccount(acc.PrivateKey().GetScriptHash()) + require.NotNil(t, actual) + require.Equal(t, "acc1", actual.Label) + require.NoError(t, actual.Decrypt("somepass", w.Scrypt)) + }) + t.Run("EncryptedWIF with name specified via input", func(t *testing.T) { + acc, err := wallet.NewAccount() + require.NoError(t, err) + require.NoError(t, acc.Encrypt("somepass", keys.NEP2ScryptParams())) + + e.In.WriteString("acc2\r") e.In.WriteString("somepass\r") e.Run(t, "neo-go", "wallet", "import", "--wallet", walletPath, "--wif", acc.EncryptedWIF) @@ -367,6 +384,7 @@ func TestWalletInit(t *testing.T) { require.NoError(t, err) actual := w.GetAccount(acc.PrivateKey().GetScriptHash()) require.NotNil(t, actual) + require.Equal(t, "acc2", actual.Label) require.NoError(t, actual.Decrypt("somepass", w.Scrypt)) }) t.Run("EncryptedWIF with wallet config", func(t *testing.T) { @@ -388,12 +406,13 @@ func TestWalletInit(t *testing.T) { e.In.WriteString(pass + "\r") } e.Run(t, "neo-go", "wallet", "import", "--wallet-config", configPath, - "--wif", acc.EncryptedWIF) + "--wif", acc.EncryptedWIF, "--name", "acc3"+configPass) w, err := wallet.NewWalletFromFile(walletPath) require.NoError(t, err) actual := w.GetAccount(acc.PrivateKey().GetScriptHash()) require.NotNil(t, actual) + require.Equal(t, "acc3"+configPass, actual.Label) require.NoError(t, actual.Decrypt(pass, w.Scrypt)) } t.Run("config password mismatch", func(t *testing.T) {