github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/stellar/util.go (about) 1 package stellar 2 3 import ( 4 "context" 5 "fmt" 6 "runtime/debug" 7 8 "github.com/keybase/client/go/libkb" 9 "github.com/keybase/client/go/protocol/keybase1" 10 "github.com/keybase/client/go/protocol/stellar1" 11 "github.com/keybase/stellarnet" 12 ) 13 14 func loadUvUpk(mctx libkb.MetaContext, uv keybase1.UserVersion) (res *keybase1.UserPlusKeysV2, err error) { 15 loadArg := libkb.NewLoadUserArgWithMetaContext(mctx).WithUID(uv.Uid) 16 upkv2, _, err := mctx.G().GetUPAKLoader().LoadV2(loadArg) 17 if err != nil { 18 return nil, err 19 } 20 if upkv2 == nil { 21 return nil, fmt.Errorf("could not load user: %v (nil)", uv.String()) 22 } 23 if upkv2.Current.EldestSeqno == uv.EldestSeqno { 24 return &upkv2.Current, nil 25 } 26 for _, incarnation := range upkv2.PastIncarnations { 27 if incarnation.EldestSeqno == uv.EldestSeqno { 28 return &incarnation, nil 29 } 30 } 31 return nil, fmt.Errorf("could not load user: %v (v)", uv.String()) 32 } 33 34 func loadOwnLatestPuk(mctx libkb.MetaContext) (gen keybase1.PerUserKeyGeneration, seed libkb.PerUserKeySeed, err error) { 35 pukring, err := mctx.G().GetPerUserKeyring(mctx.Ctx()) 36 if err != nil { 37 return 0, seed, err 38 } 39 err = pukring.Sync(mctx) 40 if err != nil { 41 return 0, seed, err 42 } 43 gen = pukring.CurrentGeneration() 44 seed, err = pukring.GetSeedByGeneration(mctx, gen) 45 return gen, seed, err 46 } 47 48 // Short-lived cache for looking up whether the logged-in user owns accounts. 49 type OwnAccountLookupCache interface { 50 OwnAccount(ctx context.Context, accountID stellar1.AccountID) (own bool, accountName string, err error) 51 } 52 53 type ownAccountLookupCacheFromGlobal struct { 54 libkb.Contextified 55 } 56 57 // NewOwnAccountLookupCache was obsoleted and exists only as an interface bridge. 58 // Feel free to continue this refactor and remove it. 59 func NewOwnAccountLookupCache(mctx libkb.MetaContext) OwnAccountLookupCache { 60 return &ownAccountLookupCacheFromGlobal{ 61 Contextified: libkb.NewContextified(mctx.G()), 62 } 63 } 64 65 func (o *ownAccountLookupCacheFromGlobal) OwnAccount(ctx context.Context, accountID stellar1.AccountID) (own bool, accountName string, err error) { 66 mctx := libkb.NewMetaContext(ctx, o.G()) 67 own, _, accountName, err = OwnAccountPlusNameCached(mctx, accountID) 68 return own, accountName, err 69 } 70 71 func LookupSenderSeed(mctx libkb.MetaContext) (stellar1.AccountID, stellarnet.SeedStr, error) { 72 senderEntry, senderAccountBundle, err := LookupSenderPrimary(mctx) 73 if err != nil { 74 return "", "", err 75 } 76 senderSeed, err := stellarnet.NewSeedStr(senderAccountBundle.Signers[0].SecureNoLogString()) 77 if err != nil { 78 return "", "", err 79 } 80 81 return senderEntry.AccountID, senderSeed, nil 82 } 83 84 func isAmountLessThanMin(amount, min string) bool { 85 cmp, err := stellarnet.CompareStellarAmounts(amount, min) 86 if err == nil && cmp == -1 { 87 return true 88 } 89 return false 90 } 91 92 func EmptyAmountStack(mctx libkb.MetaContext) { 93 mctx.Debug("unexpected empty amount\n%v", string(debug.Stack())) 94 } 95 96 // cancelOnMobileBackground returns a copy of mctx that is canceled 97 // when the app transitions out of foreground, in addition to its existing cancelation. 98 // 99 // Canceling this context releases resources associated with it, so code should 100 // call cancel as soon as the operations running in this Context complete. 101 func cancelOnMobileBackground(mctx libkb.MetaContext) (libkb.MetaContext, context.CancelFunc) { 102 mctx, cancel := mctx.WithContextCancel() 103 go func() { 104 for { 105 foreground := keybase1.MobileAppState_FOREGROUND 106 select { 107 case state := <-mctx.G().MobileAppState.NextUpdate(&foreground): 108 if state != foreground { 109 cancel() 110 return 111 } 112 case <-mctx.Ctx().Done(): 113 return 114 } 115 } 116 }() 117 return mctx, cancel 118 }