Skip to content

Commit

Permalink
Use blockchair API for BTC and BCH fast synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
esen committed Oct 31, 2023
1 parent 9f6f743 commit 38fda3a
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 61 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy_appstore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ jobs:
XCCONFIG_PROD_TRONGRID_API_KEY: ${{ secrets.XCCONFIG_PROD_TRONGRID_API_KEY }}
XCCONFIG_PROD_UNSTOPPABLE_DOMAINS_API_KEY: ${{ secrets.XCCONFIG_PROD_UNSTOPPABLE_DOMAINS_API_KEY }}
XCCONFIG_PROD_ONE_INCH_API_KEY: ${{ secrets.XCCONFIG_PROD_ONE_INCH_API_KEY }}
XCCONFIG_PROD_BLOCKCHAIR_API_KEY: ${{ secrets.XCCONFIG_PROD_BLOCKCHAIR_API_KEY }}
1 change: 1 addition & 0 deletions .github/workflows/deploy_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ jobs:
XCCONFIG_DEV_TRONGRID_API_KEY: ${{ secrets.XCCONFIG_DEV_TRONGRID_API_KEY }}
XCCONFIG_DEV_UNSTOPPABLE_DOMAINS_API_KEY: ${{ secrets.XCCONFIG_DEV_UNSTOPPABLE_DOMAINS_API_KEY }}
XCCONFIG_DEV_ONE_INCH_API_KEY: ${{ secrets.XCCONFIG_DEV_ONE_INCH_API_KEY }}
XCCONFIG_DEV_BLOCKCHAIR_API_KEY: ${{ secrets.XCCONFIG_DEV_BLOCKCHAIR_API_KEY }}
12 changes: 6 additions & 6 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12175,7 +12175,7 @@
repositoryURL = "https://github.com/horizontalsystems/BitcoinCore.Swift";
requirement = {
kind = exactVersion;
version = 2.0.3;
version = 2.1.0;
};
};
6B55461F2A6E73190054B524 /* XCRemoteSwiftPackageReference "UIExtensions.Swift" */ = {
Expand All @@ -12191,7 +12191,7 @@
repositoryURL = "https://github.com/horizontalsystems/ECashKit.Swift.git";
requirement = {
kind = exactVersion;
version = 2.0.1;
version = 2.1.0;
};
};
6BDA29AE29D6F934003847ED /* XCRemoteSwiftPackageReference "HsToolKit.Swift" */ = {
Expand Down Expand Up @@ -12279,23 +12279,23 @@
repositoryURL = "https://github.com/horizontalsystems/BitcoinKit.Swift";
requirement = {
kind = exactVersion;
version = 2.0.1;
version = 2.1.0;
};
};
D3604E6728F02DF30066C366 /* XCRemoteSwiftPackageReference "BitcoinCashKit.Swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/horizontalsystems/BitcoinCashKit.Swift";
requirement = {
kind = exactVersion;
version = 2.0.0;
version = 2.1.0;
};
};
D3604E6A28F02E3F0066C366 /* XCRemoteSwiftPackageReference "LitecoinKit.Swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/horizontalsystems/LitecoinKit.Swift";
requirement = {
kind = exactVersion;
version = 2.1.0;
version = 2.2.0;
};
};
D3604E6E28F03AC70066C366 /* XCRemoteSwiftPackageReference "MarketKit.Swift" */ = {
Expand Down Expand Up @@ -12343,7 +12343,7 @@
repositoryURL = "https://github.com/horizontalsystems/DashKit.Swift";
requirement = {
kind = exactVersion;
version = 2.0.3;
version = 2.1.0;
};
};
D36E0C2828D084AB00B622B9 /* XCRemoteSwiftPackageReference "CollectionViewCenteredFlowLayout" */ = {
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ open_sea_api_key =
unstoppable_domains_api_key =
one_inch_api_key =
swap_enabled = true
blockchair_api_key =

default_words =
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ open_sea_api_key =
unstoppable_domains_api_key =
one_inch_api_key =
swap_enabled = true
blockchair_api_key =
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ extension BtcBlockchainManager {
storage.btcRestoreMode(blockchainType: blockchainType) ?? .api
}

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

func syncMode(blockchainType: BlockchainType, accountOrigin: AccountOrigin) -> BitcoinCore.SyncMode {
if accountOrigin == .created {
return .newWallet
return apiSyncMode(blockchainType: blockchainType)
}

switch restoreMode(blockchainType: blockchainType) {
case .api: return .api
case .api: return apiSyncMode(blockchainType: blockchainType)
case .blockchain: return .full
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,8 @@ struct AppConfig {
Bundle.main.object(forInfoDictionaryKey: "SwapEnabled") as? String == "true"
}

static var blockchairApiKey: String {
(Bundle.main.object(forInfoDictionaryKey: "BlockchairApiKey") as? String) ?? ""
}

}
12 changes: 7 additions & 5 deletions UnstoppableWallet/UnstoppableWallet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BlockchairApiKey</key>
<string>${blockchair_api_key}</string>
<key>ArbiscanApiKey</key>
<string>${arbiscan_api_key}</string>
<key>BscscanApiKey</key>
Expand Down Expand Up @@ -101,8 +103,8 @@
</array>
<key>OfficeMode</key>
<string>${OfficeMode}</string>
<key>oneInchApiKey</key>
<string>${one_inch_api_key}</string>
<key>oneInchApiKey</key>
<string>${one_inch_api_key}</string>
<key>OpenSeaApiKey</key>
<string>${open_sea_api_key}</string>
<key>OptimismEtherscanApiKey</key>
Expand All @@ -119,11 +121,11 @@
<string>${ShowTestNetSwitcher}</string>
<key>SnowtraceApiKey</key>
<string>${snowtrace_api_key}</string>
<key>SwapEnabled</key>
<string>${swap_enabled}</string>
<key>SwapEnabled</key>
<string>${swap_enabled}</string>
<key>TronGridApiKey</key>
<string>${trongrid_api_key}</string>
<key>TwitterBearerToken</key>
<key>TwitterBearerToken</key>
<string>${twitter_bearer_token}</string>
<key>UIApplicationSceneManifest</key>
<dict>
Expand Down
15 changes: 0 additions & 15 deletions UnstoppableWallet/UnstoppableWallet/Models/BtcRestoreMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,4 @@ enum BtcRestoreMode: String, CaseIterable, Identifiable, Codable {
var id: Self {
self
}

var title: String {
switch self {
case .api: return "API"
case .blockchain: return "sync_mode.from_blockchain".localized
}
}

var description: String {
switch self {
case .api: return "btc_restore_mode.recommended".localized
case .blockchain: return "btc_restore_mode.more_private".localized
}
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import MarketKit
import BitcoinCore

class BtcBlockchainSettingsService {
let blockchain: Blockchain
private let btcBlockchainManager: BtcBlockchainManager

let restoreModes: [BtcSyncModeItem]
let currentRestoreMode: BtcRestoreMode

init(blockchain: Blockchain, btcBlockchainManager: BtcBlockchainManager) {
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)
}
currentRestoreMode = btcBlockchainManager.restoreMode(blockchainType: blockchain.type)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,43 @@ struct BtcBlockchainSettingsView: View {
@ObservedObject var viewModel: BtcBlockchainSettingsViewModel

@Environment(\.presentationMode) private var presentationMode
@State private var infoPresented = false

var body: some View {
ThemeView {
BottomGradientWrapper {
VStack(spacing: .margin24) {
HighlightedTextView(text: "btc_blockchain_settings.restore_source.alert".localized(viewModel.title))

VStack(spacing: 0) {
ListSectionInfoHeader(text: "btc_blockchain_settings.restore_source".localized) {
infoPresented = true
}
.sheet(isPresented: $infoPresented) {
InfoModule.restoreSourceInfo
}
VStack(spacing: .margin32) {
VStack(spacing: .margin32) {
Text("btc_blockchain_settings.restore_source.description".localized)
.themeSubhead2()
.padding(EdgeInsets(top: 0, leading: .margin16, bottom: 0, trailing: .margin16))

ListSection {
ForEach(viewModel.restoreModes) { restoreMode in
ForEach(viewModel.restoreModes, id: \.restoreMode.id) { restoreMode in
ClickableRow(action: {
viewModel.selectedRestoreMode = restoreMode
viewModel.selectedRestoreMode = restoreMode.restoreMode
}) {
switch restoreMode.icon {
case .local(let name): Image(name)
case .remote(let url): WebImage(url: URL(string: url))
.resizable()
.scaledToFit()
.frame(width: .iconSize32, height: .iconSize32)
}

VStack(spacing: 1) {
Text(restoreMode.title).themeBody()
Text(restoreMode.description).themeSubhead2()
}

if restoreMode == viewModel.selectedRestoreMode {
if restoreMode.restoreMode == viewModel.selectedRestoreMode {
Image.checkIcon
}
}
}
}

ListSectionFooter(text: "btc_blockchain_settings.restore_source.description".localized)
}

HighlightedTextView(text: "btc_blockchain_settings.restore_source.alert".localized(viewModel.title))
}
.padding(EdgeInsets(top: .margin12, leading: .margin16, bottom: .margin32, trailing: .margin16))
} bottomContent: {
Expand All @@ -53,7 +55,8 @@ struct BtcBlockchainSettingsView: View {
.disabled(!viewModel.saveEnabled)
}
}
.navigationBarTitle(viewModel.title)
.navigationTitle(viewModel.title)
.navigationBarTitleDisplayMode(.large)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
WebImage(url: URL(string: viewModel.iconUrl))
Expand All @@ -63,7 +66,7 @@ struct BtcBlockchainSettingsView: View {
}

ToolbarItem(placement: .navigationBarTrailing) {
Button("button.cancel".localized) {
Button("button.done".localized) {
presentationMode.wrappedValue.dismiss()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Combine
class BtcBlockchainSettingsViewModel: ObservableObject {
private let service: BtcBlockchainSettingsService

let restoreModes: [BtcRestoreMode] = BtcRestoreMode.allCases
let restoreModes: [BtcRestoreModeViewItem]

@Published var selectedRestoreMode: BtcRestoreMode {
didSet {
Expand All @@ -17,12 +17,34 @@ class BtcBlockchainSettingsViewModel: ObservableObject {
self.service = service

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):
image = .local(name: "blockchair_32")
default:
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
}

return BtcRestoreModeViewItem(
restoreMode: restoreMode.restoreMode,
title: restoreMode.title,
description: description,
icon: image
)
}
}
}

extension BtcBlockchainSettingsViewModel {

var title: String {
service.blockchain.name
}
Expand All @@ -34,5 +56,16 @@ extension BtcBlockchainSettingsViewModel {
func onTapSave() {
service.save(restoreMode: selectedRestoreMode)
}
}

struct BtcRestoreModeViewItem {
let restoreMode: BtcRestoreMode
let title: String
let description: String
let icon: Image

enum Image {
case local(name: String)
case remote(url: String)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct BlockchainSettingsView: View {
}) {
ItemView(
blockchain: item.blockchain,
value: item.restoreMode.title
value: item.title
)
}
}
Expand Down
Loading

0 comments on commit 38fda3a

Please sign in to comment.