-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
transform.go
675 lines (596 loc) · 20.9 KB
/
transform.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
package stellar
import (
"errors"
"fmt"
"strings"
"time"
"github.com/keybase/client/go/chat/utils"
"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/stellar/relays"
"github.com/keybase/client/go/stellar/remote"
"github.com/keybase/stellarnet"
)
// TransformPaymentSummaryGeneric converts a stellar1.PaymentSummary (p) into a
// stellar1.PaymentLocal, without any modifications based on who is viewing the transaction.
func TransformPaymentSummaryGeneric(mctx libkb.MetaContext, p stellar1.PaymentSummary, oc OwnAccountLookupCache) (*stellar1.PaymentLocal, error) {
var emptyAccountID stellar1.AccountID
return transformPaymentSummary(mctx, p, oc, emptyAccountID)
}
// TransformPaymentSummaryAccount converts a stellar1.PaymentSummary (p) into a
// stellar1.PaymentLocal, from the perspective of an owner of accountID.
func TransformPaymentSummaryAccount(mctx libkb.MetaContext, p stellar1.PaymentSummary, oc OwnAccountLookupCache, accountID stellar1.AccountID) (*stellar1.PaymentLocal, error) {
return transformPaymentSummary(mctx, p, oc, accountID)
}
// transformPaymentSummary converts a stellar1.PaymentSummary (p) into a stellar1.PaymentLocal.
// accountID can be empty ("") and exchRate can be nil, if a generic response that isn't tied
// to an account is necessary.
func transformPaymentSummary(mctx libkb.MetaContext, p stellar1.PaymentSummary, oc OwnAccountLookupCache, accountID stellar1.AccountID) (*stellar1.PaymentLocal, error) {
typ, err := p.Typ()
if err != nil {
return nil, err
}
switch typ {
case stellar1.PaymentSummaryType_STELLAR:
return transformPaymentStellar(mctx, accountID, p.Stellar(), oc)
case stellar1.PaymentSummaryType_DIRECT:
return transformPaymentDirect(mctx, accountID, p.Direct(), oc)
case stellar1.PaymentSummaryType_RELAY:
return transformPaymentRelay(mctx, accountID, p.Relay(), oc)
default:
return nil, fmt.Errorf("unrecognized payment type: %T", typ)
}
}
func TransformRequestDetails(mctx libkb.MetaContext, details stellar1.RequestDetails) (*stellar1.RequestDetailsLocal, error) {
fromAssertion, err := lookupUsername(mctx, details.FromUser.Uid)
if err != nil {
return nil, err
}
loc := stellar1.RequestDetailsLocal{
Id: details.Id,
FromAssertion: fromAssertion,
FromCurrentUser: mctx.G().GetMyUID().Equal(details.FromUser.Uid),
ToAssertion: details.ToAssertion,
Amount: details.Amount,
Asset: details.Asset,
Currency: details.Currency,
Status: details.Status,
}
if details.ToUser != nil {
loc.ToUserType = stellar1.ParticipantType_KEYBASE
} else {
loc.ToUserType = stellar1.ParticipantType_SBS
}
switch {
case details.Currency != nil:
amountDesc, err := FormatCurrency(mctx, details.Amount, *details.Currency, stellarnet.Round)
if err != nil {
amountDesc = details.Amount
mctx.Debug("error formatting external currency: %s", err)
}
loc.AmountDescription = fmt.Sprintf("%s %s", amountDesc, *details.Currency)
case details.Asset != nil:
var code string
if details.Asset.IsNativeXLM() {
code = "XLM"
if loc.FromCurrentUser {
loc.WorthAtRequestTime, _, _ = formatWorth(mctx, &details.FromDisplayAmount, &details.FromDisplayCurrency)
} else {
loc.WorthAtRequestTime, _, _ = formatWorth(mctx, &details.ToDisplayAmount, &details.ToDisplayCurrency)
}
} else {
code = details.Asset.Code
}
amountDesc, err := FormatAmountWithSuffix(mctx, details.Amount, false /* precisionTwo */, true /* simplify */, code)
if err != nil {
amountDesc = fmt.Sprintf("%s %s", details.Amount, code)
mctx.Debug("error formatting amount for asset: %s", err)
}
loc.AmountDescription = amountDesc
default:
return nil, errors.New("malformed request - currency/asset not defined")
}
return &loc, nil
}
// transformPaymentStellar converts a stellar1.PaymentSummaryStellar into a stellar1.PaymentLocal.
func transformPaymentStellar(mctx libkb.MetaContext, acctID stellar1.AccountID, p stellar1.PaymentSummaryStellar, oc OwnAccountLookupCache) (res *stellar1.PaymentLocal, err error) {
loc := stellar1.NewPaymentLocal(p.TxID, p.Ctime)
if p.IsAdvanced {
if len(p.Amount) > 0 {
// It is expected that there is no amount.
// But might as well future proof it so that if an amount shows up it gets formatted.
loc.AmountDescription, err = FormatAmountDescriptionAsset(mctx, p.Amount, p.Asset)
if err != nil {
loc.AmountDescription = ""
}
}
asset := p.Asset // p.Asset is also expected to be missing.
if p.Trustline != nil && asset.IsEmpty() {
asset = p.Trustline.Asset
}
if !asset.IsEmpty() && !asset.IsNativeXLM() {
loc.IssuerDescription = FormatAssetIssuerString(asset)
issuerAcc := stellar1.AccountID(asset.Issuer)
loc.IssuerAccountID = &issuerAcc
}
} else {
loc, err = newPaymentCommonLocal(mctx, p.TxID, p.Ctime, p.Amount, p.Asset)
if err != nil {
return nil, err
}
}
isSender := p.From.Eq(acctID)
isRecipient := p.To.Eq(acctID)
switch {
case isSender && isRecipient:
loc.Delta = stellar1.BalanceDelta_NONE
case isSender:
loc.Delta = stellar1.BalanceDelta_DECREASE
case isRecipient:
loc.Delta = stellar1.BalanceDelta_INCREASE
}
loc.AssetCode = p.Asset.Code
loc.FromAccountID = p.From
loc.FromType = stellar1.ParticipantType_STELLAR
loc.ToAccountID = &p.To
loc.ToType = stellar1.ParticipantType_STELLAR
fillOwnAccounts(mctx, loc, oc)
loc.StatusSimplified = stellar1.PaymentStatus_COMPLETED
loc.StatusDescription = strings.ToLower(loc.StatusSimplified.String())
loc.IsInflation = p.IsInflation
loc.InflationSource = p.InflationSource
loc.SourceAsset = p.SourceAsset
loc.SourceAmountMax = p.SourceAmountMax
loc.SourceAmountActual = p.SourceAmountActual
sourceConvRate, err := stellarnet.GetStellarExchangeRate(loc.SourceAmountActual, p.Amount)
if err == nil {
loc.SourceConvRate = sourceConvRate
} else {
loc.SourceConvRate = ""
}
loc.IsAdvanced = p.IsAdvanced
loc.SummaryAdvanced = p.SummaryAdvanced
loc.Operations = p.Operations
loc.Unread = p.Unread
loc.Trustline = p.Trustline
return loc, nil
}
func formatWorthAtSendTime(mctx libkb.MetaContext, p stellar1.PaymentSummaryDirect, isSender bool) (worthAtSendTime, worthCurrencyAtSendTime string, err error) {
if p.DisplayCurrency == nil || len(*p.DisplayCurrency) == 0 {
if isSender {
return formatWorth(mctx, &p.FromDisplayAmount, &p.FromDisplayCurrency)
}
return formatWorth(mctx, &p.ToDisplayAmount, &p.ToDisplayCurrency)
}
// payment has a display currency, don't need this field
return "", "", nil
}
// transformPaymentDirect converts a stellar1.PaymentSummaryDirect into a stellar1.PaymentLocal.
func transformPaymentDirect(mctx libkb.MetaContext, acctID stellar1.AccountID, p stellar1.PaymentSummaryDirect, oc OwnAccountLookupCache) (*stellar1.PaymentLocal, error) {
loc, err := newPaymentCommonLocal(mctx, p.TxID, p.Ctime, p.Amount, p.Asset)
if err != nil {
return nil, err
}
isSender := p.FromStellar.Eq(acctID)
isRecipient := p.ToStellar.Eq(acctID)
switch {
case isSender && isRecipient:
loc.Delta = stellar1.BalanceDelta_NONE
case isSender:
loc.Delta = stellar1.BalanceDelta_DECREASE
case isRecipient:
loc.Delta = stellar1.BalanceDelta_INCREASE
}
loc.Worth, _, err = formatWorth(mctx, p.DisplayAmount, p.DisplayCurrency)
if err != nil {
return nil, err
}
loc.FromAccountID = p.FromStellar
loc.FromType = stellar1.ParticipantType_STELLAR
if username, err := lookupUsername(mctx, p.From.Uid); err == nil {
loc.FromUsername = username
loc.FromType = stellar1.ParticipantType_KEYBASE
}
loc.ToAccountID = &p.ToStellar
loc.ToType = stellar1.ParticipantType_STELLAR
if p.To != nil {
if username, err := lookupUsername(mctx, p.To.Uid); err == nil {
loc.ToUsername = username
loc.ToType = stellar1.ParticipantType_KEYBASE
}
}
loc.AssetCode = p.Asset.Code
fillOwnAccounts(mctx, loc, oc)
switch {
case loc.FromAccountName != "":
// we are sender
loc.WorthAtSendTime, _, err = formatWorthAtSendTime(mctx, p, true)
case loc.ToAccountName != "":
// we are recipient
loc.WorthAtSendTime, _, err = formatWorthAtSendTime(mctx, p, false)
}
if err != nil {
return nil, err
}
loc.StatusSimplified = p.TxStatus.ToPaymentStatus()
loc.StatusDescription = strings.ToLower(loc.StatusSimplified.String())
loc.StatusDetail = p.TxErrMsg
loc.Note, loc.NoteErr = decryptNote(mctx, p.TxID, p.NoteB64)
loc.SourceAmountMax = p.SourceAmountMax
loc.SourceAmountActual = p.SourceAmountActual
loc.SourceAsset = p.SourceAsset
sourceConvRate, err := stellarnet.GetStellarExchangeRate(loc.SourceAmountActual, p.Amount)
if err == nil {
loc.SourceConvRate = sourceConvRate
} else {
loc.SourceConvRate = ""
}
loc.Unread = p.Unread
loc.FromAirdrop = p.FromAirdrop
return loc, nil
}
// transformPaymentRelay converts a stellar1.PaymentSummaryRelay into a stellar1.PaymentLocal.
func transformPaymentRelay(mctx libkb.MetaContext, acctID stellar1.AccountID, p stellar1.PaymentSummaryRelay, oc OwnAccountLookupCache) (*stellar1.PaymentLocal, error) {
loc, err := newPaymentCommonLocal(mctx, p.TxID, p.Ctime, p.Amount, stellar1.AssetNative())
if err != nil {
return nil, err
}
// isSender compares uid but not eldest-seqno because relays can survive resets.
isSender := p.From.Uid.Equal(mctx.G().GetMyUID())
loc.Delta = stellar1.BalanceDelta_INCREASE
if isSender {
loc.Delta = stellar1.BalanceDelta_DECREASE
}
loc.Worth, _, err = formatWorth(mctx, p.DisplayAmount, p.DisplayCurrency)
if err != nil {
return nil, err
}
loc.AssetCode = "XLM" // We can hardcode relay payments, since the asset will always be XLM
loc.FromAccountID = p.FromStellar
loc.FromUsername, err = lookupUsername(mctx, p.From.Uid)
if err != nil {
mctx.Debug("sender lookup failed: %s", err)
return nil, errors.New("sender lookup failed")
}
loc.FromType = stellar1.ParticipantType_KEYBASE
loc.ToAssertion = p.ToAssertion
loc.ToType = stellar1.ParticipantType_SBS
toName := loc.ToAssertion
if p.To != nil {
username, err := lookupUsername(mctx, p.To.Uid)
if err != nil {
mctx.Debug("recipient lookup failed: %s", err)
return nil, errors.New("recipient lookup failed")
}
loc.ToUsername = username
loc.ToType = stellar1.ParticipantType_KEYBASE
toName = username
}
if p.TxStatus != stellar1.TransactionStatus_SUCCESS {
// The funding tx is not complete.
loc.StatusSimplified = p.TxStatus.ToPaymentStatus()
loc.StatusDetail = p.TxErrMsg
} else {
loc.StatusSimplified = stellar1.PaymentStatus_CLAIMABLE
if isSender {
loc.StatusDetail = fmt.Sprintf("%v can claim this when they set up their wallet.", toName)
loc.ShowCancel = true
}
}
if p.Claim != nil {
loc.StatusSimplified = p.Claim.ToPaymentStatus()
loc.ToAccountID = &p.Claim.ToStellar
loc.ToType = stellar1.ParticipantType_STELLAR
loc.ToUsername = ""
loc.ToAccountName = ""
claimStatus := p.Claim.ToPaymentStatus()
if claimStatus != stellar1.PaymentStatus_ERROR {
// if there's a claim and it's not currently erroring, then hide the
// `cancel` button
loc.ShowCancel = false
}
if claimStatus == stellar1.PaymentStatus_CANCELED {
// canceled payment. blank out toAssertion and stow in originalToAssertion
// set delta to what it would have been had the payment completed
loc.ToAssertion = ""
loc.OriginalToAssertion = p.ToAssertion
loc.Delta = stellar1.BalanceDelta_INCREASE
if acctID == p.FromStellar {
loc.Delta = stellar1.BalanceDelta_DECREASE
}
}
if username, err := lookupUsername(mctx, p.Claim.To.Uid); err == nil {
loc.ToUsername = username
loc.ToType = stellar1.ParticipantType_KEYBASE
}
if p.Claim.TxStatus == stellar1.TransactionStatus_SUCCESS {
// If the claim succeeded, the relay payment is done.
loc.StatusDetail = ""
} else {
claimantUsername, err := lookupUsername(mctx, p.Claim.To.Uid)
if err != nil {
return nil, err
}
if p.Claim.TxErrMsg != "" {
loc.StatusDetail = p.Claim.TxErrMsg
} else {
words := "is in the works"
switch p.Claim.TxStatus {
case stellar1.TransactionStatus_PENDING:
words = "is pending"
case stellar1.TransactionStatus_SUCCESS:
words = "has succeeded"
case stellar1.TransactionStatus_ERROR_TRANSIENT, stellar1.TransactionStatus_ERROR_PERMANENT:
words = "has failed"
}
loc.StatusDetail = fmt.Sprintf("Funded. %v's claim %v.", claimantUsername, words)
}
}
}
loc.StatusDescription = strings.ToLower(loc.StatusSimplified.String())
fillOwnAccounts(mctx, loc, oc)
relaySecrets, err := relays.DecryptB64(mctx, p.TeamID, p.BoxB64)
if err == nil {
loc.Note = relaySecrets.Note
} else {
loc.NoteErr = fmt.Sprintf("error decrypting note: %s", err)
}
loc.FromAirdrop = p.FromAirdrop
return loc, nil
}
func formatWorth(mctx libkb.MetaContext, amount, currency *string) (worth, worthCurrency string, err error) {
if amount == nil || currency == nil {
return "", "", nil
}
if len(*amount) == 0 || len(*currency) == 0 {
return "", "", nil
}
worth, err = FormatCurrencyWithCodeSuffix(mctx, *amount, stellar1.OutsideCurrencyCode(*currency), stellarnet.Round)
if err != nil {
return "", "", err
}
return worth, *currency, nil
}
func lookupUsername(mctx libkb.MetaContext, uid keybase1.UID) (string, error) {
uname, err := mctx.G().GetUPAKLoader().LookupUsername(mctx.Ctx(), uid)
if err != nil {
return "", err
}
return uname.String(), nil
}
func fillOwnAccounts(mctx libkb.MetaContext, loc *stellar1.PaymentLocal, oc OwnAccountLookupCache) {
lookupOwnAccountQuick := func(accountID *stellar1.AccountID) (accountName string) {
if accountID == nil {
return ""
}
own, name, err := oc.OwnAccount(mctx.Ctx(), *accountID)
if err != nil || !own {
return ""
}
if name != "" {
return name
}
return accountID.String()
}
loc.FromAccountName = lookupOwnAccountQuick(&loc.FromAccountID)
loc.ToAccountName = lookupOwnAccountQuick(loc.ToAccountID)
if loc.FromAccountName != "" && loc.ToAccountName != "" {
loc.FromType = stellar1.ParticipantType_OWNACCOUNT
loc.ToType = stellar1.ParticipantType_OWNACCOUNT
}
}
func decryptNote(mctx libkb.MetaContext, txid stellar1.TransactionID, note string) (plaintext, errOutput string) {
if len(note) == 0 {
return "", ""
}
decrypted, err := NoteDecryptB64(mctx, note)
if err != nil {
return "", fmt.Sprintf("failed to decrypt payment note: %s", err)
}
if decrypted.StellarID != txid {
return "", "discarded note for wrong transaction ID"
}
return utils.EscapeForDecorate(mctx.Ctx(), decrypted.Note), ""
}
func newPaymentCommonLocal(mctx libkb.MetaContext, txID stellar1.TransactionID, ctime stellar1.TimeMs, amount string, asset stellar1.Asset) (*stellar1.PaymentLocal, error) {
loc := stellar1.NewPaymentLocal(txID, ctime)
formatted, err := FormatAmountDescriptionAsset(mctx, amount, asset)
if err != nil {
return nil, err
}
loc.AmountDescription = formatted
if !asset.IsNativeXLM() {
loc.IssuerDescription = FormatAssetIssuerString(asset)
issuerAcc := stellar1.AccountID(asset.Issuer)
loc.IssuerAccountID = &issuerAcc
}
return loc, nil
}
func RemoteRecentPaymentsToPage(mctx libkb.MetaContext, remoter remote.Remoter, accountID stellar1.AccountID, remotePage stellar1.PaymentsPage) (page stellar1.PaymentsPageLocal, err error) {
oc := NewOwnAccountLookupCache(mctx)
page.Payments = make([]stellar1.PaymentOrErrorLocal, len(remotePage.Payments))
for i, p := range remotePage.Payments {
page.Payments[i].Payment, err = TransformPaymentSummaryAccount(mctx, p, oc, accountID)
if err != nil {
mctx.Debug("RemoteRecentPaymentsToPage error transforming payment %v: %v", i, err)
s := err.Error()
page.Payments[i].Err = &s
page.Payments[i].Payment = nil // just to make sure
}
}
page.Cursor = remotePage.Cursor
if remotePage.OldestUnread != nil {
oldestUnread := stellar1.NewPaymentID(*remotePage.OldestUnread)
page.OldestUnread = &oldestUnread
}
return page, nil
}
func RemotePendingToLocal(mctx libkb.MetaContext, remoter remote.Remoter, accountID stellar1.AccountID, pending []stellar1.PaymentSummary) (payments []stellar1.PaymentOrErrorLocal, err error) {
oc := NewOwnAccountLookupCache(mctx)
payments = make([]stellar1.PaymentOrErrorLocal, len(pending))
for i, p := range pending {
payment, err := TransformPaymentSummaryAccount(mctx, p, oc, accountID)
if err != nil {
s := err.Error()
payments[i].Err = &s
payments[i].Payment = nil // just to make sure
} else {
payments[i].Payment = payment
payments[i].Err = nil
}
}
return payments, nil
}
func AccountDetailsToWalletAccountLocal(mctx libkb.MetaContext, accountID stellar1.AccountID, details stellar1.AccountDetails,
isPrimary bool, accountName string, accountMode stellar1.AccountMode) (stellar1.WalletAccountLocal, error) {
var empty stellar1.WalletAccountLocal
balance, err := balanceList(details.Balances).balanceDescription(mctx)
if err != nil {
return empty, err
}
activeDeviceType, err := mctx.G().ActiveDevice.DeviceType(mctx)
if err != nil {
return empty, err
}
isMobile := activeDeviceType == keybase1.DeviceTypeV2_MOBILE
// AccountModeEditable - can user change "account mode" to mobile only or
// back? This setting can only be changed from a mobile device that's over
// 7 days old (since provisioning).
editable := false
if isMobile {
ctime, err := mctx.G().ActiveDevice.Ctime(mctx)
if err != nil {
return empty, err
}
deviceProvisionedAt := time.Unix(int64(ctime)/1000, 0)
deviceAge := mctx.G().Clock().Since(deviceProvisionedAt)
if deviceAge > 7*24*time.Hour {
editable = true
}
}
// AccountDeviceReadOnly - if account is mobileOnly and current device is
// either desktop, or mobile but not only enough (7 days since
// provisioning).
readOnly := false
if accountMode == stellar1.AccountMode_MOBILE {
if isMobile {
// Mobile devices eligible to edit are also eligible to do
// transactions.
readOnly = !editable
} else {
// All desktop devices are read only.
readOnly = true
}
}
// Is there enough to make any transaction?
var availableInt int64
if details.Available != "" {
availableInt, err = stellarnet.ParseStellarAmount(details.Available)
if err != nil {
return empty, err
}
}
baseFee := getGlobal(mctx.G()).BaseFee(mctx)
// TODO: this is something that stellard can just tell us.
isFunded, err := hasPositiveLumenBalance(details.Balances)
if err != nil {
return empty, err
}
const trustlineReserveStroops int64 = stellarnet.StroopsPerLumen / 2
acct := stellar1.WalletAccountLocal{
AccountID: accountID,
IsDefault: isPrimary,
Name: accountName,
BalanceDescription: balance,
Seqno: details.Seqno,
AccountMode: accountMode,
AccountModeEditable: editable,
DeviceReadOnly: readOnly,
IsFunded: isFunded,
CanSubmitTx: availableInt > int64(baseFee),
CanAddTrustline: availableInt > int64(baseFee)+trustlineReserveStroops,
}
conf, err := mctx.G().GetStellar().GetServerDefinitions(mctx.Ctx())
if err == nil {
for _, currency := range []string{details.DisplayCurrency, DefaultCurrencySetting} {
currency, ok := conf.GetCurrencyLocal(stellar1.OutsideCurrencyCode(currency))
if ok {
acct.CurrencyLocal = currency
break
}
}
}
if acct.CurrencyLocal.Code == "" {
mctx.Debug("warning: AccountDetails for %v has empty currency code", details.AccountID)
}
return acct, nil
}
type balanceList []stellar1.Balance
// Example: "56.0227002 XLM + more"
func (a balanceList) balanceDescription(mctx libkb.MetaContext) (res string, err error) {
var more bool
for _, b := range a {
if b.Asset.IsNativeXLM() {
res, err = FormatAmountDescriptionXLM(mctx, b.Amount)
if err != nil {
return "", err
}
} else {
more = true
}
}
if res == "" {
res = "0 XLM"
}
if more {
res += " + more"
}
return res, nil
}
// TransformToAirdropStatus takes the result from api server status_check
// and transforms it into stellar1.AirdropStatus.
func TransformToAirdropStatus(status remote.AirdropStatusAPI) stellar1.AirdropStatus {
var out stellar1.AirdropStatus
switch {
case status.AlreadyRegistered:
out.State = stellar1.AirdropAccepted
case status.Qualifications.QualifiesOverall:
out.State = stellar1.AirdropQualified
default:
out.State = stellar1.AirdropUnqualified
}
dq := stellar1.AirdropQualification{
Title: status.AirdropConfig.MinActiveDevicesTitle,
Valid: status.Qualifications.HasEnoughDevices,
}
out.Rows = append(out.Rows, dq)
aq := stellar1.AirdropQualification{
Title: status.AirdropConfig.AccountCreationTitle,
}
var used []string
for k, q := range status.Qualifications.ServiceChecks {
if q.Qualifies {
aq.Valid = true
break
}
if q.Username == "" {
continue
}
if !q.IsOldEnough {
continue
}
if q.IsUsedAlready {
used = append(used, fmt.Sprintf("%s@%s", q.Username, k))
}
}
if !aq.Valid {
aq.Subtitle = status.AirdropConfig.AccountCreationSubtitle
if len(used) > 0 {
usedDisplay := strings.Join(used, ", ")
aq.Subtitle += " " + fmt.Sprintf(status.AirdropConfig.AccountUsed, usedDisplay)
}
}
out.Rows = append(out.Rows, aq)
return out
}