Skip to content

Commit eb2fb01

Browse files
patrickxbbuoyad
authored andcommitted
Handle account_change notification, forward to frontend (keybase#15460)
* Working on FetchSecretlessBundle in wallet state * Send notifications * Rip out * Add new notification * More cleanup * More cleanup * More * Fix test regression * Refactor * Consolidate display currency logic on opening send form (keybase#15443) * move display currency into account record * merge changes * all values if accounts loaded already * set to default currency on load if not set * asset input loading state * fix 'could not get exchange rate' * load accounts even less * accountsUpdate handler * remove disp currency load on set * storyshots * fix test
1 parent 64707e3 commit eb2fb01

25 files changed

Lines changed: 357 additions & 208 deletions

File tree

‎go/client/wallet_api_listen_display.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ func (d *walletNotificationDisplay) AccountDetailsUpdate(ctx context.Context, ar
8888
return nil
8989
}
9090

91+
func (d *walletNotificationDisplay) AccountsUpdate(ctx context.Context, accounts []stellar1.WalletAccountLocal) error {
92+
return nil
93+
}
94+
9195
func (d *walletNotificationDisplay) PendingPaymentsUpdate(ctx context.Context, arg stellar1.PendingPaymentsUpdateArg) error {
9296
return nil
9397
}

‎go/libkb/notify_router.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type NotifyListener interface {
8383
WalletPaymentStatusNotification(accountID stellar1.AccountID, paymentID stellar1.PaymentID)
8484
WalletRequestStatusNotification(reqID stellar1.KeybaseRequestID)
8585
WalletAccountDetailsUpdate(accountID stellar1.AccountID, account stellar1.WalletAccountLocal)
86+
WalletAccountsUpdate(accounts []stellar1.WalletAccountLocal)
8687
WalletPendingPaymentsUpdate(accountID stellar1.AccountID, pending []stellar1.PaymentOrErrorLocal)
8788
WalletRecentPaymentsUpdate(accountID stellar1.AccountID, firstPage stellar1.PaymentsPageLocal)
8889
TeamListUnverifiedChanged(teamName string)
@@ -177,6 +178,7 @@ func (n *NoopNotifyListener) WalletPaymentStatusNotification(accountID stellar1.
177178
func (n *NoopNotifyListener) WalletRequestStatusNotification(reqID stellar1.KeybaseRequestID) {}
178179
func (n *NoopNotifyListener) WalletAccountDetailsUpdate(accountID stellar1.AccountID, account stellar1.WalletAccountLocal) {
179180
}
181+
func (n *NoopNotifyListener) WalletAccountsUpdate(accounts []stellar1.WalletAccountLocal) {}
180182
func (n *NoopNotifyListener) WalletPendingPaymentsUpdate(accountID stellar1.AccountID, pending []stellar1.PaymentOrErrorLocal) {
181183
}
182184
func (n *NoopNotifyListener) WalletRecentPaymentsUpdate(accountID stellar1.AccountID, firstPage stellar1.PaymentsPageLocal) {
@@ -1291,6 +1293,29 @@ func (n *NotifyRouter) HandleWalletAccountDetailsUpdate(ctx context.Context, acc
12911293
n.G().Log.CDebugf(ctx, "- Sent wallet AccountDetailsUpdate")
12921294
}
12931295

1296+
func (n *NotifyRouter) HandleWalletAccountsUpdate(ctx context.Context, accounts []stellar1.WalletAccountLocal) {
1297+
if n == nil {
1298+
return
1299+
}
1300+
n.G().Log.CDebugf(ctx, "+ Sending wallet AccountsUpdate")
1301+
n.cm.ApplyAll(func(id ConnectionID, xp rpc.Transporter) bool {
1302+
// If the connection wants the `Wallet` notification type
1303+
if n.getNotificationChannels(id).Wallet {
1304+
// In the background do...
1305+
go func() {
1306+
(stellar1.NotifyClient{
1307+
Cli: rpc.NewClient(xp, NewContextifiedErrorUnwrapper(n.G()), nil),
1308+
}).AccountsUpdate(context.Background(), accounts)
1309+
}()
1310+
}
1311+
return true
1312+
})
1313+
if n.listener != nil {
1314+
n.listener.WalletAccountsUpdate(accounts)
1315+
}
1316+
n.G().Log.CDebugf(ctx, "- Sent wallet AccountsUpdate")
1317+
}
1318+
12941319
func (n *NotifyRouter) HandleWalletPendingPaymentsUpdate(ctx context.Context, accountID stellar1.AccountID, pending []stellar1.PaymentOrErrorLocal) {
12951320
if n == nil {
12961321
return

‎go/protocol/stellar1/extras.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
PushPaymentStatus = "stellar.payment_status"
2323
PushPaymentNotification = "stellar.payment_notification"
2424
PushRequestStatus = "stellar.request_status"
25+
PushAccountChange = "stellar.account_change"
2526
)
2627

2728
func KeybaseTransactionIDFromString(s string) (KeybaseTransactionID, error) {

‎go/protocol/stellar1/notify.go‎

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go/stellar/remote/interface.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ type Remoter interface {
1919
RecentPayments(ctx context.Context, accountID stellar1.AccountID, cursor *stellar1.PageCursor, limit int, skipPending bool) (stellar1.PaymentsPage, error)
2020
PendingPayments(ctx context.Context, accountID stellar1.AccountID, limit int) ([]stellar1.PaymentSummary, error)
2121
PaymentDetails(ctx context.Context, txID string) (res stellar1.PaymentDetails, err error)
22-
// GetAccountDisplayCurrency is not used as a mock now - since this
23-
// setting only lives in database table on the server, we can do full
24-
// integration testing here. Otherwise, all what ChangeDisplayCurrency
25-
// test would do is testing a mock.
2622
GetAccountDisplayCurrency(ctx context.Context, accountID stellar1.AccountID) (string, error)
2723
ExchangeRate(ctx context.Context, currency string) (stellar1.OutsideExchangeRate, error)
2824
SubmitRequest(ctx context.Context, post stellar1.RequestPost) (stellar1.KeybaseRequestID, error)

‎go/stellar/stellar.go‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,3 +1983,77 @@ func RandomBuildPaymentID() (stellar1.BuildPaymentID, error) {
19831983
}
19841984
return stellar1.BuildPaymentID("bb" + hex.EncodeToString(randBytes)), nil
19851985
}
1986+
1987+
func AllWalletAccounts(mctx libkb.MetaContext, remoter remote.Remoter) ([]stellar1.WalletAccountLocal, error) {
1988+
bundle, err := remote.FetchSecretlessBundle(mctx)
1989+
if err != nil {
1990+
return nil, err
1991+
}
1992+
1993+
var accts []stellar1.WalletAccountLocal
1994+
for _, entry := range bundle.Accounts {
1995+
acct, err := accountLocal(mctx, remoter, entry)
1996+
if err != nil {
1997+
return nil, err
1998+
}
1999+
2000+
accts = append(accts, acct)
2001+
}
2002+
2003+
// Put the primary account first, then sort by name, then by account ID
2004+
sort.SliceStable(accts, func(i, j int) bool {
2005+
if accts[i].IsDefault {
2006+
return true
2007+
}
2008+
if accts[j].IsDefault {
2009+
return false
2010+
}
2011+
if accts[i].Name == accts[j].Name {
2012+
return accts[i].AccountID < accts[j].AccountID
2013+
}
2014+
return accts[i].Name < accts[j].Name
2015+
})
2016+
2017+
return accts, nil
2018+
}
2019+
2020+
// WalletAccount returns stellar1.WalletAccountLocal for accountID.
2021+
func WalletAccount(mctx libkb.MetaContext, remoter remote.Remoter, accountID stellar1.AccountID) (stellar1.WalletAccountLocal, error) {
2022+
bundle, err := remote.FetchSecretlessBundle(mctx)
2023+
if err != nil {
2024+
return stellar1.WalletAccountLocal{}, err
2025+
}
2026+
entry, err := bundle.Lookup(accountID)
2027+
if err != nil {
2028+
return stellar1.WalletAccountLocal{}, err
2029+
}
2030+
2031+
return accountLocal(mctx, remoter, entry)
2032+
}
2033+
2034+
func accountLocal(mctx libkb.MetaContext, remoter remote.Remoter, entry stellar1.BundleEntry) (stellar1.WalletAccountLocal, error) {
2035+
var empty stellar1.WalletAccountLocal
2036+
details, err := AccountDetails(mctx, remoter, entry.AccountID)
2037+
if err != nil {
2038+
mctx.CDebugf("remote.Details failed for %q: %s", entry.AccountID, err)
2039+
return empty, err
2040+
}
2041+
2042+
return AccountDetailsToWalletAccountLocal(mctx, details, entry.IsPrimary, entry.Name)
2043+
}
2044+
2045+
// AccountDetails gets stellar1.AccountDetails for accountID.
2046+
//
2047+
// It has the side effect of updating the badge state with the
2048+
// stellar payment unread count for accountID.
2049+
func AccountDetails(mctx libkb.MetaContext, remoter remote.Remoter, accountID stellar1.AccountID) (stellar1.AccountDetails, error) {
2050+
details, err := remoter.Details(mctx.Ctx(), accountID)
2051+
details.SetDefaultDisplayCurrency()
2052+
if err != nil {
2053+
return details, err
2054+
}
2055+
2056+
mctx.G().GetStellar().UpdateUnreadCount(mctx.Ctx(), accountID, details.UnreadPayments)
2057+
2058+
return details, nil
2059+
}

‎go/stellar/stellargregor/stellargregor.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package stellargregor
22

33
import (
44
"context"
5+
"encoding/json"
6+
"errors"
57
"fmt"
68
"strings"
79

@@ -37,6 +39,8 @@ func (h *Handler) Create(ctx context.Context, cli gregor1.IncomingInterface, cat
3739
return true, h.paymentNotification(mctx, cli, category, item)
3840
case stellar1.PushRequestStatus:
3941
return true, h.requestStatus(mctx, cli, category, item)
42+
case stellar1.PushAccountChange:
43+
return true, h.accountChange(mctx, cli, category, item)
4044
default:
4145
if strings.HasPrefix(category, "stellar.") {
4246
return false, fmt.Errorf("unknown handler category: %q", category)
@@ -64,6 +68,47 @@ func (h *Handler) autoClaim(mctx libkb.MetaContext, cli gregor1.IncomingInterfac
6468
return nil
6569
}
6670

71+
type accountChangeMsg struct {
72+
AccountID string `json:"account_id"`
73+
Reason string `json:"reason"`
74+
}
75+
76+
func (h *Handler) accountChange(mctx libkb.MetaContext, cli gregor1.IncomingInterface, category string, item gregor.Item) error {
77+
mctx.CDebugf("%v: %v received", h.Name(), category)
78+
79+
if item.Body() == nil {
80+
return errors.New("stellar handler for account_change: nil message body")
81+
}
82+
var msgBody accountChangeMsg
83+
if err := json.Unmarshal(item.Body().Bytes(), &msgBody); err != nil {
84+
return err
85+
}
86+
87+
if msgBody.AccountID != "" {
88+
account, err := stellar.WalletAccount(mctx, h.walletState, stellar1.AccountID(msgBody.AccountID))
89+
if err == nil {
90+
h.G().NotifyRouter.HandleWalletAccountDetailsUpdate(mctx.Ctx(), stellar1.AccountID(msgBody.AccountID), account)
91+
} else {
92+
h.G().Log.CDebugf(mctx.Ctx(), "failed to HandleWalletAccountDetailsUpdate")
93+
}
94+
} else {
95+
accounts, err := stellar.AllWalletAccounts(mctx, h.walletState)
96+
if err == nil {
97+
h.G().NotifyRouter.HandleWalletAccountsUpdate(mctx.Ctx(), accounts)
98+
} else {
99+
h.G().Log.CDebugf(mctx.Ctx(), "failed to HandleWalletAccountsUpdate")
100+
}
101+
}
102+
103+
// We will locally dismiss for now so that each client only plays them once:
104+
if err := h.G().GregorDismisser.LocalDismissItem(mctx.Ctx(), item.Metadata().MsgID()); err != nil {
105+
h.G().Log.CDebugf(mctx.Ctx(), "failed to local dismiss account_change: %s", err)
106+
}
107+
108+
return nil
109+
}
110+
111+
// paymentStatus is an old IBM and shouldn't happen anymore
67112
func (h *Handler) paymentStatus(mctx libkb.MetaContext, cli gregor1.IncomingInterface, category string, item gregor.Item) error {
68113
mctx.CDebugf("%v: %v received IBM, ignoring it", h.Name(), category)
69114

@@ -75,6 +120,7 @@ func (h *Handler) paymentStatus(mctx libkb.MetaContext, cli gregor1.IncomingInte
75120
return nil
76121
}
77122

123+
// paymentNotification is an old IBM and shouldn't happen anymore
78124
func (h *Handler) paymentNotification(mctx libkb.MetaContext, cli gregor1.IncomingInterface, category string, item gregor.Item) error {
79125
mctx.CDebugf("%s: %s received IBM, ignoring it", h.Name(), category)
80126

@@ -86,6 +132,7 @@ func (h *Handler) paymentNotification(mctx libkb.MetaContext, cli gregor1.Incomi
86132
return nil
87133
}
88134

135+
// requestStatus is an old IBM and shouldn't happen anymore
89136
func (h *Handler) requestStatus(mctx libkb.MetaContext, cli gregor1.IncomingInterface, category string, item gregor.Item) error {
90137
mctx.CDebugf("%v: %v received IBM, ignoring it", h.Name(), category)
91138

‎go/stellar/stellarsvc/details.go‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎go/stellar/stellarsvc/frontend.go‎

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,7 @@ func (s *Server) GetWalletAccountsLocal(ctx context.Context, sessionID int) (acc
3030
return nil, err
3131
}
3232

33-
bundle, err := remote.FetchSecretlessBundle(mctx)
34-
if err != nil {
35-
return nil, err
36-
}
37-
38-
for _, entry := range bundle.Accounts {
39-
acct, err := s.accountLocal(mctx, entry)
40-
if err != nil {
41-
return nil, err
42-
}
43-
44-
accts = append(accts, acct)
45-
}
46-
47-
// Put the primary account first, then sort by name, then by account ID
48-
sort.SliceStable(accts, func(i, j int) bool {
49-
if accts[i].IsDefault {
50-
return true
51-
}
52-
if accts[j].IsDefault {
53-
return false
54-
}
55-
if accts[i].Name == accts[j].Name {
56-
return accts[i].AccountID < accts[j].AccountID
57-
}
58-
return accts[i].Name < accts[j].Name
59-
})
60-
61-
return accts, nil
33+
return stellar.AllWalletAccounts(mctx, s.remoter)
6234
}
6335

6436
func (s *Server) GetWalletAccountLocal(ctx context.Context, arg stellar1.GetWalletAccountLocalArg) (acct stellar1.WalletAccountLocal, err error) {
@@ -72,22 +44,12 @@ func (s *Server) GetWalletAccountLocal(ctx context.Context, arg stellar1.GetWall
7244
return acct, err
7345
}
7446

75-
bundle, err := remote.FetchSecretlessBundle(mctx)
76-
if err != nil {
77-
return acct, err
78-
}
79-
80-
entry, err := bundle.Lookup(arg.AccountID)
81-
if err != nil {
82-
return acct, err
83-
}
84-
85-
return s.accountLocal(mctx, entry)
47+
return stellar.WalletAccount(mctx, s.remoter, arg.AccountID)
8648
}
8749

8850
func (s *Server) accountLocal(mctx libkb.MetaContext, entry stellar1.BundleEntry) (stellar1.WalletAccountLocal, error) {
8951
var empty stellar1.WalletAccountLocal
90-
details, err := s.accountDetails(mctx.Ctx(), entry.AccountID)
52+
details, err := stellar.AccountDetails(mctx, s.remoter, entry.AccountID)
9153
if err != nil {
9254
mctx.CDebugf("remote.Details failed for %q: %s", entry.AccountID, err)
9355
return empty, err
@@ -106,7 +68,7 @@ func (s *Server) GetAccountAssetsLocal(ctx context.Context, arg stellar1.GetAcco
10668
return nil, err
10769
}
10870

109-
details, err := s.accountDetails(ctx, arg.AccountID)
71+
details, err := stellar.AccountDetails(mctx, s.remoter, arg.AccountID)
11072
if err != nil {
11173
s.G().Log.CDebugf(ctx, "remote.Details failed for %q: %s", arg.AccountID, err)
11274
return nil, err

‎go/stellar/stellarsvc/service_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ func TestSendLocalKeybase(t *testing.T) {
422422
}
423423
require.Equal(t, "9899.9999900", balances[0].Amount)
424424

425+
srvRecip.walletState.RefreshAll(tcs[1].MetaContext(), "test")
425426
balances, err = srvRecip.BalancesLocal(context.Background(), accountIDRecip)
426427
if err != nil {
427428
t.Fatal(err)

0 commit comments

Comments
 (0)