Merge pull request #2889 from nspcc-dev/ask-nep2-label

cli: ask for NEP2 account label if not provided
This commit is contained in:
Roman Khimov 2023-04-06 10:42:52 +03:00 committed by GitHub
commit a80d7bef80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 {
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 {

View file

@ -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) {