Skip to content

Commit

Permalink
Fix brave/brave-ios#7730: Bump Brave Core to 1.57.9 (brave/brave-ios#…
Browse files Browse the repository at this point in the history
…7731)

* AccountInfo refactor updates

* Update BraveCore to v1.57.9

---------

Co-authored-by: Kyle Hickinson <[email protected]>
  • Loading branch information
StephenHeaps and kylehickinson authored Jul 17, 2023
1 parent 3f0419c commit b42880d
Show file tree
Hide file tree
Showing 42 changed files with 328 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ extension ProviderDisplayString {
}

extension BraveCore.BraveRewards.PublisherInfo: ProviderDisplayString {}
extension BraveCore.BraveRewards.PendingContributionInfo: ProviderDisplayString {}

extension BraveCore.BraveRewards.PublisherInfo {
/// The display name to show when showing Publisher names (example: "X on GitHub", "Y on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ class AdsNotificationHandler: BraveAdsNotificationHandler {
guard let self = self else { return }
switch action {
case .opened:
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .clicked)
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .clicked, completion: { _ in })
case .dismissed:
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .dismissed)
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .dismissed, completion: { _ in })
case .timedOut:
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .timedOut)
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .timedOut, completion: { _ in })
case .disliked:
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .dismissed)
self.ads.toggleThumbsDown(forAd: notification.creativeInstanceID, advertiserId: notification.advertiserID)
self.ads.reportNotificationAdEvent(notification.placementID, eventType: .dismissed, completion: { _ in })
self.ads.toggleThumbsDown(forAd: notification.creativeInstanceID, advertiserId: notification.advertiserID, segment: notification.segment)
}
self.actionOccured?(notification, action)
}

ads.reportNotificationAdEvent(notification.placementID, eventType: .viewed)
ads.reportNotificationAdEvent(notification.placementID, eventType: .viewed, completion: { _ in })
notificationsPresenter?.display(notification: rewardsNotification, from: presentingController)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public class BrowserViewController: UIViewController {
if rewards.isEnabled {
rewards.startLedgerService(nil)
} else {
rewards.ads.initialize { _ in }
rewards.ads.initialize(walletInfo: .init()) { _ in }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ extension Tab: BraveWalletProviderDelegate {
let origin = getOrigin()

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

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

func keyringRestored(_ keyringId: String) {
func keyringRestored(_ keyringId: BraveWallet.KeyringId) {
}

func keyringReset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ class NewTabPageViewController: UIViewController {
rewards.ads.reportNewTabPageAdEvent(
background.wallpaperId.uuidString,
creativeInstanceId: sponsoredBackground.creativeInstanceId,
eventType: event
eventType: event,
completion: { _ in }
)
}
}
Expand Down Expand Up @@ -560,7 +561,7 @@ class NewTabPageViewController: UIViewController {
Preferences.BraveNews.userOptedIn.value = true
Preferences.BraveNews.isShowingOptIn.value = false
Preferences.BraveNews.isEnabled.value = true
rewards.ads.initialize { [weak self] _ in
rewards.ads.initialize(walletInfo: .init()) { [weak self] _ in
// Initialize ads if it hasn't already been done
self?.loadFeedContents()
}
Expand Down Expand Up @@ -588,7 +589,8 @@ class NewTabPageViewController: UIViewController {
rewards.ads.reportPromotedContentAdEvent(
item.content.urlHash,
creativeInstanceId: creativeInstanceID,
eventType: .clicked
eventType: .clicked,
completion: { _ in }
)
}
delegate?.navigateToInput(
Expand Down Expand Up @@ -616,7 +618,8 @@ class NewTabPageViewController: UIViewController {
rewards.ads.reportInlineContentAdEvent(
ad.placementID,
creativeInstanceId: ad.creativeInstanceID,
eventType: .clicked
eventType: .clicked,
completion: { _ in }
)
}
delegate?.navigateToInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class BraveNewsSectionProvider: NSObject, NTPObservableSectionProvider {
self?.rewards.ads.reportPromotedContentAdEvent(
item.content.urlHash,
creativeInstanceId: creativeInstanceID,
eventType: .viewed
eventType: .viewed,
completion: { _ in }
)
}
}
Expand All @@ -218,7 +219,8 @@ class BraveNewsSectionProvider: NSObject, NTPObservableSectionProvider {
self?.rewards.ads.reportInlineContentAdEvent(
ad.placementID,
creativeInstanceId: ad.creativeInstanceID,
eventType: .viewed
eventType: .viewed,
completion: { _ in }
)
self?.recordWeeklyAdsViewedP3A(adViewed: true)
}
Expand Down
22 changes: 13 additions & 9 deletions Sources/Brave/Frontend/Rewards/BraveRewards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,28 @@ public class BraveRewards: NSObject {
}

private(set) var isAdsInitialized: Bool = false
private func fetchWalletAndInitializeAds() {
private func fetchWalletAndInitializeAds(toggleAds: Bool? = nil) {
if isAdsInitialized {
return
}
isAdsInitialized = true
guard let ledger = ledger else { return }
ledger.currentWalletInfo { wallet in
if let wallet = wallet {
var walletInfo: BraveAds.WalletInfo?
if let wallet {
let seed = wallet.recoverySeed.map(\.uint8Value)
self.ads.updateWalletInfo(
wallet.paymentId,
base64Seed: Data(seed).base64EncodedString()
walletInfo = .init(
paymentId: wallet.paymentId,
recoverySeed: Data(seed).base64EncodedString()
)
}
self.ads.initialize() { success in
self.ads.initialize(walletInfo: walletInfo ?? .init()) { success in
if !success {
self.isAdsInitialized = false
} else {
if let toggleAds {
self.ads.isEnabled = toggleAds
}
}
}
}
Expand Down Expand Up @@ -120,7 +125,6 @@ public class BraveRewards: NSObject {
guard let self = self else { return }
self.ledger?.setAutoContributeEnabled(newValue)
let wasEnabled = self.ads.isEnabled
self.ads.isEnabled = newValue
if !wasEnabled && newValue {
Preferences.Rewards.adsEnabledTimestamp.value = Date()
} else if wasEnabled && !newValue {
Expand All @@ -130,7 +134,7 @@ public class BraveRewards: NSObject {
if !newValue {
self.proposeAdsShutdown()
} else {
self.fetchWalletAndInitializeAds()
self.fetchWalletAndInitializeAds(toggleAds: true)
}
self.didChangeValue(for: \.isEnabled)
}
Expand Down Expand Up @@ -163,7 +167,7 @@ public class BraveRewards: NSObject {
at: configuration.storageURL.appendingPathComponent("ads")
)
if ads.isEnabled {
ads.initialize { _ in }
ads.initialize(walletInfo: .init()) { _ in }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveNews/Customize/NewsSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public struct NewsSettingsView: View {
OptInView { @MainActor in
Preferences.BraveNews.isShowingOptIn.value = false
// Initialize ads if it hasn't already been done
await dataSource.ads?.initialize()
await dataSource.ads?.initialize(walletInfo: .init())
if dataSource.isSourcesExpired {
await withCheckedContinuation { c in
dataSource.load {
Expand Down
7 changes: 4 additions & 3 deletions Sources/BraveShared/Extensions/BraveAdsExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import Shared
import UIKit

extension BraveAds {
public func initialize(_ completion: @escaping (Bool) -> Void) {
public func initialize(walletInfo: BraveAds.WalletInfo, completion: @escaping (Bool) -> Void) {
self.initialize(
with: .init(deviceId: UIDevice.current.identifierForVendor?.uuidString ?? ""),
buildChannelInfo: .init(
isRelease: AppConstants.buildChannel == .release,
name: AppConstants.buildChannel.rawValue
),
walletInfo: walletInfo,
completion: completion
)
}

@discardableResult
@MainActor public func initialize() async -> Bool {
@MainActor public func initialize(walletInfo: BraveAds.WalletInfo) async -> Bool {
await withCheckedContinuation { c in
self.initialize { success in
self.initialize(walletInfo: walletInfo) { success in
c.resume(returning: success)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/BraveWallet/Crypto/Stores/AccountActivityStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ class AccountActivityStore: ObservableObject {
}

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

func keyringRestored(_ keyringId: BraveWallet.KeyringId) {
}

func keyringReset() {
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 @@ -366,10 +366,10 @@ extension AssetDetailStore: BraveWalletKeyringServiceObserver {
update()
}

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

func keyringRestored(_ keyringId: String) {
func keyringRestored(_ keyringId: BraveWallet.KeyringId) {
}

func locked() {
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 @@ -551,7 +551,7 @@ extension CryptoStore: BraveWalletKeyringServiceObserver {
WalletProviderAccountCreationRequestManager.shared.cancelAllPendingRequests(coins: [.eth, .sol])
rejectAllPendingWebpageRequests()
}
public func keyringCreated(_ keyringId: String) {
public func keyringCreated(_ keyringId: BraveWallet.KeyringId) {
Task { @MainActor [weak self] in
if let newCoin = WalletConstants.supportedCoinTypes.first(where: { $0.keyringId == keyringId }) {
self?.userAssetManager.migrateUserAssets(for: newCoin, completion: {
Expand All @@ -560,7 +560,7 @@ extension CryptoStore: BraveWalletKeyringServiceObserver {
}
}
}
public func keyringRestored(_ keyringId: String) {
public func keyringRestored(_ keyringId: BraveWallet.KeyringId) {
// if a keyring is restored, we want to reset user assets local storage
// and migrate with for new keyring
WalletUserAssetGroup.removeAllGroup() { [weak self] in
Expand Down
45 changes: 25 additions & 20 deletions Sources/BraveWallet/Crypto/Stores/KeyringStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ struct RecoveryWord: Hashable, Identifiable {
/// This wraps a KeyringService that you would obtain through BraveCore and makes it observable
public class KeyringStore: ObservableObject {
/// The defualt keyring information. By default this is an empty keyring which has no accounts.
@Published private(set) var defaultKeyring: BraveWallet.KeyringInfo = .init()
@Published private(set) var defaultKeyring: BraveWallet.KeyringInfo = .init(
id: .default,
isKeyringCreated: false,
isLocked: true,
isBackedUp: false,
accountInfos: []
)
/// 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 Down Expand Up @@ -142,7 +148,7 @@ public class KeyringStore: ObservableObject {
rpcService.add(self)
updateKeyringInfo()

self.keyringService.keyringInfo(BraveWallet.DefaultKeyringId) { [self] keyringInfo in
self.keyringService.keyringInfo(BraveWallet.KeyringId.default) { [self] keyringInfo in
isOnboardingVisible = !keyringInfo.isKeyringCreated
if isKeychainPasswordStored && isOnboardingVisible {
// If a user deletes the app and they had a stored user password in the past that keychain item
Expand Down Expand Up @@ -172,7 +178,7 @@ public class KeyringStore: ObservableObject {
let selectedAccountAddress = await keyringService.selectedAccount(selectedCoin)
let allKeyrings = await keyringService.keyrings(for: WalletConstants.supportedCoinTypes)
self.defaultAccounts = await keyringService.defaultAccounts(for: WalletConstants.supportedCoinTypes)
if let defaultKeyring = allKeyrings.first(where: { $0.id == BraveWallet.DefaultKeyringId }) {
if let defaultKeyring = allKeyrings.first(where: { $0.id == BraveWallet.KeyringId.default }) {
self.defaultKeyring = defaultKeyring
self.isDefaultKeyringCreated = defaultKeyring.isKeyringCreated
}
Expand Down Expand Up @@ -208,11 +214,7 @@ public class KeyringStore: ObservableObject {

if coreSelectedAccount != account.address {
// Update the selected account in core
let success = await keyringService.setSelectedAccount(
account.coin,
keyringId: account.keyringId,
address: account.address
)
let success = await keyringService.setSelectedAccount(account.accountId)
if success {
self.selectedAccount = account
}
Expand Down Expand Up @@ -312,9 +314,13 @@ public class KeyringStore: ObservableObject {
}

func addPrimaryAccount(_ name: String, coin: BraveWallet.CoinType, completion: ((Bool) -> Void)? = nil) {
keyringService.addAccount(name, coin: coin) { success in
keyringService.addAccount(
coin,
keyringId: coin.keyringId,
accountName: name
) { accountInfo in
self.updateKeyringInfo()
completion?(success)
completion?(accountInfo != nil)
}
}

Expand Down Expand Up @@ -351,7 +357,10 @@ public class KeyringStore: ObservableObject {
}

func removeSecondaryAccount(for account: BraveWallet.AccountInfo, password: String, completion: ((Bool) -> Void)? = nil) {
keyringService.removeImportedAccount(account.coin, keyringId: account.keyringId, address: account.address, password: password) { success in
keyringService.removeAccount(
account.accountId,
password: password
) { success in
completion?(success)
if success {
self.updateKeyringInfo()
Expand All @@ -364,15 +373,11 @@ public class KeyringStore: ObservableObject {
self.updateKeyringInfo()
completion?(success)
}
if account.isImported {
keyringService.setKeyringImportedAccountName(account.coin, keyringId: account.keyringId, address: account.address, name: name, completion: handler)
} else {
keyringService.setKeyringDerivedAccountName(account.coin, keyringId: account.keyringId, address: account.address, name: name, completion: handler)
}
keyringService.setAccountName(account.accountId, name: name, completion: handler)
}

func privateKey(for account: BraveWallet.AccountInfo, password: String, completion: @escaping (String?) -> Void) {
keyringService.encodePrivateKey(forExport: account.coin, keyringId: account.keyringId, address: account.address, password: password, completion: { key in
keyringService.encodePrivateKey(forExport: account.accountId, password: password, completion: { key in
completion(key.isEmpty ? nil : key)
})
}
Expand Down Expand Up @@ -429,21 +434,21 @@ extension KeyringStore: BraveWalletKeyringServiceObserver {
updateKeyringInfo()
}

public func keyringCreated(_ keyringId: String) {
public func keyringCreated(_ keyringId: BraveWallet.KeyringId) {
Task { @MainActor in
let newKeyring = await keyringService.keyringInfo(keyringId)
if let newKeyringCoin = newKeyring.coin {
let selectedAccount = await keyringService.selectedAccount(newKeyringCoin)
// if the new Keyring doesn't have a selected account, select the first account
if selectedAccount == nil, let newAccount = newKeyring.accountInfos.first {
await keyringService.setSelectedAccount(newAccount.coin, keyringId: newAccount.keyringId, address: newAccount.address)
await keyringService.setSelectedAccount(newAccount.accountId)
}
}
updateKeyringInfo()
}
}

public func keyringRestored(_ keyringId: String) {
public func keyringRestored(_ keyringId: BraveWallet.KeyringId) {
updateKeyringInfo()
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/NFTStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ extension NFTStore: BraveWalletKeyringServiceObserver {
}
public func backedUp() {
}
public func keyringCreated(_ keyringId: String) {
public func keyringCreated(_ keyringId: BraveWallet.KeyringId) {
}
public func keyringRestored(_ keyringId: String) {
public func keyringRestored(_ keyringId: BraveWallet.KeyringId) {
}
public func locked() {
}
Expand Down
Loading

0 comments on commit b42880d

Please sign in to comment.