fix: corresponding keystore files are deleted when account is migrated to a keycard
This commit is contained in:
parent
fdf4c05968
commit
77b7ce5a09
|
@ -10,6 +10,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
|
@ -626,10 +627,11 @@ func TestConvertAccount(t *testing.T) {
|
|||
err = backend.AccountManager().InitKeystore(keyStoreDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
const path = "m/44'/60'/0'/0"
|
||||
backend.AccountManager()
|
||||
accs, err := backend.AccountManager().
|
||||
AccountsGenerator().
|
||||
GenerateAndDeriveAddresses(12, 1, "", []string{"m/44'/60'/0'/0"})
|
||||
GenerateAndDeriveAddresses(12, 1, "", []string{path})
|
||||
require.NoError(t, err)
|
||||
|
||||
generateAccount := accs[0]
|
||||
|
@ -663,10 +665,22 @@ func TestConvertAccount(t *testing.T) {
|
|||
SigningPhrase: "yurt joey vibe",
|
||||
WalletRootAddress: types.HexToAddress(accountInfo.Address)}
|
||||
|
||||
acc := accounts.Account{
|
||||
Address: types.HexToAddress(generateAccount.Address),
|
||||
KeyUID: generateAccount.KeyUID,
|
||||
Type: accounts.AccountTypeGenerated,
|
||||
PublicKey: types.Hex2Bytes(generateAccount.PublicKey),
|
||||
Path: path,
|
||||
Wallet: false,
|
||||
Chat: false,
|
||||
Name: "GeneratedAccount",
|
||||
}
|
||||
|
||||
accounts := []*accounts.Account{&acc}
|
||||
err = backend.saveAccountsAndSettings(
|
||||
s,
|
||||
¶ms.NodeConfig{},
|
||||
nil)
|
||||
accounts)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = backend.OpenAccounts()
|
||||
|
@ -690,13 +704,36 @@ func TestConvertAccount(t *testing.T) {
|
|||
KeycardPairing: "pairing",
|
||||
}
|
||||
|
||||
_, keyStoreErrBefore := os.Stat(keyStoreDir)
|
||||
addrWithoutPrefix := strings.ToLower(generateAccount.Address[2:len(generateAccount.Address)])
|
||||
found := false
|
||||
err = filepath.Walk(keyStoreDir, func(path string, fileInfo os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !fileInfo.IsDir() && strings.Contains(path, addrWithoutPrefix) {
|
||||
found = true
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, found)
|
||||
|
||||
err = backend.ConvertToKeycardAccount(keyStoreDir, keycardAccount, keycardSettings, password, keycardPassword)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, keyStoreErrAfter := os.Stat(keyStoreDir)
|
||||
require.True(t, keyStoreErrBefore == keyStoreErrAfter)
|
||||
err = filepath.Walk(keyStoreDir, func(path string, fileInfo os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !fileInfo.IsDir() && strings.Contains(path, addrWithoutPrefix) {
|
||||
found = false
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, found)
|
||||
|
||||
err = backend.ensureAppDBOpened(keycardAccount, keycardPassword)
|
||||
require.NoError(t, err)
|
||||
|
|
|
@ -598,12 +598,30 @@ func (b *GethStatusBackend) ConvertToKeycardAccount(keyStoreDir string, account
|
|||
return err
|
||||
}
|
||||
|
||||
knownAccounts, err := accountDB.GetAccounts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.closeAppDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return b.ChangeDatabasePassword(account.KeyUID, password, newPassword)
|
||||
err = b.ChangeDatabasePassword(account.KeyUID, password, newPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, acc := range knownAccounts {
|
||||
if account.KeyUID == acc.KeyUID {
|
||||
// This action deletes an account from the keystore, no need to check for error in this context here, cause if this
|
||||
// action fails from whichever reason the account is still successfully migrated since keystore won't be used any more.
|
||||
_ = b.accountManager.DeleteAccount(keyStoreDir, acc.Address)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) VerifyDatabasePassword(keyUID string, password string) error {
|
||||
|
|
|
@ -425,13 +425,24 @@ func (api *API) VerifyPassword(password string) bool {
|
|||
return err == nil
|
||||
}
|
||||
|
||||
func (api *API) AddMigratedKeyPair(ctx context.Context, kcUID string, kpName string, keyUID string, accountAddresses []string) error {
|
||||
func (api *API) AddMigratedKeyPair(ctx context.Context, kcUID string, kpName string, keyUID string, accountAddresses []string, keyStoreDir string) error {
|
||||
var addresses []types.Address
|
||||
for _, addr := range accountAddresses {
|
||||
addresses = append(addresses, types.Address(common.HexToAddress(addr)))
|
||||
}
|
||||
|
||||
return api.db.AddMigratedKeyPair(kcUID, kpName, keyUID, addresses)
|
||||
err := api.db.AddMigratedKeyPair(kcUID, kpName, keyUID, addresses)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Once we migrate a keypair, corresponding keystore files need to be deleted.
|
||||
for _, addr := range addresses {
|
||||
// This action deletes an account from the keystore, no need to check for error in this context here, cause if this
|
||||
// action fails from whichever reason the account is still successfully migrated since keystore won't be used any more.
|
||||
_ = api.manager.DeleteAccount(keyStoreDir, addr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api *API) GetAllMigratedKeyPairs(ctx context.Context) ([]*keypairs.KeyPair, error) {
|
||||
|
|
Loading…
Reference in New Issue