Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
[1.61] Fix #8252: Remove KeyringInfo Refactor & Update BraveCore to v…
Browse files Browse the repository at this point in the history
…1.61.39 (#8313)

* Update for KeyringInfo removal refactoring

* Update BraveCore to v1.61.39
  • Loading branch information
StephenHeaps authored and Brandon-T committed Oct 27, 2023
1 parent b170673 commit a613931
Show file tree
Hide file tree
Showing 30 changed files with 124 additions and 399 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ extension Tab: BraveWalletProviderDelegate {
let origin = getOrigin()

// check if we receive account creation request without a wallet setup
let keyring = await keyringService.keyringInfo(BraveWallet.KeyringId.default)
if !keyring.isKeyringCreated {
let isWalletCreated = await keyringService.isWalletCreated()
if !isWalletCreated {
// Wallet is not setup. User must onboard / setup wallet first.
self.tabDelegate?.showWalletNotification(self, origin: origin)
return
Expand Down Expand Up @@ -579,13 +579,13 @@ extension Tab: BraveWalletSolanaEventsListener {
}

extension Tab: BraveWalletKeyringServiceObserver {
func keyringCreated(_ keyringId: BraveWallet.KeyringId) {
func walletCreated() {
}

func walletRestored() {
}

func keyringReset() {
func walletReset() {
reload()
tabDelegate?.updateURLBarWalletButton()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ struct AssetDetailView: View {
}
)
)
.onChange(of: keyringStore.defaultKeyring) { newValue in
if newValue.isLocked, isShowingAuroraBridgeAlert {
.onChange(of: keyringStore.isWalletLocked) { isLocked in
if isLocked, isShowingAuroraBridgeAlert {
isShowingAuroraBridgeAlert = false
}
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/BraveWallet/Crypto/CryptoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ public struct CryptoView: View {
}

private var visibleScreen: VisibleScreen {
let keyring = keyringStore.defaultKeyring
if !keyring.isKeyringCreated || keyringStore.isOnboardingVisible {
if !keyringStore.isWalletCreated || keyringStore.isOnboardingVisible {
return .onboarding
}
if keyring.isLocked || keyringStore.isRestoreFromUnlockBiometricsPromptVisible {
if keyringStore.isWalletLocked || keyringStore.isRestoreFromUnlockBiometricsPromptVisible {
return .unlock
}
return .crypto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct PortfolioHeaderView: View {
@ObservedObject private var isShowingBalances = Preferences.Wallet.isShowingBalances

private var isShowingBackupBanner: Bool {
!keyringStore.defaultKeyring.isBackedUp && !dismissedBackupBannerThisSession
!keyringStore.isWalletBackedUp && !dismissedBackupBannerThisSession
}

private var emptyBalanceData: [BalanceTimePrice] {
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/AccountActivityStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ class AccountActivityStore: ObservableObject, WalletObserverStore {
}

extension AccountActivityStore: BraveWalletKeyringServiceObserver {
func keyringCreated(_ keyringId: BraveWallet.KeyringId) {
func walletCreated() {
}

func walletRestored() {
}

func keyringReset() {
func walletReset() {
}

func locked() {
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/AssetDetailStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,14 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
}

extension AssetDetailStore: BraveWalletKeyringServiceObserver {
func keyringReset() {
func walletReset() {
}

func accountsChanged() {
update()
}

func keyringCreated(_ keyringId: BraveWallet.KeyringId) {
func walletCreated() {
}

func walletRestored() {
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/CryptoStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ public class CryptoStore: ObservableObject, WalletObserverStore {
guard !isObserving else { return }
self.keyringServiceObserver = KeyringServiceObserver(
keyringService: keyringService,
_keyringReset: { [weak self] in
_walletReset: { [weak self] in
WalletProviderPermissionRequestsManager.shared.cancelAllPendingRequests(for: [.eth, .sol])
WalletProviderAccountCreationRequestManager.shared.cancelAllPendingRequests(coins: [.eth, .sol])
self?.rejectAllPendingWebpageRequests()
},
_keyringCreated: { _ in
_walletCreated: {
// 1. We don't need to rely on this observer method to migrate user visible assets
// when user creates a new wallet, since in this case `CryptoStore` has not yet been initialized
// 2. We don't need to rely on this observer method to migrate user visible assets
Expand Down
117 changes: 52 additions & 65 deletions Sources/BraveWallet/Crypto/Stores/KeyringStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,8 @@ enum PasswordStatus: Equatable {
///
/// This wraps a KeyringService that you would obtain through BraveCore and makes it observable
public class KeyringStore: ObservableObject, WalletObserverStore {
/// The defualt keyring information. By default this is an empty keyring which has no accounts.
@Published private(set) var defaultKeyring: BraveWallet.KeyringInfo = .init(
id: .default,
isKeyringCreated: false,
isLocked: true,
isBackedUp: false
)
/// A boolean indciates front-end has or has not loaded Keyring from the core
@Published var isDefaultKeyringLoaded = false
/// A boolean indciates front-end has or has not loaded accounts from the core
@Published var isLoaded = false
/// Whether or not the user should be viewing the onboarding flow to setup a keyring
@Published private(set) var isOnboardingVisible: Bool = false
/// Whether or not the last time the wallet was locked was due to the user manually locking it
Expand All @@ -147,11 +140,12 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
setSelectedAccount(to: selectedAccount)
}
}
/// All available `KeyringInfo` for all supported coin type
@Published var allKeyrings: [BraveWallet.KeyringInfo] = []
/// Indicates if default keyring has been created. This value used for display wallet related settings if default
/// keyring has been created
@Published var isDefaultKeyringCreated: Bool = false
/// Indicates if the wallet has been created. This value used for display wallet related settings if created.
@Published var isWalletCreated: Bool = false
/// Indicates if the wallet has been locked.
@Published var isWalletLocked: Bool = true
/// Indicates if the wallet has been backed up.
@Published var isWalletBackedUp: Bool = false
/// All `AccountInfo` for all available keyrings
@Published var allAccounts: [BraveWallet.AccountInfo] = []

Expand Down Expand Up @@ -192,13 +186,13 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
self.walletService = walletService
self.rpcService = rpcService
self.keychain = keychain
self.setupObservers()

updateKeyringInfo()

self.keyringService.keyringInfo(BraveWallet.KeyringId.default) { [self] keyringInfo in
isOnboardingVisible = !keyringInfo.isKeyringCreated

setupObservers()
updateInfo()

Task { @MainActor in
let isWalletCreated = await keyringService.isWalletCreated()
self.isOnboardingVisible = !isWalletCreated
if isKeychainPasswordStored && isOnboardingVisible {
// If a user deletes the app and they had a stored user password in the past that keychain item
// stays persisted. When we grab the keyring for the first time we should check to see if they have
Expand All @@ -210,35 +204,27 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
cancellable = NotificationCenter.default
.publisher(for: UIApplication.didBecomeActiveNotification, object: nil)
.sink { [weak self] _ in
self?.updateKeyringInfo()
self?.updateInfo()
}
}

public func setupObservers() {
guard !isObserving else { return }
self.keyringServiceObserver = KeyringServiceObserver(
keyringService: keyringService,
_keyringReset: { [weak self] in
_walletReset: { [weak self] in
self?.isOnboardingVisible = true
self?.updateKeyringInfo()
self?.updateInfo()
},
_keyringCreated: { [weak self] keyringId in
_walletCreated: { [weak self] in
guard let self else { return }
if self.isOnboardingVisible, !self.isCreatingWallet, keyringId == BraveWallet.KeyringId.default {
if self.isOnboardingVisible, !self.isCreatingWallet {
// Another window has created a wallet. We should dismiss onboarding on this
// window and allow the other window to continue with it's onboarding flow.
self.isOnboardingVisible = false
}

Task { @MainActor in
let allAccounts = await self.keyringService.allAccounts()
let allAccountsForKeyring = allAccounts.accounts.filter { $0.keyringId == keyringId }
// if the new Keyring doesn't have a selected account, select the first account
if allAccounts.selectedAccount == nil, let newAccount = allAccountsForKeyring.first {
await self.keyringService.setSelectedAccount(newAccount.accountId)
}
self.updateKeyringInfo()
}
self.updateInfo()
},
_walletRestored: { [weak self] in
guard let self else { return }
Expand All @@ -248,34 +234,34 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
self.isOnboardingVisible = false
}

self.updateKeyringInfo()
self.updateInfo()
},
_locked: { [weak self] in
// Put this in the background since biometrics prompt will block the main queue
DispatchQueue.main.async {
self?.updateKeyringInfo()
self?.updateInfo()
}
},
_unlocked: { [weak self] in
self?.updateKeyringInfo()
self?.updateInfo()
},
_backedUp: { [weak self] in
self?.updateKeyringInfo()
self?.updateInfo()
},
_accountsChanged: { [weak self] in
self?.updateKeyringInfo()
self?.updateInfo()
},
_selectedWalletAccountChanged: { [weak self] _ in
self?.updateKeyringInfo()
self?.updateInfo()
},
_selectedDappAccountChanged: { [weak self] _, _ in
self?.updateKeyringInfo()
self?.updateInfo()
}
)
self.rpcServiceObserver = JsonRpcServiceObserver(
rpcService: rpcService,
_chainChangedEvent: { [weak self] _, _, _ in
self?.updateKeyringInfo()
self?.updateInfo()
}
)
}
Expand All @@ -285,7 +271,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
rpcServiceObserver = nil
}

private func updateKeyringInfo() {
private func updateInfo() {
if UIApplication.shared.applicationState != .active {
// Changes made in the backgroud due to timers going off at launch don't
// re-render things properly.
Expand All @@ -295,21 +281,19 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
}
Task { @MainActor in // fetch all KeyringInfo for all coin types
let allAccounts = await keyringService.allAccounts()
let allKeyrings = await keyringService.keyrings(for: WalletConstants.supportedCoinTypes())
if let defaultKeyring = allKeyrings.first(where: { $0.id == BraveWallet.KeyringId.default }) {
self.defaultKeyring = defaultKeyring
self.isDefaultKeyringLoaded = true
self.isDefaultKeyringCreated = defaultKeyring.isKeyringCreated
// fallback case where user completed front-end onboarding, but has no keyring created/accounts.
if !defaultKeyring.isKeyringCreated && Preferences.Wallet.isOnboardingCompleted.value {
Preferences.Wallet.isOnboardingCompleted.reset()
}
}
self.allKeyrings = allKeyrings
self.allAccounts = allAccounts.accounts
if let selectedAccount = allAccounts.selectedAccount {
self.selectedAccount = selectedAccount
}
self.isWalletCreated = await keyringService.isWalletCreated()
// fallback case where user completed front-end onboarding, but has no keyring created/accounts.
// this can occur if we crash prior to saving, ex. brave-ios #8291
if !isWalletCreated && Preferences.Wallet.isOnboardingCompleted.value {
Preferences.Wallet.isOnboardingCompleted.reset()
}
self.isWalletLocked = await keyringService.isLocked()
self.isWalletBackedUp = await keyringService.isWalletBackedUp()
self.isLoaded = true
}
}

Expand All @@ -331,6 +315,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
self.isCreatingWallet = false
self.isRestoringWallet = false
self.isOnboardingVisible = false
updateInfo()
}

func notifyWalletBackupComplete() {
Expand All @@ -340,10 +325,11 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
func lock() {
lockedManually = true
keyringService.lock()
isWalletLocked = true
}

func unlock(password: String, completion: @escaping (Bool) -> Void) {
if !defaultKeyring.isKeyringCreated {
if !isWalletCreated {
completion(false)
return
}
Expand All @@ -352,13 +338,14 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
if unlocked {
// Reset this state for next unlock
self?.lockedManually = false
self?.isWalletLocked = false
}
self?.updateKeyringInfo()
self?.updateInfo()
}
}

func validate(password: String, completion: @escaping (Bool) -> Void) {
if !defaultKeyring.isKeyringCreated {
if !isWalletCreated {
completion(false)
return
}
Expand Down Expand Up @@ -390,7 +377,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
func createWallet(password: String, completion: ((String) -> Void)? = nil) {
isCreatingWallet = true
keyringService.createWallet(password) { [weak self] mnemonic in
self?.updateKeyringInfo()
self?.updateInfo()
if !mnemonic.isEmpty {
self?.passwordToSaveInBiometric = password
}
Expand Down Expand Up @@ -424,7 +411,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
// Restoring from wallet means you already have your phrase backed up
self.passwordToSaveInBiometric = password
self.notifyWalletBackupComplete()
self.updateKeyringInfo()
self.updateInfo()
self.resetKeychainStoredPassword()
}
for coin in WalletConstants.supportedCoinTypes(.dapps) { // only coin types support dapps have permission management
Expand Down Expand Up @@ -453,7 +440,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
keyringId: BraveWallet.KeyringId.keyringId(for: coin, on: chainId),
accountName: name
) { accountInfo in
self.updateKeyringInfo()
self.updateInfo()
completion?(accountInfo != nil)
}
}
Expand All @@ -472,7 +459,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
}
} else {
keyringService.importAccount(name, privateKey: privateKey, coin: coin) { accountInfo in
self.updateKeyringInfo()
self.updateInfo()
completion?(accountInfo)
}
}
Expand All @@ -496,14 +483,14 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
) { success in
completion?(success)
if success {
self.updateKeyringInfo()
self.updateInfo()
}
}
}

func renameAccount(_ account: BraveWallet.AccountInfo, name: String, completion: ((Bool) -> Void)? = nil) {
let handler: (Bool) -> Void = { success in
self.updateKeyringInfo()
self.updateInfo()
completion?(success)
}
keyringService.setAccountName(account.accountId, name: name, completion: handler)
Expand All @@ -516,7 +503,7 @@ public class KeyringStore: ObservableObject, WalletObserverStore {
}

func notifyUserInteraction() {
if defaultKeyring.isLocked {
if isWalletLocked {
// Auto-lock isn't running until the keyring is unlocked
return
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveWallet/Crypto/Stores/UserAssetsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class UserAssetsStore: ObservableObject, WalletObserverStore {
guard !isObserving else { return }
self.keyringServiceObserver = KeyringServiceObserver(
keyringService: keyringService,
_keyringCreated: { [weak self] _ in
_walletCreated: { [weak self] in
self?.update()
}
)
Expand Down
Loading

0 comments on commit a613931

Please sign in to comment.