Skip to content

Commit

Permalink
verify a wallet name is available before allowing users to input thei…
Browse files Browse the repository at this point in the history
…r seed
  • Loading branch information
bugjt committed Sep 23, 2024
1 parent 647f8b5 commit 9171eae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
13 changes: 13 additions & 0 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions ui/page/components/wallet_setup_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ui/page/wallet/single_wallet_main_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}))
}

0 comments on commit 9171eae

Please sign in to comment.