Skip to content

Commit

Permalink
Fix brave/brave-ios#7012: Make IpfsAPI non-optional in Wallet (brav…
Browse files Browse the repository at this point in the history
…e/brave-ios#7190)

Make `IpfsAPI` non-optional in Wallet. Add `TestIpfsAPI` for mocking & unit tests.
  • Loading branch information
StephenHeaps authored Apr 4, 2023
1 parent 0d911f2 commit 94df40b
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Growth

extension WalletStore {
/// Creates a WalletStore based on whether or not the user is in Private Mode
static func from(ipfsApi: IpfsAPI?, privateMode: Bool) -> WalletStore? {
static func from(ipfsApi: IpfsAPI, privateMode: Bool) -> WalletStore? {
guard
let keyringService = BraveWallet.KeyringServiceFactory.get(privateMode: privateMode),
let rpcService = BraveWallet.JsonRpcServiceFactory.get(privateMode: privateMode),
Expand Down Expand Up @@ -47,7 +47,7 @@ extension WalletStore {

extension CryptoStore {
/// Creates a CryptoStore based on whether or not the user is in Private Mode
static func from(ipfsApi: IpfsAPI?, privateMode: Bool) -> CryptoStore? {
static func from(ipfsApi: IpfsAPI, privateMode: Bool) -> CryptoStore? {
guard
let keyringService = BraveWallet.KeyringServiceFactory.get(privateMode: privateMode),
let rpcService = BraveWallet.JsonRpcServiceFactory.get(privateMode: privateMode),
Expand Down Expand Up @@ -201,7 +201,7 @@ extension Tab: BraveWalletProviderDelegate {
completion(.internal, nil)
return
}
case .fil:
case .fil, .btc:
// not supported
fallthrough
@unknown default:
Expand All @@ -210,7 +210,7 @@ extension Tab: BraveWalletProviderDelegate {
}
}

guard WalletStore.from(ipfsApi: nil, privateMode: isPrivate) != nil else {
guard !isPrivate else {
completion(.internal, nil)
return
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/Brave/Frontend/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,7 @@ class SettingsViewController: TableViewController {
let web3SettingsView = Web3SettingsView(
settingsStore: settingsStore,
networkStore: cryptoStore?.networkStore,
keyringStore: keyringStore,
ipfsAPI: ipfsAPI
keyringStore: keyringStore
).environment(\.openURL, .init(handler: { [weak self] url in
guard let self = self else { return .discarded }
(self.presentingViewController ?? self).dismiss(animated: true) { [self] in
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 @@ -32,7 +32,7 @@ class AccountActivityStore: ObservableObject {
private let txService: BraveWalletTxService
private let blockchainRegistry: BraveWalletBlockchainRegistry
private let solTxManagerProxy: BraveWalletSolanaTxManagerProxy
private let ipfsApi: IpfsAPI?
private let ipfsApi: IpfsAPI
/// Cache for storing `BlockchainToken`s that are not in user assets or our token registry.
/// This could occur with a dapp creating a transaction.
private var tokenInfoCache: [String: BraveWallet.BlockchainToken] = [:]
Expand All @@ -47,7 +47,7 @@ class AccountActivityStore: ObservableObject {
txService: BraveWalletTxService,
blockchainRegistry: BraveWalletBlockchainRegistry,
solTxManagerProxy: BraveWalletSolanaTxManagerProxy,
ipfsApi: IpfsAPI?
ipfsApi: IpfsAPI
) {
self.account = account
self.observeAccountUpdates = observeAccountUpdates
Expand Down
7 changes: 4 additions & 3 deletions Sources/BraveWallet/Crypto/Stores/CryptoStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class CryptoStore: ObservableObject {
private let txService: BraveWalletTxService
private let ethTxManagerProxy: BraveWalletEthTxManagerProxy
private let solTxManagerProxy: BraveWalletSolanaTxManagerProxy
private let ipfsApi: IpfsAPI?
private let ipfsApi: IpfsAPI

public init(
keyringService: BraveWalletKeyringService,
Expand All @@ -102,7 +102,7 @@ public class CryptoStore: ObservableObject {
txService: BraveWalletTxService,
ethTxManagerProxy: BraveWalletEthTxManagerProxy,
solTxManagerProxy: BraveWalletSolanaTxManagerProxy,
ipfsApi: IpfsAPI?
ipfsApi: IpfsAPI
) {
self.keyringService = keyringService
self.rpcService = rpcService
Expand Down Expand Up @@ -278,7 +278,8 @@ public class CryptoStore: ObservableObject {
keyringService: keyringService,
walletService: walletService,
rpcService: rpcService,
txService: txService
txService: txService,
ipfsApi: ipfsApi
)

private var nftDetailStore: NFTDetailStore?
Expand Down
9 changes: 4 additions & 5 deletions Sources/BraveWallet/Crypto/Stores/NFTDetailStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ struct NFTMetadata: Codable, Equatable {
self.description = description
}

func httpfyIpfsUrl(ipfsApi: IpfsAPI?) -> NFTMetadata {
guard let ipfsApi,
let imageURLString,
func httpfyIpfsUrl(ipfsApi: IpfsAPI) -> NFTMetadata {
guard let imageURLString,
imageURLString.hasPrefix("ipfs://"),
let url = URL(string: imageURLString) else {
return NFTMetadata(imageURLString: self.imageURLString, name: self.name, description: self.description)
Expand All @@ -51,15 +50,15 @@ struct NFTMetadata: Codable, Equatable {

class NFTDetailStore: ObservableObject {
private let rpcService: BraveWalletJsonRpcService
private let ipfsApi: IpfsAPI?
private let ipfsApi: IpfsAPI
let nft: BraveWallet.BlockchainToken
@Published var isLoading: Bool = false
@Published var nftMetadata: NFTMetadata?
@Published var networkInfo: BraveWallet.NetworkInfo = .init()

init(
rpcService: BraveWalletJsonRpcService,
ipfsApi: IpfsAPI?,
ipfsApi: IpfsAPI,
nft: BraveWallet.BlockchainToken,
nftMetadata: NFTMetadata?
) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/PortfolioStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ public class PortfolioStore: ObservableObject {
private let walletService: BraveWalletBraveWalletService
private let assetRatioService: BraveWalletAssetRatioService
private let blockchainRegistry: BraveWalletBlockchainRegistry
private let ipfsApi: IpfsAPI?
private let ipfsApi: IpfsAPI

public init(
keyringService: BraveWalletKeyringService,
rpcService: BraveWalletJsonRpcService,
walletService: BraveWalletBraveWalletService,
assetRatioService: BraveWalletAssetRatioService,
blockchainRegistry: BraveWalletBlockchainRegistry,
ipfsApi: IpfsAPI?
ipfsApi: IpfsAPI
) {
self.keyringService = keyringService
self.rpcService = rpcService
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/SendTokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public class SendTokenStore: ObservableObject {
private var sendAmountUpdatedTimer: Timer?
private var prefilledToken: BraveWallet.BlockchainToken?
private var metadataCache: [String: NFTMetadata] = [:]
private let ipfsApi: IpfsAPI?
private let ipfsApi: IpfsAPI

public init(
keyringService: BraveWalletKeyringService,
Expand All @@ -145,7 +145,7 @@ public class SendTokenStore: ObservableObject {
ethTxManagerProxy: BraveWalletEthTxManagerProxy,
solTxManagerProxy: BraveWalletSolanaTxManagerProxy,
prefilledToken: BraveWallet.BlockchainToken?,
ipfsApi: IpfsAPI?
ipfsApi: IpfsAPI
) {
self.keyringService = keyringService
self.rpcService = rpcService
Expand Down
3 changes: 3 additions & 0 deletions Sources/BraveWallet/Crypto/Stores/SettingsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,22 @@ public class SettingsStore: ObservableObject {
private let walletService: BraveWalletBraveWalletService
private let rpcService: BraveWalletJsonRpcService
private let txService: BraveWalletTxService
let ipfsApi: IpfsAPI
private let keychain: KeychainType

public init(
keyringService: BraveWalletKeyringService,
walletService: BraveWalletBraveWalletService,
rpcService: BraveWalletJsonRpcService,
txService: BraveWalletTxService,
ipfsApi: IpfsAPI,
keychain: KeychainType = Keychain()
) {
self.keyringService = keyringService
self.walletService = walletService
self.rpcService = rpcService
self.txService = txService
self.ipfsApi = ipfsApi
self.keychain = keychain

keyringService.add(self)
Expand Down
8 changes: 4 additions & 4 deletions Sources/BraveWallet/Crypto/Stores/UserAssetsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class AssetStore: ObservableObject, Equatable {

private let walletService: BraveWalletBraveWalletService
private let rpcService: BraveWalletJsonRpcService
private let ipfsApi: IpfsAPI?
private let ipfsApi: IpfsAPI
private(set) var isCustomToken: Bool

init(
walletService: BraveWalletBraveWalletService,
rpcService: BraveWalletJsonRpcService,
network: BraveWallet.NetworkInfo,
token: BraveWallet.BlockchainToken,
ipfsApi: IpfsAPI?,
ipfsApi: IpfsAPI,
isCustomToken: Bool,
isVisible: Bool
) {
Expand Down Expand Up @@ -68,7 +68,7 @@ public class UserAssetsStore: ObservableObject {
private let rpcService: BraveWalletJsonRpcService
private let keyringService: BraveWalletKeyringService
private let assetRatioService: BraveWalletAssetRatioService
private let ipfsApi: IpfsAPI?
private let ipfsApi: IpfsAPI
private var allTokens: [BraveWallet.BlockchainToken] = []
private var timer: Timer?

Expand All @@ -78,7 +78,7 @@ public class UserAssetsStore: ObservableObject {
rpcService: BraveWalletJsonRpcService,
keyringService: BraveWalletKeyringService,
assetRatioService: BraveWalletAssetRatioService,
ipfsApi: IpfsAPI?
ipfsApi: IpfsAPI
) {
self.walletService = walletService
self.blockchainRegistry = blockchainRegistry
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/WalletStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class WalletStore {
txService: BraveWalletTxService,
ethTxManagerProxy: BraveWalletEthTxManagerProxy,
solTxManagerProxy: BraveWalletSolanaTxManagerProxy,
ipfsApi: IpfsAPI?
ipfsApi: IpfsAPI
) {
self.keyringStore = .init(keyringService: keyringService, walletService: walletService, rpcService: rpcService)
self.setUp(
Expand All @@ -57,7 +57,7 @@ public class WalletStore {
txService: BraveWalletTxService,
ethTxManagerProxy: BraveWalletEthTxManagerProxy,
solTxManagerProxy: BraveWalletSolanaTxManagerProxy,
ipfsApi: IpfsAPI?
ipfsApi: IpfsAPI
) {
self.cancellable = self.keyringStore.$defaultKeyring
.map(\.isKeyringCreated)
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Extensions/RpcServiceExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ extension BraveWalletJsonRpcService {
}

/// Returns a nullable NFT metadata
@MainActor func fetchNFTMetadata(for token: BraveWallet.BlockchainToken, ipfsApi: IpfsAPI?) async -> NFTMetadata? {
@MainActor func fetchNFTMetadata(for token: BraveWallet.BlockchainToken, ipfsApi: IpfsAPI) async -> NFTMetadata? {
var metaDataString = ""
if token.isErc721 {
let (_, metaData, result, errMsg) = await self.erc721Metadata(token.contractAddress, tokenId: token.tokenId, chainId: token.chainId)
Expand All @@ -302,7 +302,7 @@ extension BraveWalletJsonRpcService {
}

/// Returns a map of Token.id with its NFT metadata
@MainActor func fetchNFTMetadata(tokens: [BraveWallet.BlockchainToken], ipfsApi: IpfsAPI?) async -> [String: NFTMetadata] {
@MainActor func fetchNFTMetadata(tokens: [BraveWallet.BlockchainToken], ipfsApi: IpfsAPI) async -> [String: NFTMetadata] {
await withTaskGroup(of: [String: NFTMetadata].self) { @MainActor [weak self] group -> [String: NFTMetadata] in
guard let self = self else { return [:] }
for token in tokens {
Expand Down
30 changes: 30 additions & 0 deletions Sources/BraveWallet/Extensions/TestIpfsAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import BraveCore

#if DEBUG

class TestIpfsAPI: IpfsAPI {
var ipfsGateway: URL?
var nftIpfsGateway: URL?

var _resolveGatewayUrl: ((_ input: URL) -> URL?)?
func resolveGatewayUrl(for input: URL) -> URL? {
_resolveGatewayUrl?(input)
}

var _resolveGatewayUrlForNft: ((_ input: URL) -> URL?)?
func resolveGatewayUrl(forNft input: URL) -> URL? {
_resolveGatewayUrlForNft?(input)
}

var _contentHashToCIDv1URL: ((_ contentHash: [NSNumber]) -> URL?)?
func contentHashToCIDv1URL(for contentHash: [NSNumber]) -> URL? {
_contentHashToCIDv1URL?(contentHash)
}
}

#endif
11 changes: 6 additions & 5 deletions Sources/BraveWallet/Preview Content/MockStores.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension WalletStore {
txService: MockTxService(),
ethTxManagerProxy: MockEthTxManagerProxy(),
solTxManagerProxy: BraveWallet.TestSolanaTxManagerProxy.previewProxy,
ipfsApi: nil
ipfsApi: TestIpfsAPI()
)
}
}
Expand All @@ -38,7 +38,7 @@ extension CryptoStore {
txService: MockTxService(),
ethTxManagerProxy: MockEthTxManagerProxy(),
solTxManagerProxy: BraveWallet.TestSolanaTxManagerProxy.previewProxy,
ipfsApi: nil
ipfsApi: TestIpfsAPI()
)
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ extension SendTokenStore {
ethTxManagerProxy: MockEthTxManagerProxy(),
solTxManagerProxy: BraveWallet.TestSolanaTxManagerProxy.previewProxy,
prefilledToken: .previewToken,
ipfsApi: nil
ipfsApi: TestIpfsAPI()
)
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ extension UserAssetsStore {
rpcService: MockJsonRpcService(),
keyringService: MockKeyringService(),
assetRatioService: MockAssetRatioService(),
ipfsApi: nil
ipfsApi: TestIpfsAPI()
)
}
}
Expand All @@ -173,7 +173,7 @@ extension AccountActivityStore {
txService: MockTxService(),
blockchainRegistry: MockBlockchainRegistry(),
solTxManagerProxy: BraveWallet.TestSolanaTxManagerProxy.previewProxy,
ipfsApi: nil
ipfsApi: TestIpfsAPI()
)
}
}
Expand Down Expand Up @@ -204,6 +204,7 @@ extension SettingsStore {
walletService: MockBraveWalletService(),
rpcService: MockJsonRpcService(),
txService: MockTxService(),
ipfsApi: TestIpfsAPI(),
keychain: TestableKeychain()
)
}
Expand Down
Loading

0 comments on commit 94df40b

Please sign in to comment.