Skip to content

Commit f0d31c7

Browse files
committed
added syncronization
1 parent 5302fe3 commit f0d31c7

7 files changed

Lines changed: 66 additions & 69 deletions

File tree

BDKSwiftExampleWallet/App/BDKSwiftExampleWalletApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct BDKSwiftExampleWalletApp: App {
1717
WindowGroup {
1818
NavigationStack(path: $navigationPath) {
1919
let keyClient = KeyClient.live
20-
var syncService: BDKSyncService = EsploraServerSyncService(
20+
let syncService: BDKSyncService = EsploraServerSyncService(
2121
keyClient: keyClient,
2222
network: .bitcoin
2323
)

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ protocol BDKSyncService {
1818
func createWallet(params: String?) throws
1919
func loadWallet() throws
2020
func deleteWallet() throws
21+
func startSync(progress: SyncScriptInspector) async throws
22+
func startFullScan(progress: FullScanScriptInspector) async throws
2123

2224
func updateNetwork(network: Network)
2325
func updateEsploraURL(_ url: String)

BDKSwiftExampleWallet/Service/BDKSyncService/EsploraServerSyncService.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class EsploraServerSyncService: BDKSyncService {
2323
network: Network = .signet,
2424
connection: Connection? = nil
2525
) {
26-
self.connection = connection
26+
self.connection = connection// ?? (try? Connection.createConnection())
2727
self.keyClient = keyClient
2828
self.network = network
2929

@@ -55,4 +55,41 @@ final class EsploraServerSyncService: BDKSyncService {
5555
try? keyClient.saveEsploraURL(url)
5656
self.esploraClient = .init(url: url)
5757
}
58+
59+
func startSync(progress: SyncScriptInspector) async throws {
60+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
61+
let esploraClient = self.esploraClient
62+
let syncRequest = try wallet.startSyncWithRevealedSpks()
63+
.inspectSpks(inspector: progress)
64+
.build()
65+
let update = try esploraClient.sync(
66+
request: syncRequest,
67+
parallelRequests: UInt64(5)
68+
)
69+
let _ = try wallet.applyUpdate(update: update)
70+
guard let connection = self.connection else {
71+
throw WalletError.dbNotFound
72+
}
73+
let _ = try wallet.persist(connection: connection)
74+
}
75+
76+
func startFullScan(progress: FullScanScriptInspector) async throws {
77+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
78+
let esploraClient = esploraClient
79+
let fullScanRequest = try wallet.startFullScan()
80+
.inspectSpksForAllKeychains(inspector: progress)
81+
.build()
82+
let update = try esploraClient.fullScan(
83+
request: fullScanRequest,
84+
// using https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#address-gap-limit
85+
stopGap: UInt64(20),
86+
// using https://github.com/bitcoindevkit/bdk/blob/master/example-crates/example_wallet_esplora_blocking/src/main.rs
87+
parallelRequests: UInt64(5)
88+
)
89+
let _ = try wallet.applyUpdate(update: update)
90+
guard let connection = self.connection else {
91+
throw WalletError.dbNotFound
92+
}
93+
let _ = try wallet.persist(connection: connection)
94+
}
5895
}
Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,5 @@
1-
////
2-
//// KyotoSyncService.swift
3-
//// BDKSwiftExampleWallet
4-
////
5-
//// Created by Rubens Machion on 16/05/25.
6-
////
71
//
8-
//import BitcoinDevKit
9-
//import Foundation
10-
//
11-
//final class KyotoSyncService: BDKSyncService {
12-
//
13-
// var connection: Connection?
14-
// var keyClient: KeyClient
15-
// var network: Network
16-
// var wallet: Wallet?
17-
// var needsFullScan = false
18-
//
19-
// private var node: CbfNode?
20-
// private var client: CbfClient?
21-
//
22-
// init(
23-
// keyClient: KeyClient = .live,
24-
// network: Network = .signet,
25-
// connection: Connection? = nil
26-
// ) {
27-
// self.connection = connection
28-
// self.keyClient = keyClient
29-
// self.network = network
30-
// }
31-
//
32-
// func createWallet(params: String?) throws {
33-
// self.wallet = try buildWallet(params: params)
34-
// }
35-
//
36-
// func loadWallet() throws {
37-
//
38-
// }
39-
//
40-
// func deleteWallet() throws {
41-
//
42-
// }
43-
//
44-
// func updateNetwork(network: Network) {
45-
// self.network = network
46-
// }
47-
//}
48-
49-
//
50-
// Untitled.swift
2+
// KyotoSyncService.swift
513
// BDKSwiftExampleWallet
524
//
535
// Created by Rubens Machion on 16/05/25.
@@ -64,7 +16,8 @@ final class KyotoSyncService: BDKSyncService {
6416
var wallet: Wallet?
6517
var needsFullScan = false
6618

67-
private var esploraClient: EsploraClient
19+
private var node: CbfNode?
20+
private var client: CbfClient?
6821

6922
init(
7023
keyClient: KeyClient = .live,
@@ -74,33 +27,29 @@ final class KyotoSyncService: BDKSyncService {
7427
self.connection = connection
7528
self.keyClient = keyClient
7629
self.network = network
77-
78-
let url = (try? keyClient.getEsploraURL()) ?? network.url
79-
self.esploraClient = .init(
80-
url: url
81-
)
8230
}
8331

8432
func createWallet(params: String?) throws {
8533
self.wallet = try buildWallet(params: params)
8634
}
8735

8836
func loadWallet() throws {
89-
let wallet = try loadWalleFromBackup()
90-
self.wallet = wallet
37+
9138
}
9239

9340
func deleteWallet() throws {
94-
try deleteData()
95-
needsFullScan = true
41+
9642
}
9743

9844
func updateNetwork(network: Network) {
9945
self.network = network
10046
}
10147

102-
func updateEsploraURL(_ url: String) {
103-
try? keyClient.saveEsploraURL(url)
104-
self.esploraClient = .init(url: url)
48+
func startSync(progress: SyncScriptInspector) async throws {
49+
50+
}
51+
52+
func startFullScan(progress: FullScanScriptInspector) async throws {
53+
10554
}
10655
}

BDKSwiftExampleWallet/View Model/HomeViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Foundation
1313
class HomeViewModel: ObservableObject {
1414
let bdkClient: BDKClient
1515

16-
private let bdkSyncService: BDKSyncService
16+
let bdkSyncService: BDKSyncService
1717

1818
var homeViewError: AppError?
1919
var isWalletLoaded = false

BDKSwiftExampleWallet/View Model/WalletViewModel.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,31 @@ class WalletViewModel {
6161
}
6262
}
6363

64+
private let bdkSyncService: BDKSyncService
65+
6466
init(
6567
bdkClient: BDKClient = .live,
6668
keyClient: KeyClient = .live,
6769
priceClient: PriceClient = .live,
6870
transactions: [CanonicalTx] = [],
69-
walletSyncState: WalletSyncState = .notStarted
71+
walletSyncState: WalletSyncState = .notStarted,
72+
bdkSyncService: BDKSyncService
7073
) {
7174
self.bdkClient = bdkClient
7275
self.keyClient = keyClient
7376
self.priceClient = priceClient
7477
self.transactions = transactions
7578
self.walletSyncState = walletSyncState
79+
self.bdkSyncService = bdkSyncService
7680
}
7781

7882
private func fullScanWithProgress() async {
7983
self.walletSyncState = .syncing
8084
do {
8185
let inspector = WalletFullScanScriptInspector(updateProgress: updateProgressFullScan)
82-
try await bdkClient.fullScanWithInspector(inspector)
86+
try await bdkSyncService.startFullScan(progress: inspector)
87+
88+
// try await bdkClient.fullScanWithInspector(inspector)
8389
self.walletSyncState = .synced
8490
} catch let error as CannotConnectError {
8591
self.walletViewError = .generic(message: error.localizedDescription)
@@ -137,7 +143,9 @@ class WalletViewModel {
137143
self.walletSyncState = .syncing
138144
do {
139145
let inspector = WalletSyncScriptInspector(updateProgress: updateProgress)
140-
try await bdkClient.syncWithInspector(inspector)
146+
try await bdkSyncService.startSync(progress: inspector)
147+
148+
// try await bdkClient.syncWithInspector(inspector)
141149
self.walletSyncState = .synced
142150
} catch let error as CannotConnectError {
143151
self.walletViewError = .generic(message: error.localizedDescription)

BDKSwiftExampleWallet/View/HomeView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ struct HomeView: View {
1818
WalletView(
1919
viewModel: .init(
2020
bdkClient: .live,
21-
priceClient: .live
21+
priceClient: .live,
22+
bdkSyncService: viewModel.bdkSyncService
2223
),
2324
sendNavigationPath: $navigationPath
2425
)

0 commit comments

Comments
 (0)