-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
wallet_api_listen_display.go
116 lines (99 loc) · 3.3 KB
/
wallet_api_listen_display.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package client
import (
"fmt"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/stellar1"
"golang.org/x/net/context"
)
const (
notifTypeWallet = "wallet"
sourcePayment = "payment"
sourcePaymentStatus = "payment_status"
sourceRequest = "request"
)
type walletNotification struct {
// wallet
Type string `json:"type"`
// payment, request, etc
Source string `json:"source"`
Notification interface{} `json:"notification,omitempty"`
Error *string `json:"error,omitempty"`
}
func newWalletNotification(source string) *walletNotification {
return &walletNotification{
Source: source,
Type: notifTypeWallet,
Notification: nil,
}
}
type walletNotificationDisplay struct {
*baseNotificationDisplay
cli stellar1.LocalClient
deduper map[string]bool
}
func newWalletNotificationDisplay(g *libkb.GlobalContext) *walletNotificationDisplay {
cli, err := GetWalletClient(g)
if err != nil {
panic(err.Error())
}
return &walletNotificationDisplay{
baseNotificationDisplay: newBaseNotificationDisplay(g),
cli: cli,
deduper: make(map[string]bool),
}
}
func deduperKey(details stellar1.PaymentDetailsLocal) string {
return fmt.Sprintf("%s:%s", details.Summary.TxID, details.Summary.StatusSimplified)
}
func (d *walletNotificationDisplay) displayPaymentDetails(ctx context.Context, source string,
accountID stellar1.AccountID, paymentID stellar1.PaymentID) error {
notif := newWalletNotification(source)
details, err := d.cli.GetPaymentDetailsLocal(ctx, stellar1.GetPaymentDetailsLocalArg{
AccountID: accountID,
Id: paymentID,
})
if err != nil {
errStr := err.Error()
notif.Error = &errStr
d.printJSON(notif)
return nil
}
if _, dupeMsg := d.deduper[deduperKey(details)]; dupeMsg {
return nil
}
d.deduper[deduperKey(details)] = true
notif.Notification = details
d.printJSON(notif)
return nil
}
func (d *walletNotificationDisplay) PaymentNotification(ctx context.Context, arg stellar1.PaymentNotificationArg) error {
return d.displayPaymentDetails(ctx, sourcePayment, arg.AccountID, arg.PaymentID)
}
func (d *walletNotificationDisplay) PaymentStatusNotification(ctx context.Context, arg stellar1.PaymentStatusNotificationArg) error {
return d.displayPaymentDetails(ctx, sourcePaymentStatus, arg.AccountID, arg.PaymentID)
}
func (d *walletNotificationDisplay) RequestStatusNotification(ctx context.Context, reqID stellar1.KeybaseRequestID) error {
notif := newWalletNotification(sourceRequest)
if details, err := d.cli.GetRequestDetailsLocal(ctx, stellar1.GetRequestDetailsLocalArg{
ReqID: reqID,
}); err != nil {
errStr := err.Error()
notif.Error = &errStr
} else {
notif.Notification = details
}
d.printJSON(notif)
return nil
}
func (d *walletNotificationDisplay) AccountDetailsUpdate(ctx context.Context, arg stellar1.AccountDetailsUpdateArg) error {
return nil
}
func (d *walletNotificationDisplay) AccountsUpdate(ctx context.Context, accounts []stellar1.WalletAccountLocal) error {
return nil
}
func (d *walletNotificationDisplay) PendingPaymentsUpdate(ctx context.Context, arg stellar1.PendingPaymentsUpdateArg) error {
return nil
}
func (d *walletNotificationDisplay) RecentPaymentsUpdate(ctx context.Context, arg stellar1.RecentPaymentsUpdateArg) error {
return nil
}