Skip to content

Commit

Permalink
listen for bond expiry outside of the trading page and trigger a wall…
Browse files Browse the repository at this point in the history
…et unlock if necessary

Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed Jan 18, 2024
1 parent e2f8499 commit a3411fb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
63 changes: 50 additions & 13 deletions ui/page/root/home_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync/atomic"
"time"

"decred.org/dcrdex/client/core"
"decred.org/dcrdex/dex"
"gioui.org/io/key"
"gioui.org/layout"
Expand Down Expand Up @@ -173,10 +174,9 @@ func (hp *HomePage) OnNavigatedTo() {
hp.isBalanceHidden = hp.AssetsManager.IsTotalBalanceVisible()
}

type dexWalletLoginInfo struct {
type dexWalletUnlockInfo struct {
passEditor cryptomaterial.Editor
walletName string
walletID int
wallet sharedW.Asset
}

// initDEX initializes a new dex client if dex is not ready.
Expand All @@ -198,10 +198,10 @@ func (hp *HomePage) initDEX() {
<-dexc.Ready()

loginDEXNow := dexc.Active()
walletsToUnlock := make(map[string]*dexWalletLoginInfo)
walletsToUnlock := make(map[string]*dexWalletUnlockInfo)
for _, xc := range dexc.Exchanges() {
for _, bond := range xc.Auth.PendingBonds {
loginInfo, err := hp.fetchDEXWalletLoginInfo(bond.AssetID)
loginInfo, err := hp.dexWalletUnlockInfo(bond.AssetID)
if err != nil {
log.Error(err)
continue
Expand All @@ -210,7 +210,7 @@ func (hp *HomePage) initDEX() {
}

// Always unlock current bond asset.
loginInfo, err := hp.fetchDEXWalletLoginInfo(xc.Auth.BondAssetID)
loginInfo, err := hp.dexWalletUnlockInfo(xc.Auth.BondAssetID)
if err != nil {
log.Error(err)
continue
Expand All @@ -235,7 +235,7 @@ func (hp *HomePage) initDEX() {
UseCustomWidget(func(gtx C) D {
extra := ""
if len(walletsToUnlock) > 0 {
extra = values.StringF(values.StrLoginDEXForBondWallet, len(walletsToUnlock))
extra = values.StringF(values.StrUnlockBondWalletMsg, len(walletsToUnlock))
}

msg := extra
Expand Down Expand Up @@ -282,7 +282,7 @@ func (hp *HomePage) initDEX() {
for _, w := range walletsToUnlock {
w.passEditor.SetError("")

err := hp.AssetsManager.WalletWithID(w.walletID).UnlockWallet(w.passEditor.Editor.Text())
err := w.wallet.UnlockWallet(w.passEditor.Editor.Text())
if err != nil {
w.passEditor.SetError(err.Error())
return false
Expand All @@ -293,10 +293,48 @@ func (hp *HomePage) initDEX() {
}).
SetCancelable(false)
hp.ParentWindow().ShowModal(loginModal)

noteFeeder := dexc.NotificationFeed()
for {
select {
case <-hp.dexCtx.Done():
noteFeeder.ReturnFeed()
return
case n := <-noteFeeder.C:
if n.Topic() != core.TopicBondExpired {
continue // we only care about expired bond atm.
}

bondNote := n.(*core.BondPostNote)
w, err := hp.dexWalletUnlockInfo(bondNote.Auth.BondAssetID)
if err != nil {
log.Error(err)
continue
}

if !w.wallet.IsLocked() {
continue // nothing to do
}

// Unlock wallet now.
passModal := modal.NewPasswordModal(hp.Load).
Title(values.String(values.StrUnlockWithPassword)).
Description(values.StringF(values.StrUnlockBondWalletMsg, 1))
passModal.PositiveButton(values.String(values.StrUnlock), func(password string, m *modal.PasswordModal) bool {
err := w.wallet.UnlockWallet(password)
if err != nil {
m.SetError(err.Error())
m.SetLoading(false)
return false
}
return true
})
}
}
}()
}

func (hp *HomePage) fetchDEXWalletLoginInfo(assetID uint32) (*dexWalletLoginInfo, error) {
func (hp *HomePage) dexWalletUnlockInfo(assetID uint32) (*dexWalletUnlockInfo, error) {
dexClient := hp.AssetsManager.DexClient()
settings, err := dexClient.WalletSettings(assetID)
if err != nil {
Expand All @@ -314,12 +352,11 @@ func (hp *HomePage) fetchDEXWalletLoginInfo(assetID uint32) (*dexWalletLoginInfo
return nil, fmt.Errorf("dex wallet with ID %d is missing", walletID)
}

wl := &dexWalletLoginInfo{
walletID: walletID,
walletName: wallet.GetWalletName(),
wl := &dexWalletUnlockInfo{
wallet: wallet,
}

wl.passEditor = hp.Theme.EditorPassword(new(widget.Editor), values.StringF(values.StrSpendingPasswordFor, wl.walletName))
wl.passEditor = hp.Theme.EditorPassword(new(widget.Editor), values.StringF(values.StrSpendingPasswordFor, wallet.GetWalletName()))
wl.passEditor.Editor.SingleLine, wl.passEditor.IsRequired = true, true
return wl, nil
}
Expand Down
2 changes: 1 addition & 1 deletion ui/values/localizable/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,6 @@ const EN = `
"loginToTradeDEX" = "Please login with your DEX password."
"login" = "Login"
"loginDEXForActiveOrders" = "You have one or more active order, login to DEX now! %s"
"loginDEXForBondWallet" = "%d wallet(s) need unlocking to process your bonds."
"unlockBondWalletMsg" = "%d wallet(s) need unlocking to process your DEX bonds."
"spendingPasswordFor" = "Spending Password for %s"
`
2 changes: 1 addition & 1 deletion ui/values/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,6 @@ const (
StrLoginToTradeDEX = "loginToTradeDEX"
StrLogin = "login"
StrLoginDEXForActiveOrders = "loginDEXForActiveOrders"
StrLoginDEXForBondWallet = "loginDEXForBondWallet"
StrUnlockBondWalletMsg = "unlockBondWalletMsg"
StrSpendingPasswordFor = "spendingPasswordFor"
)

0 comments on commit a3411fb

Please sign in to comment.