Skip to content

Commit

Permalink
Add separate Blockchair restore modes
Browse files Browse the repository at this point in the history
  • Loading branch information
esen committed Nov 14, 2023
1 parent a3a70a4 commit fbca333
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 45 deletions.
6 changes: 3 additions & 3 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12436,7 +12436,7 @@
repositoryURL = "https://github.com/horizontalsystems/BitcoinCore.Swift";
requirement = {
kind = exactVersion;
version = 2.1.1;
version = 2.1.3;
};
};
6B55461F2A6E73190054B524 /* XCRemoteSwiftPackageReference "UIExtensions.Swift" */ = {
Expand Down Expand Up @@ -12548,15 +12548,15 @@
repositoryURL = "https://github.com/horizontalsystems/BitcoinKit.Swift";
requirement = {
kind = exactVersion;
version = 2.1.2;
version = 2.1.3;
};
};
D3604E6728F02DF30066C366 /* XCRemoteSwiftPackageReference "BitcoinCashKit.Swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/horizontalsystems/BitcoinCashKit.Swift";
requirement = {
kind = exactVersion;
version = 2.1.1;
version = 2.1.2;
};
};
D3604E6A28F02E3F0066C366 /* XCRemoteSwiftPackageReference "LitecoinKit.Swift" */ = {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "APIplaceHolder@2x.png",
"filename" : "API_Hybrid@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "APIplaceHolder@3x.png",
"filename" : "API_Hybrid@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BtcBlockchainManager {
.bitcoinCash,
.ecash,
.litecoin,
.dash,
.dash
]

private let marketKit: MarketKit.Kit
Expand All @@ -30,6 +30,10 @@ class BtcBlockchainManager {
allBlockchains = []
}
}

private func fastestSyncMode(blockchainType: BlockchainType) -> BtcRestoreMode {
blockchainType.supports(restoreMode: .blockchair) ? .blockchair : .hybrid
}
}

extension BtcBlockchainManager {
Expand All @@ -46,23 +50,17 @@ extension BtcBlockchainManager {
}

func restoreMode(blockchainType: BlockchainType) -> BtcRestoreMode {
storage.btcRestoreMode(blockchainType: blockchainType) ?? .api
}

func apiSyncMode(blockchainType: BlockchainType) -> BitcoinCore.SyncMode {
switch blockchainType {
case .bitcoin, .bitcoinCash: return .blockchair(key: AppConfig.blockchairApiKey)
default: return .api
}
storage.btcRestoreMode(blockchainType: blockchainType) ?? fastestSyncMode(blockchainType: blockchainType)
}

func syncMode(blockchainType: BlockchainType, accountOrigin: AccountOrigin) -> BitcoinCore.SyncMode {
if accountOrigin == .created {
return apiSyncMode(blockchainType: blockchainType)
}
let _restoreMode = accountOrigin == .created
? fastestSyncMode(blockchainType: blockchainType)
: restoreMode(blockchainType: blockchainType)

switch restoreMode(blockchainType: blockchainType) {
case .api: return apiSyncMode(blockchainType: blockchainType)
switch _restoreMode {
case .blockchair: return .blockchair(key: AppConfig.blockchairApiKey)
case .hybrid: return .api
case .blockchain: return .full
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ extension BlockchainType {
}
}

func supports(restoreMode: BtcRestoreMode) -> Bool {
guard case .blockchair = restoreMode else {
return true
}

switch self {
case .bitcoin, .bitcoinCash: return true
default: return false
}
}

}

extension BlockchainType: Comparable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
enum BtcRestoreMode: String, CaseIterable, Identifiable, Codable {
case api
case blockchair
case hybrid
case blockchain

var id: Self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MarketKit
import BitcoinCore
import MarketKit

class BtcBlockchainSettingsService {
let blockchain: Blockchain
Expand All @@ -12,19 +12,15 @@ class BtcBlockchainSettingsService {
self.blockchain = blockchain
self.btcBlockchainManager = btcBlockchainManager

restoreModes = BtcRestoreMode.allCases.map { restoreMode in
let syncMode = btcBlockchainManager.apiSyncMode(blockchainType: blockchain.type)
return BtcSyncModeItem(blockchain: blockchain, restoreMode: restoreMode, syncMode: syncMode)
}
restoreModes = BtcRestoreMode.allCases
.filter { blockchain.type.supports(restoreMode: $0) }
.map { BtcSyncModeItem(blockchain: blockchain, restoreMode: $0) }
currentRestoreMode = btcBlockchainManager.restoreMode(blockchainType: blockchain.type)
}

}

extension BtcBlockchainSettingsService {

func save(restoreMode: BtcRestoreMode) {
btcBlockchainManager.save(restoreMode: restoreMode, blockchainType: blockchain.type)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ class BtcBlockchainSettingsViewModel: ObservableObject {
selectedRestoreMode = service.currentRestoreMode
restoreModes = service.restoreModes.map { restoreMode in
let image: BtcRestoreModeViewItem.Image
switch (restoreMode.restoreMode, restoreMode.syncMode) {
case (.api, .api):
image = .local(name: "api_placeholder_32")
case (.api, .blockchair):
switch restoreMode.restoreMode {
case .blockchair:
image = .local(name: "blockchair_32")
default:
case .hybrid:
image = .local(name: "api_placeholder_32")
case .blockchain:
image = .remote(url: service.blockchain.type.imageUrl)
}

let description: String
switch restoreMode.restoreMode {
case .api: description = "btc_restore_mode.fast".localized
case .blockchain: description = "btc_restore_mode.slow".localized
case .blockchair: description = "btc_restore_mode.blockchair".localized
case .hybrid: description = "btc_restore_mode.hybrid".localized
case .blockchain: description = "btc_restore_mode.blockchain".localized
}

return BtcRestoreModeViewItem(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BitcoinCore
import Combine
import MarketKit
import RxRelay
import RxSwift
import BitcoinCore

class BlockchainSettingsViewModel: ObservableObject {
private let btcBlockchainManager: BtcBlockchainManager
Expand All @@ -29,8 +29,7 @@ class BlockchainSettingsViewModel: ObservableObject {
btcItems = btcBlockchainManager.allBlockchains
.map { blockchain in
let restoreMode = btcBlockchainManager.restoreMode(blockchainType: blockchain.type)
let syncMode = btcBlockchainManager.apiSyncMode(blockchainType: blockchain.type)
return BtcSyncModeItem(blockchain: blockchain, restoreMode: restoreMode, syncMode: syncMode)
return BtcSyncModeItem(blockchain: blockchain, restoreMode: restoreMode)
}
.sorted { $0.blockchain.type.order < $1.blockchain.type.order }
}
Expand All @@ -55,13 +54,12 @@ extension BlockchainSettingsViewModel {
struct BtcSyncModeItem {
let blockchain: Blockchain
let restoreMode: BtcRestoreMode
let syncMode: BitcoinCore.SyncMode

var title: String {
switch (restoreMode, syncMode) {
case (.api, .api): return "API"
case (.api, .blockchair): return "Blockchair API"
default: return "sync_mode.from_blockchain".localized(blockchain.name)
switch restoreMode {
case .blockchair: return "Blockchair API"
case .hybrid: return "sync_mode.hybrid".localized
case .blockchain: return "sync_mode.from_blockchain".localized(blockchain.name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
"coin_settings.title" = "Blockchain Settings";
"coin_settings.bitcoin_cash_coin_type.title.type0" = "Legacy";
"coin_settings.bitcoin_cash_coin_type.title.type145" = "CashAddress";
"sync_mode.hybrid" = "Hybrid";
"sync_mode.from_blockchain" = "%@ Blockchain";
"blockchain_settings.description" = "Select the address format for receiving payments. A correct format should be chosen when restoring an existing wallet.";

Expand Down Expand Up @@ -1215,8 +1216,9 @@
"btc_blockchain_settings.restore_source.description" = "Choose the source Unstoppable app should use for synchronizing wallet(s).";
"btc_blockchain_settings.restore_source.alert" = "After changing Restore Source the wallet will have to resync itself with the %@ blockchain.";

"btc_restore_mode.fast" = "Fast, Private, Centralized";
"btc_restore_mode.slow" = "Slow, Private, Decentralized";
"btc_restore_mode.blockchair" = "Fast, Private, Centralized";
"btc_restore_mode.hybrid" = "API + Blockchain";
"btc_restore_mode.blockchain" = "Slow, Private, Decentralized";

"btc_transaction_sort_mode.shuffle" = "Shuffle";
"btc_transaction_sort_mode.shuffle.description" = "Random Indexing";
Expand Down

0 comments on commit fbca333

Please sign in to comment.