-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
build.go
1135 lines (1037 loc) · 35.5 KB
/
build.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package stellar
import (
"fmt"
"regexp"
"strings"
"sync"
"time"
"github.com/keybase/client/go/stellar/remote"
"github.com/keybase/client/go/engine"
"github.com/keybase/client/go/externals"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/client/go/protocol/stellar1"
"github.com/keybase/client/go/slotctx"
"github.com/keybase/client/go/stellar/stellarcommon"
"github.com/keybase/stellarnet"
stellarAddress "github.com/stellar/go/address"
)
func ShouldOfferAdvancedSend(mctx libkb.MetaContext, remoter remote.Remoter, from, to stellar1.AccountID) (shouldShow stellar1.AdvancedBanner, err error) {
theirBalances, err := remoter.Balances(mctx.Ctx(), to)
if err != nil {
return stellar1.AdvancedBanner_NO_BANNER, err
}
for _, bal := range theirBalances {
if !bal.Asset.IsNativeXLM() {
return stellar1.AdvancedBanner_RECEIVER_BANNER, nil
}
}
// Lookup our assets
ourBalances, err := remoter.Balances(mctx.Ctx(), from)
if err != nil {
return stellar1.AdvancedBanner_NO_BANNER, err
}
for _, bal := range ourBalances {
asset := bal.Asset
if !asset.IsNativeXLM() {
return stellar1.AdvancedBanner_SENDER_BANNER, nil
}
}
// Neither of us have non-native assets so return false
return stellar1.AdvancedBanner_NO_BANNER, nil
}
func GetSendAssetChoicesLocal(mctx libkb.MetaContext, remoter remote.Remoter, arg stellar1.GetSendAssetChoicesLocalArg) (res []stellar1.SendAssetChoiceLocal, err error) {
owns, _, err := OwnAccount(mctx, arg.From)
if err != nil {
return res, err
}
if !owns {
return res, fmt.Errorf("account %s is not owned by current user", arg.From)
}
ourBalances, err := remoter.Balances(mctx.Ctx(), arg.From)
if err != nil {
return res, err
}
res = []stellar1.SendAssetChoiceLocal{}
for _, bal := range ourBalances {
asset := bal.Asset
if asset.IsNativeXLM() {
// We are only doing non-native assets here.
continue
}
choice := stellar1.SendAssetChoiceLocal{
Asset: asset,
Enabled: true,
Left: bal.Asset.Code,
Right: bal.Asset.Issuer,
}
res = append(res, choice)
}
if arg.To != "" {
recipient, err := LookupRecipient(mctx, stellarcommon.RecipientInput(arg.To), false)
if err != nil {
mctx.G().Log.CDebugf(mctx.Ctx(), "Skipping asset filtering: LookupRecipient for %q failed with: %s",
arg.To, err)
return res, nil
}
theirBalancesHash := make(map[string]bool)
assetHashCode := func(a stellar1.Asset) string {
return fmt.Sprintf("%s%s%s", a.Type, a.Code, a.Issuer)
}
if recipient.AccountID != nil {
theirBalances, err := remoter.Balances(mctx.Ctx(), stellar1.AccountID(recipient.AccountID.String()))
if err != nil {
mctx.G().Log.CDebugf(mctx.Ctx(), "Skipping asset filtering: remoter.Balances for %q failed with: %s",
recipient.AccountID, err)
return res, nil
}
for _, bal := range theirBalances {
theirBalancesHash[assetHashCode(bal.Asset)] = true
}
}
for i, choice := range res {
available := theirBalancesHash[assetHashCode(choice.Asset)]
if !available {
choice.Enabled = false
recipientStr := "Recipient"
if recipient.User != nil {
recipientStr = recipient.User.Username.String()
}
choice.Subtext = fmt.Sprintf("%s does not accept %s", recipientStr, choice.Asset.Code)
res[i] = choice
}
}
}
return res, nil
}
func StartBuildPaymentLocal(mctx libkb.MetaContext) (res stellar1.BuildPaymentID, err error) {
return getGlobal(mctx.G()).startBuildPayment(mctx)
}
func StopBuildPaymentLocal(mctx libkb.MetaContext, bid stellar1.BuildPaymentID) {
getGlobal(mctx.G()).stopBuildPayment(mctx, bid)
}
func BuildPaymentLocal(mctx libkb.MetaContext, arg stellar1.BuildPaymentLocalArg) (res stellar1.BuildPaymentResLocal, err error) {
tracer := mctx.G().CTimeTracer(mctx.Ctx(), "BuildPaymentLocal", true)
defer tracer.Finish()
var data *buildPaymentData
var release func()
if arg.Bid.IsNil() {
// Compatibility for pre-bid gui and tests.
mctx = mctx.WithCtx(
getGlobal(mctx.G()).buildPaymentSlot.Use(mctx.Ctx(), arg.SessionID))
} else {
mctx, data, release, err = getGlobal(mctx.G()).acquireBuildPayment(mctx, arg.Bid, arg.SessionID)
defer release()
if err != nil {
return res, err
}
// Mark the payment as not ready to send while the new values are validated.
data.ReadyToReview = false
data.ReadyToSend = false
data.Frozen = nil
}
readyChecklist := struct {
from bool
to bool
amount bool
secretNote bool
publicMemo bool
}{}
log := func(format string, args ...interface{}) {
mctx.Debug("bpl: "+format, args...)
}
bpc := getGlobal(mctx.G()).getBuildPaymentCache()
if bpc == nil {
return res, fmt.Errorf("missing build payment cache")
}
// -------------------- from --------------------
tracer.Stage("from")
fromInfo := struct {
available bool
from stellar1.AccountID
}{}
if arg.FromPrimaryAccount != arg.From.IsNil() {
// Exactly one of `arg.From` and `arg.FromPrimaryAccount` must be set.
return res, fmt.Errorf("invalid build payment parameters")
}
fromPrimaryAccount := arg.FromPrimaryAccount
if arg.FromPrimaryAccount {
primaryAccountID, err := bpc.PrimaryAccount(mctx)
if err != nil {
log("PrimaryAccount -> err:[%T] %v", err, err)
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "error",
Message: fmt.Sprintf("Could not find primary account.%v", msgMore(err)),
})
} else {
fromInfo.from = primaryAccountID
fromInfo.available = true
}
} else {
owns, fromPrimary, err := getGlobal(mctx.G()).OwnAccountCached(mctx, arg.From)
if err != nil || !owns {
log("OwnsAccount (from) -> owns:%v err:[%T] %v", owns, err, err)
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "error",
Message: fmt.Sprintf("Could not find source account.%v", msgMore(err)),
})
} else {
fromInfo.from = arg.From
fromInfo.available = true
fromPrimaryAccount = fromPrimary
}
}
if fromInfo.available {
res.From = fromInfo.from
readyChecklist.from = true
}
// -------------------- to --------------------
tracer.Stage("to")
var recipientUV keybase1.UserVersion
skipRecipient := len(arg.To) == 0
var minAmountXLM string
if !skipRecipient && arg.ToIsAccountID {
_, err := libkb.ParseStellarAccountID(arg.To)
if err != nil {
res.ToErrMsg = err.Error()
skipRecipient = true
} else {
readyChecklist.to = true
}
}
if !skipRecipient {
recipient, err := bpc.LookupRecipient(mctx, stellarcommon.RecipientInput(arg.To))
if err != nil {
log("error with recipient field %v: %v", arg.To, err)
res.ToErrMsg = "Recipient not found."
} else {
bannerThey := "they"
bannerTheir := "their"
if recipient.User != nil && !arg.ToIsAccountID {
bannerThey = recipient.User.Username.String()
bannerTheir = fmt.Sprintf("%s's", recipient.User.Username)
recipientUV = recipient.User.UV
}
if recipient.AccountID == nil && fromInfo.available && !fromPrimaryAccount {
// This would have been a relay from a non-primary account.
// We cannot allow that.
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "error",
Message: fmt.Sprintf("Because %v hasn’t set up their wallet yet, you can only send to them from your default account.", bannerThey),
})
} else {
readyChecklist.to = true
addMinBanner := func(them, amount string) {
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "info",
Message: fmt.Sprintf("Because it's %s first transaction, you must send at least %s XLM.", them, amount),
})
}
var sendingToSelf bool
var selfSendErr error
if recipient.AccountID == nil {
// Sending a payment to a target with no account. (relay)
minAmountXLM = "2.01"
addMinBanner(bannerTheir, minAmountXLM)
} else {
sendingToSelf, _, selfSendErr = getGlobal(mctx.G()).OwnAccountCached(mctx, stellar1.AccountID(recipient.AccountID.String()))
isFunded, err := bpc.IsAccountFunded(mctx, stellar1.AccountID(recipient.AccountID.String()), arg.Bid)
if err != nil {
log("error checking recipient funding status %v: %v", *recipient.AccountID, err)
} else if !isFunded {
// Sending to a non-funded stellar account.
minAmountXLM = "1"
log("OwnsAccount (to) -> owns:%v err:%v", sendingToSelf, selfSendErr)
if !sendingToSelf || selfSendErr != nil {
// Likely sending to someone else's account.
addMinBanner(bannerTheir, minAmountXLM)
} else {
// Sending to our own account.
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "info",
Message: fmt.Sprintf("Because it's the first transaction on your receiving account, you must send at least %v XLM.", minAmountXLM),
})
}
}
}
if fromInfo.available && !sendingToSelf && !fromPrimaryAccount {
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "info",
Message: "Your Keybase username will not be linked to this transaction.",
})
}
if recipient.AccountID != nil {
tracer.Stage("offer advanced send")
if fromInfo.available {
offerAdvancedForm, err := bpc.ShouldOfferAdvancedSend(mctx, fromInfo.from, stellar1.AccountID(*recipient.AccountID))
if err == nil {
if offerAdvancedForm != stellar1.AdvancedBanner_NO_BANNER {
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "info",
OfferAdvancedSendForm: offerAdvancedForm,
})
}
} else {
log("error determining whether to offer the advanced send page: %v", err)
}
} else {
log("failed to determine from address while determining whether to offer the advanced send page")
}
}
if recipient.HasMemo() {
res.PublicMemoOverride = *recipient.PublicMemo
log("recipient has federation public memo override: %q", res.PublicMemoOverride)
}
}
}
}
// -------------------- amount + asset --------------------
tracer.Stage("amount + asset")
bpaArg := buildPaymentAmountArg{
Bid: arg.Bid,
Amount: arg.Amount,
Currency: arg.Currency,
Asset: arg.Asset,
}
if fromInfo.available {
bpaArg.From = &fromInfo.from
}
amountX := buildPaymentAmountHelper(mctx, bpc, bpaArg)
res.AmountErrMsg = amountX.amountErrMsg
res.WorthDescription = amountX.worthDescription
res.WorthInfo = amountX.worthInfo
res.WorthCurrency = amountX.worthCurrency
res.DisplayAmountXLM = amountX.displayAmountXLM
res.DisplayAmountFiat = amountX.displayAmountFiat
res.SendingIntentionXLM = amountX.sendingIntentionXLM
if amountX.haveAmount {
if !amountX.asset.IsNativeXLM() {
return res, fmt.Errorf("sending non-XLM assets is not supported")
}
readyChecklist.amount = true
if fromInfo.available {
// Check that the sender has enough asset available.
// Note: When adding support for sending non-XLM assets, check the asset instead of XLM here.
availableToSendXLM, err := bpc.AvailableXLMToSend(mctx, fromInfo.from)
if err != nil {
log("error getting available balance: %v", err)
} else {
baseFee := getGlobal(mctx.G()).BaseFee(mctx)
availableToSendXLM = SubtractFeeSoft(mctx, availableToSendXLM, baseFee)
availableToSendFormatted := availableToSendXLM + " XLM"
availableToSendXLMFmt, err := FormatAmount(mctx,
availableToSendXLM, false, stellarnet.Truncate)
if err == nil {
availableToSendFormatted = availableToSendXLMFmt + " XLM"
}
if arg.Currency != nil && amountX.rate != nil {
// If the user entered an amount in outside currency and an exchange
// rate is available, attempt to show them available balance in that currency.
availableToSendOutside, err := stellarnet.ConvertXLMToOutside(availableToSendXLM, amountX.rate.Rate)
if err != nil {
log("error converting available-to-send", err)
} else {
formattedATS, err := FormatCurrencyWithCodeSuffix(mctx,
availableToSendOutside, amountX.rate.Currency, stellarnet.Truncate)
if err != nil {
log("error formatting available-to-send", err)
} else {
availableToSendFormatted = formattedATS
}
}
}
cmp, err := stellarnet.CompareStellarAmounts(availableToSendXLM, amountX.amountOfAsset)
switch {
case err != nil:
log("error comparing amounts (%v) (%v): %v", availableToSendXLM, amountX.amountOfAsset, err)
case cmp == -1:
log("Send amount is more than available to send %v > %v", amountX.amountOfAsset, availableToSendXLM)
readyChecklist.amount = false // block sending
available, err := stellarnet.ParseStellarAmount(availableToSendXLM)
if err != nil {
mctx.Debug("error parsing available balance: %v", err)
available = 0
}
if available <= 0 { // Don't show "You only have 0 worth of Lumens"
if arg.Currency != nil {
res.AmountErrMsg = fmt.Sprintf("You have *%s* worth of Lumens available to send.", availableToSendFormatted)
} else {
res.AmountErrMsg = fmt.Sprintf("You have *%s* available to send.", availableToSendFormatted)
}
} else {
if arg.Currency != nil {
res.AmountErrMsg = fmt.Sprintf("You only have *%s* worth of Lumens available to send.", availableToSendFormatted)
} else {
res.AmountErrMsg = fmt.Sprintf("You only have *%s* available to send.", availableToSendFormatted)
}
}
default:
// Welcome back. How was your stay at the error handling hotel?
res.AmountAvailable = availableToSendFormatted + " available"
}
}
}
if minAmountXLM != "" {
cmp, err := stellarnet.CompareStellarAmounts(amountX.amountOfAsset, minAmountXLM)
switch {
case err != nil:
log("error comparing amounts", err)
case cmp == -1:
// amount is less than minAmountXLM
readyChecklist.amount = false // block sending
res.AmountErrMsg = fmt.Sprintf("You must send at least *%s XLM*", minAmountXLM)
}
}
// Note: When adding support for sending non-XLM assets, check here that the recipient accepts the asset.
}
// helper so the GUI doesn't have to call FormatCurrency separately
if arg.Currency != nil {
res.WorthAmount = amountX.amountOfAsset
}
// -------------------- note + memo --------------------
tracer.Stage("note + memo")
if len(arg.SecretNote) <= libkb.MaxStellarPaymentNoteLength {
readyChecklist.secretNote = true
} else {
res.SecretNoteErrMsg = "Note is too long."
}
if len(arg.PublicMemo) <= libkb.MaxStellarPaymentPublicNoteLength {
readyChecklist.publicMemo = true
} else {
res.PublicMemoErrMsg = "Memo is too long."
}
// -------------------- end --------------------
if readyChecklist.from && readyChecklist.to && readyChecklist.amount && readyChecklist.secretNote && readyChecklist.publicMemo {
res.ReadyToReview = true
if data != nil {
// Mark the payment as ready to review.
data.ReadyToReview = true
data.ReadyToSend = false
data.Frozen = &frozenPayment{
From: fromInfo.from,
To: arg.To,
ToUV: recipientUV,
ToIsAccountID: arg.ToIsAccountID,
Amount: amountX.amountOfAsset,
Asset: amountX.asset,
}
}
}
// Return the context's error.
// If just `nil` were returned then in the event of a cancellation
// resilient parts of this function could hide it, causing
// a bogus return value.
return res, mctx.Ctx().Err()
}
type reviewButtonState string
const reviewButtonSpinning = "spinning"
const reviewButtonEnabled = "enabled"
const reviewButtonDisabled = "disabled"
func ReviewPaymentLocal(mctx libkb.MetaContext, stellarUI stellar1.UiInterface, arg stellar1.ReviewPaymentLocalArg) (err error) {
tracer := mctx.G().CTimeTracer(mctx.Ctx(), "ReviewPaymentLocal", true)
defer tracer.Finish()
if arg.Bid.IsNil() {
return fmt.Errorf("missing payment ID")
}
mctx, data, release, err := getGlobal(mctx.G()).acquireBuildPayment(mctx, arg.Bid, arg.SessionID)
defer release()
if err != nil {
return err
}
seqno := 0
notify := func(banners []stellar1.SendBannerLocal, nextButton reviewButtonState) chan struct{} {
seqno++
seqno := seqno // Shadow seqno to freeze it for the goroutine below.
receivedCh := make(chan struct{}) // channel closed when the notification has been acked.
mctx.Debug("sending UIPaymentReview bid:%v sessionID:%v seqno:%v nextButton:%v banners:%v",
arg.Bid, arg.SessionID, seqno, nextButton, len(banners))
for _, banner := range banners {
mctx.Debug("banner: %+v", banner)
}
go func() {
err := stellarUI.PaymentReviewed(mctx.Ctx(), stellar1.PaymentReviewedArg{
SessionID: arg.SessionID,
Msg: stellar1.UIPaymentReviewed{
Bid: arg.Bid,
ReviewID: arg.ReviewID,
Seqno: seqno,
Banners: banners,
NextButton: string(nextButton),
},
})
if err != nil {
mctx.Debug("error in response to UIPaymentReview: %v", err)
}
close(receivedCh)
}()
return receivedCh
}
if !data.ReadyToReview {
// Caller goofed.
<-notify([]stellar1.SendBannerLocal{{
Level: "error",
Message: "This payment is not ready to review",
}}, reviewButtonDisabled)
return fmt.Errorf("this payment is not ready to review")
}
if data.Frozen == nil {
// Should be impossible.
return fmt.Errorf("this payment is missing values")
}
notify(nil, reviewButtonSpinning)
wantFollowingCheck := true
if data.Frozen.ToIsAccountID {
mctx.Debug("skipping identify for account ID recipient: %v", data.Frozen.To)
data.ReadyToSend = true
wantFollowingCheck = false
}
recipientAssertion := data.Frozen.To
// how would you have this before identify? from LookupRecipient?
// does that mean that identify is happening twice?
recipientUV := data.Frozen.ToUV
// check if it is a federation address
if strings.Contains(recipientAssertion, stellarAddress.Separator) {
name, domain, err := stellarAddress.Split(recipientAssertion)
// if there is an error, let this fall through and get identified
if err == nil {
if domain != "keybase.io" {
mctx.Debug("skipping identify for federation address recipient: %s", data.Frozen.To)
data.ReadyToSend = true
wantFollowingCheck = false
} else {
mctx.Debug("identifying keybase user %s in federation address recipient: %s", name, data.Frozen.To)
recipientAssertion = name
}
}
} else if !isKeybaseAssertion(mctx, recipientAssertion) { // assume assertion resolution happened already.
data.ReadyToSend = true
wantFollowingCheck = false
}
mctx.Debug("wantFollowingCheck: %v", wantFollowingCheck)
var stickyBanners []stellar1.SendBannerLocal
if wantFollowingCheck {
if isFollowing, err := isFollowingForReview(mctx, recipientAssertion); err == nil && !isFollowing {
stickyBanners = []stellar1.SendBannerLocal{{
Level: "warning",
Message: fmt.Sprintf("You are not following %v. Are you sure this is the right person?", recipientAssertion),
}}
notify(stickyBanners, reviewButtonSpinning)
}
}
if !data.ReadyToSend {
mctx.Debug("identifying recipient: %v", recipientAssertion)
identifySuccessCh := make(chan struct{}, 1)
identifyTrackFailCh := make(chan struct{}, 1)
identifyErrCh := make(chan error, 1)
// Forward notifications about successful identifies of this recipient.
go func() {
unsubscribe, globalSuccessCh := mctx.G().IdentifyDispatch.Subscribe(mctx)
defer unsubscribe()
for {
select {
case <-mctx.Ctx().Done():
return
case idRes := <-globalSuccessCh:
if recipientUV.IsNil() || !idRes.Target.Equal(recipientUV.Uid) {
continue
}
mctx.Debug("review forwarding identify success")
select {
case <-mctx.Ctx().Done():
return
case identifySuccessCh <- struct{}{}:
}
}
}
}()
// Start an identify in the background.
go identifyForReview(mctx, recipientAssertion,
identifySuccessCh, identifyTrackFailCh, identifyErrCh)
waiting:
for {
select {
case <-mctx.Ctx().Done():
return mctx.Ctx().Err()
case <-identifyErrCh:
stickyBanners = nil
notify([]stellar1.SendBannerLocal{{
Level: "error",
Message: fmt.Sprintf("Error while identifying %v. Please check your network and try again.", recipientAssertion),
}}, reviewButtonDisabled)
case <-identifyTrackFailCh:
stickyBanners = nil
notify([]stellar1.SendBannerLocal{{
Level: "error",
Message: fmt.Sprintf("Some of %v's proofs have changed since you last followed them.", recipientAssertion),
ProofsChanged: true,
}}, reviewButtonDisabled)
case <-identifySuccessCh:
data.ReadyToSend = true
break waiting
}
}
}
if err := mctx.Ctx().Err(); err != nil {
return err
}
receivedEnableCh := notify(stickyBanners, reviewButtonEnabled)
// Stay open until this call gets canceled or until frontend
// acks a notification that enables the button.
select {
case <-receivedEnableCh:
case <-mctx.Ctx().Done():
}
return mctx.Ctx().Err()
}
// identifyForReview runs identify on a user, looking only for tracking breaks.
// Sends a value to exactly one of the three channels.
func identifyForReview(mctx libkb.MetaContext, assertion string,
successCh chan<- struct{},
trackFailCh chan<- struct{},
errCh chan<- error) {
// Goroutines that are blocked on otherwise unreachable channels are not GC'd.
// So use ctx to clean up.
sendSuccess := func() {
mctx.Debug("identifyForReview(%v) -> success", assertion)
select {
case successCh <- struct{}{}:
case <-mctx.Ctx().Done():
}
}
sendTrackFail := func() {
mctx.Debug("identifyForReview(%v) -> fail", assertion)
select {
case trackFailCh <- struct{}{}:
case <-mctx.Ctx().Done():
}
}
sendErr := func(err error) {
mctx.Debug("identifyForReview(%v) -> err %v", assertion, err)
select {
case errCh <- err:
case <-mctx.Ctx().Done():
}
}
mctx.Debug("identifyForReview(%v)", assertion)
reason := fmt.Sprintf("Identify transaction recipient: %s", assertion)
eng := engine.NewResolveThenIdentify2(mctx.G(), &keybase1.Identify2Arg{
UserAssertion: assertion,
CanSuppressUI: true,
NoErrorOnTrackFailure: true, // take heed
Reason: keybase1.IdentifyReason{Reason: reason},
IdentifyBehavior: keybase1.TLFIdentifyBehavior_RESOLVE_AND_CHECK,
})
err := engine.RunEngine2(mctx, eng)
if err != nil {
sendErr(err)
return
}
idRes, err := eng.Result(mctx)
if err != nil {
sendErr(err)
return
}
if idRes == nil {
sendErr(fmt.Errorf("missing identify result"))
return
}
mctx.Debug("identifyForReview: uv: %v", idRes.Upk.Current.ToUserVersion())
if idRes.TrackBreaks != nil {
sendTrackFail()
return
}
sendSuccess()
}
// Whether the logged-in user following the recipient. If the recipient is the logged-in user, returns true.
// Unresolved assertions will false negative.
func isFollowingForReview(mctx libkb.MetaContext, assertion string) (isFollowing bool, err error) {
// The 'following' check blocks sending, and is not that important, so impose a timeout.
var cancel func()
mctx, cancel = mctx.WithTimeout(time.Second * 5)
defer cancel()
err = mctx.G().GetFullSelfer().WithSelf(mctx.Ctx(), func(u *libkb.User) error {
idTable := u.IDTable()
if idTable == nil {
return nil
}
targetUsername := libkb.NewNormalizedUsername(assertion)
selfUsername := libkb.NewNormalizedUsername(u.GetName())
if targetUsername.Eq(selfUsername) {
isFollowing = true
return nil
}
for _, track := range idTable.GetTrackList() {
if trackedUsername, err := track.GetTrackedUsername(); err == nil {
if trackedUsername.Eq(targetUsername) {
isFollowing = true
return nil
}
}
}
return nil
})
return isFollowing, err
}
func isKeybaseAssertion(mctx libkb.MetaContext, assertion string) bool {
expr, err := externals.AssertionParse(mctx, assertion)
if err != nil {
mctx.Debug("error parsing assertion: %s", err)
return false
}
switch expr.(type) {
case libkb.AssertionKeybase:
return true
case *libkb.AssertionKeybase:
return true
default:
return false
}
}
func BuildRequestLocal(mctx libkb.MetaContext, arg stellar1.BuildRequestLocalArg) (res stellar1.BuildRequestResLocal, err error) {
tracer := mctx.G().CTimeTracer(mctx.Ctx(), "BuildRequestLocal", true)
defer tracer.Finish()
mctx = mctx.WithCtx(
getGlobal(mctx.G()).buildPaymentSlot.Use(
mctx.Ctx(), arg.SessionID))
if err := mctx.Ctx().Err(); err != nil {
return res, err
}
readyChecklist := struct {
to bool
amount bool
secretNote bool
}{}
log := func(format string, args ...interface{}) {
mctx.Debug("brl: "+format, args...)
}
bpc := getGlobal(mctx.G()).getBuildPaymentCache()
if bpc == nil {
return res, fmt.Errorf("missing build payment cache")
}
// -------------------- to --------------------
tracer.Stage("to")
skipRecipient := len(arg.To) == 0
if !skipRecipient {
_, err := bpc.LookupRecipient(mctx, stellarcommon.RecipientInput(arg.To))
if err != nil {
log("error with recipient field %v: %v", arg.To, err)
res.ToErrMsg = "Recipient not found."
} else {
readyChecklist.to = true
}
}
// -------------------- amount + asset --------------------
tracer.Stage("amount + asset")
bpaArg := buildPaymentAmountArg{
Amount: arg.Amount,
Currency: arg.Currency,
Asset: arg.Asset,
}
// For requests From is always the primary account.
primaryAccountID, err := bpc.PrimaryAccount(mctx)
if err != nil {
log("PrimaryAccount -> err:%v", err)
res.Banners = append(res.Banners, stellar1.SendBannerLocal{
Level: "error",
Message: fmt.Sprintf("Could not find primary account.%v", msgMore(err)),
})
} else {
bpaArg.From = &primaryAccountID
}
amountX := buildPaymentAmountHelper(mctx, bpc, bpaArg)
res.AmountErrMsg = amountX.amountErrMsg
res.WorthDescription = amountX.worthDescription
res.WorthInfo = amountX.worthInfo
res.DisplayAmountXLM = amountX.displayAmountXLM
res.DisplayAmountFiat = amountX.displayAmountFiat
res.SendingIntentionXLM = amountX.sendingIntentionXLM
readyChecklist.amount = amountX.haveAmount
// -------------------- note --------------------
tracer.Stage("note")
if len(arg.SecretNote) <= libkb.MaxStellarPaymentNoteLength {
readyChecklist.secretNote = true
} else {
res.SecretNoteErrMsg = "Note is too long."
}
// -------------------- end --------------------
if readyChecklist.to && readyChecklist.amount && readyChecklist.secretNote {
res.ReadyToRequest = true
}
// Return the context's error.
// If just `nil` were returned then in the event of a cancellation
// resilient parts of this function could hide it, causing
// a bogus return value.
return res, mctx.Ctx().Err()
}
type buildPaymentAmountArg struct {
// See buildPaymentLocal in avdl from which these args are copied.
Bid stellar1.BuildPaymentID
Amount string
Currency *stellar1.OutsideCurrencyCode
Asset *stellar1.Asset
From *stellar1.AccountID
}
type buildPaymentAmountResult struct {
haveAmount bool // whether `amountOfAsset` and `asset` are valid
amountOfAsset string
asset stellar1.Asset
amountErrMsg string
worthDescription string
worthInfo string
worthCurrency string
// Rate may be nil if there was an error fetching it.
rate *stellar1.OutsideExchangeRate
displayAmountXLM string
displayAmountFiat string
sendingIntentionXLM bool
}
var zeroOrNoAmountRE = regexp.MustCompile(`^0*\.?0*$`)
func buildPaymentAmountHelper(mctx libkb.MetaContext, bpc BuildPaymentCache, arg buildPaymentAmountArg) (res buildPaymentAmountResult) {
log := func(format string, args ...interface{}) {
mctx.Debug("bpl: "+format, args...)
}
res.asset = stellar1.AssetNative()
switch {
case arg.Currency != nil && arg.Asset == nil:
// Amount is of outside currency.
res.sendingIntentionXLM = false
convertAmountOutside := "0"
if zeroOrNoAmountRE.MatchString(arg.Amount) {
// Zero or no amount given. Still convert for 0.
} else {
amount, err := stellarnet.ParseAmount(arg.Amount)
if err != nil || amount.Sign() < 0 {
// Invalid or negative amount.
res.amountErrMsg = "Invalid amount."
return res
}
if amount.Sign() > 0 {
// Only save the amount if it's non-zero. So that =="0" later works.
convertAmountOutside = arg.Amount
}
}
xrate, err := bpc.GetOutsideExchangeRate(mctx, *arg.Currency)
if err != nil {
log("error getting exchange rate for %v: %v", arg.Currency, err)
res.amountErrMsg = fmt.Sprintf("Could not get exchange rate for %v", arg.Currency.String())
return res
}
res.rate = &xrate
xlmAmount, err := stellarnet.ConvertOutsideToXLM(convertAmountOutside, xrate.Rate)
if err != nil {
log("error converting: %v", err)
res.amountErrMsg = "Could not convert to XLM"
return res
}
res.amountOfAsset = xlmAmount
xlmAmountFormatted, err := FormatAmountDescriptionXLM(mctx, xlmAmount)
if err != nil {
log("error formatting converted XLM amount: %v", err)
res.amountErrMsg = "Could not convert to XLM"
return res
}
res.worthDescription = xlmAmountFormatted
res.worthCurrency = string(*arg.Currency)
if convertAmountOutside != "0" {
// haveAmount gates whether the send button is enabled.
// Only enable after `worthDescription` is set.
// Don't allow the user to send if they haven't seen `worthDescription`,
// since that's what they are really sending.
res.haveAmount = true
}
res.worthInfo, err = buildPaymentWorthInfo(mctx, xrate)
if err != nil {
log("error making worth info: %v", err)
res.worthInfo = ""
}
res.displayAmountXLM = xlmAmountFormatted
res.displayAmountFiat, err = FormatCurrencyWithCodeSuffix(mctx, convertAmountOutside, *arg.Currency, stellarnet.Round)
if err != nil {
log("error converting for displayAmountFiat: %q / %q : %s", convertAmountOutside, arg.Currency, err)
res.displayAmountFiat = ""
}
return res
case arg.Currency == nil:
res.sendingIntentionXLM = true
if arg.Asset != nil {
res.asset = *arg.Asset
}
// Amount is of asset.
useAmount := "0"
if zeroOrNoAmountRE.MatchString(arg.Amount) {
// Zero or no amount given.
} else {
amountInt64, err := stellarnet.ParseStellarAmount(arg.Amount)
if err != nil || amountInt64 <= 0 {
res.amountErrMsg = "Invalid amount."
return res
}
res.amountOfAsset = arg.Amount
res.haveAmount = true
useAmount = arg.Amount
}
if !res.asset.IsNativeXLM() {
res.sendingIntentionXLM = false
// If sending non-XLM asset, don't try to show a worth.
return res
}
// Attempt to show the converted amount in outside currency.
// Unlike when sending based on outside currency, conversion is not critical.
if arg.From == nil {
log("missing from address so can't convert XLM amount")
return res
}
currency, err := bpc.GetOutsideCurrencyPreference(mctx, *arg.From, arg.Bid)
if err != nil {
log("error getting preferred currency for %v: %v", *arg.From, err)
return res
}
xrate, err := bpc.GetOutsideExchangeRate(mctx, currency)
if err != nil {
log("error getting exchange rate for %v: %v", currency, err)
return res
}
res.rate = &xrate
outsideAmount, err := stellarnet.ConvertXLMToOutside(useAmount, xrate.Rate)
if err != nil {
log("error converting: %v", err)
return res
}
outsideAmountFormatted, err := FormatCurrencyWithCodeSuffix(mctx, outsideAmount, xrate.Currency, stellarnet.Round)
if err != nil {
log("error formatting converted outside amount: %v", err)
return res
}
res.worthDescription = outsideAmountFormatted
res.worthCurrency = string(currency)
res.worthInfo, err = buildPaymentWorthInfo(mctx, xrate)
if err != nil {
log("error making worth info: %v", err)
res.worthInfo = ""
}