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).
This commit is contained in:
Anna Shaleva 2023-01-26 07:11:57 +03:00
parent b56dff2b1e
commit 7e709313d3
2 changed files with 27 additions and 9 deletions

View file

@ -985,7 +985,12 @@ func newAccountFromWIF(w io.Writer, wif string, scrypt keys.ScryptParams, label
if pass != nil { if pass != nil {
phrase = *pass 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 name = *label
} }
// note: NEP2 strings always have length of 58 even though // 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.") 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 { if pass == nil {
phrase, err = readNewPassword() phrase, err = readNewPassword()
if err != nil { if err != nil {

View file

@ -356,9 +356,26 @@ func TestWalletInit(t *testing.T) {
t.Run("InvalidPassword", func(t *testing.T) { t.Run("InvalidPassword", func(t *testing.T) {
e.In.WriteString("password1\r") e.In.WriteString("password1\r")
e.RunWithError(t, "neo-go", "wallet", "import", "--wallet", walletPath, 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.In.WriteString("somepass\r")
e.Run(t, "neo-go", "wallet", "import", "--wallet", walletPath, e.Run(t, "neo-go", "wallet", "import", "--wallet", walletPath,
"--wif", acc.EncryptedWIF) "--wif", acc.EncryptedWIF)
@ -367,6 +384,7 @@ func TestWalletInit(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
actual := w.GetAccount(acc.PrivateKey().GetScriptHash()) actual := w.GetAccount(acc.PrivateKey().GetScriptHash())
require.NotNil(t, actual) require.NotNil(t, actual)
require.Equal(t, "acc2", actual.Label)
require.NoError(t, actual.Decrypt("somepass", w.Scrypt)) require.NoError(t, actual.Decrypt("somepass", w.Scrypt))
}) })
t.Run("EncryptedWIF with wallet config", func(t *testing.T) { 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.In.WriteString(pass + "\r")
} }
e.Run(t, "neo-go", "wallet", "import", "--wallet-config", configPath, e.Run(t, "neo-go", "wallet", "import", "--wallet-config", configPath,
"--wif", acc.EncryptedWIF) "--wif", acc.EncryptedWIF, "--name", "acc3"+configPass)
w, err := wallet.NewWalletFromFile(walletPath) w, err := wallet.NewWalletFromFile(walletPath)
require.NoError(t, err) require.NoError(t, err)
actual := w.GetAccount(acc.PrivateKey().GetScriptHash()) actual := w.GetAccount(acc.PrivateKey().GetScriptHash())
require.NotNil(t, actual) require.NotNil(t, actual)
require.Equal(t, "acc3"+configPass, actual.Label)
require.NoError(t, actual.Decrypt(pass, w.Scrypt)) require.NoError(t, actual.Decrypt(pass, w.Scrypt))
} }
t.Run("config password mismatch", func(t *testing.T) { t.Run("config password mismatch", func(t *testing.T) {