From 9171eae99e437f7d48e0581bc343e8a11ba37a38 Mon Sep 17 00:00:00 2001 From: bugjt <@bugjt> Date: Mon, 23 Sep 2024 08:59:33 +0700 Subject: [PATCH] verify a wallet name is available before allowing users to input their seed --- libwallet/assets_manager.go | 13 +++++++++++++ ui/page/components/wallet_setup_page.go | 21 +++++++++++++++++++++ ui/page/wallet/single_wallet_main_page.go | 4 ++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/libwallet/assets_manager.go b/libwallet/assets_manager.go index 0e74aafeb..a382f9633 100644 --- a/libwallet/assets_manager.go +++ b/libwallet/assets_manager.go @@ -27,6 +27,7 @@ import ( "github.com/crypto-power/cryptopower/libwallet/assets/btc" "github.com/crypto-power/cryptopower/libwallet/assets/dcr" "github.com/crypto-power/cryptopower/libwallet/assets/ltc" + "github.com/crypto-power/cryptopower/libwallet/assets/wallet" sharedW "github.com/crypto-power/cryptopower/libwallet/assets/wallet" ) @@ -615,6 +616,18 @@ func (mgr *AssetsManager) DeleteBadWallet(walletID int) error { return nil } +// Check if wallet name is already exists +func (mgr *AssetsManager) WalletNameIsExists(walletName string) (bool, error) { + wallet := wallet.Wallet{} + err := mgr.params.DB.One("Name", walletName, &wallet) + if err == nil { + return true, nil + } else if err != storm.ErrNotFound { + return false, err + } + return false, nil +} + // RootDirFileSizeInBytes returns the total directory size of // Assets Manager's root directory in bytes. func (mgr *AssetsManager) RootDirFileSizeInBytes(dataDir string) (int64, error) { diff --git a/ui/page/components/wallet_setup_page.go b/ui/page/components/wallet_setup_page.go index 022a3cfe1..8b9588743 100644 --- a/ui/page/components/wallet_setup_page.go +++ b/ui/page/components/wallet_setup_page.go @@ -434,6 +434,9 @@ func (pg *CreateWallet) handleEditorEvents(gtx C) { // create wallet action if (pg.continueBtn.Clicked(gtx) || isSubmit) && pg.validCreateWalletInputs() { + if pg.checkWalletNameExists() { + return + } go pg.createWallet() } @@ -565,6 +568,10 @@ func (pg *CreateWallet) HandleUserInteractions(gtx C) { // restore wallet actions if pg.restoreBtn.Clicked(gtx) && pg.validRestoreWalletInputs() { + if pg.checkWalletNameExists() { + return + } + afterRestore := func(newWallet sharedW.Asset) { // todo setup mixer for restored accounts automatically pg.walletCreationSuccessCallback(newWallet) @@ -574,6 +581,20 @@ func (pg *CreateWallet) HandleUserInteractions(gtx C) { } } +func (pg *CreateWallet) checkWalletNameExists() bool { + walletName := pg.walletName.Editor.Text() + exists, err := pg.AssetsManager.WalletNameIsExists(walletName) + if err != nil { + pg.walletName.SetError(err.Error()) + return true + } + if exists { + pg.walletName.SetError(values.StringF(values.StrWalletExist, walletName)) + return true + } + return false +} + func (pg *CreateWallet) passwordsMatch(editors ...*widget.Editor) bool { if len(editors) < 2 { return false diff --git a/ui/page/wallet/single_wallet_main_page.go b/ui/page/wallet/single_wallet_main_page.go index cd4c1e498..a6e93b1dd 100644 --- a/ui/page/wallet/single_wallet_main_page.go +++ b/ui/page/wallet/single_wallet_main_page.go @@ -858,8 +858,8 @@ func (swmp *SingleWalletMasterPage) showBackupInfo() { func (swmp *SingleWalletMasterPage) backup(wallet sharedW.Asset) { currentPage := swmp.ParentWindow().CurrentPageID() - swmp.ParentWindow().Display(seedbackup.NewBackupInstructionsPage(swmp.Load, swmp.selectedWallet, func(_ *load.Load, navigator app.WindowNavigator) { - swmp.selectedWallet.SaveUserConfigValue(sharedW.SeedBackupNotificationConfigKey, true) + swmp.ParentWindow().Display(seedbackup.NewBackupInstructionsPage(swmp.Load, wallet, func(_ *load.Load, navigator app.WindowNavigator) { + wallet.SaveUserConfigValue(sharedW.SeedBackupNotificationConfigKey, true) navigator.ClosePagesAfter(currentPage) })) }