Skip to content

Commit 8af10a8

Browse files
committed
no message
1 parent 91acf93 commit 8af10a8

File tree

5 files changed

+177
-48
lines changed

5 files changed

+177
-48
lines changed

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import Foundation
1111
private class BDKService {
1212
static var shared: BDKService = BDKService()
1313

14-
private let service: BDKSyncService = KyotoService()
14+
// private let service: BDKSyncService = KyotoService()
15+
private let service: BDKSyncService = EsploraService()
1516

1617
private var balance: Balance?
1718
private var connection: Connection?
@@ -121,6 +122,14 @@ private class BDKService {
121122
func fullScanWithInspector(inspector: FullScanScriptInspector) async throws {
122123
try await service.startFullScan(progress: inspector)
123124
}
125+
126+
func syncWithInspector2(progress: @escaping SyncScanProgress) async throws {
127+
try await service.startSync2(progress: progress)
128+
}
129+
130+
func fullScanWithInspector2(progress: @escaping FullScanProgress) async throws {
131+
try await service.startFullScan2(progress: progress)
132+
}
124133

125134
func calculateFee(tx: Transaction) throws -> Amount {
126135
try service.calculateFee(tx: tx)
@@ -156,6 +165,8 @@ struct BDKClient {
156165
let listUnspent: () throws -> [LocalOutput]
157166
let syncWithInspector: (SyncScriptInspector) async throws -> Void
158167
let fullScanWithInspector: (FullScanScriptInspector) async throws -> Void
168+
let syncScanWithSyncScanProgress: (@escaping SyncScanProgress) async throws -> Void
169+
let fullScanWithFullScanProgress: (@escaping FullScanProgress) async throws -> Void
159170
let getAddress: () throws -> String
160171
let send: (String, UInt64, UInt64) throws -> Void
161172
let calculateFee: (Transaction) throws -> Amount
@@ -191,6 +202,12 @@ extension BDKClient {
191202
fullScanWithInspector: { inspector in
192203
try await BDKService.shared.fullScanWithInspector(inspector: inspector)
193204
},
205+
syncScanWithSyncScanProgress: { progress in
206+
try await BDKService.shared.syncWithInspector2(progress: progress)
207+
},
208+
fullScanWithFullScanProgress: { progress in
209+
try await BDKService.shared.fullScanWithInspector2(progress: progress)
210+
},
194211
getAddress: { try BDKService.shared.getAddress() },
195212
send: { (address, amount, feeRate) in
196213
Task {
@@ -246,6 +263,8 @@ extension BDKClient {
246263
},
247264
syncWithInspector: { _ in },
248265
fullScanWithInspector: { _ in },
266+
syncScanWithSyncScanProgress: { _ in },
267+
fullScanWithFullScanProgress: { _ in },
249268
getAddress: { "tb1pd8jmenqpe7rz2mavfdx7uc8pj7vskxv4rl6avxlqsw2u8u7d4gfs97durt" },
250269
send: { _, _, _ in },
251270
calculateFee: { _ in Amount.fromSat(satoshi: UInt64(615)) },

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import BitcoinDevKit
99
import Foundation
1010

11+
typealias FullScanProgress = (UInt64) -> Void
12+
typealias SyncScanProgress = (UInt64, UInt64) -> Void
13+
1114
protocol BDKSyncService {
1215
var connection: Connection? { get }
1316
var keyClient: KeyClient { get }
@@ -20,6 +23,9 @@ protocol BDKSyncService {
2023
func startSync(progress: SyncScriptInspector) async throws
2124
func startFullScan(progress: FullScanScriptInspector) async throws
2225

26+
func startSync2(progress: @escaping SyncScanProgress) async throws
27+
func startFullScan2(progress: @escaping FullScanProgress) async throws
28+
2329
func updateNetwork(network: Network)
2430
func updateEsploraURL(_ url: String)
2531

BDKSwiftExampleWallet/Service/BDKSyncService/EsploraService.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,49 @@ final class EsploraService: BDKSyncService {
7171
let _ = try wallet.persist(connection: connection)
7272
}
7373

74+
func startSync2(progress: @escaping SyncScanProgress) async throws {
75+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
76+
let syncScanInspector = WalletSyncScriptInspector { scripts, total in
77+
progress(scripts, total)
78+
}
79+
let esploraClient = self.esploraClient
80+
let syncRequest = try wallet.startSyncWithRevealedSpks()
81+
.inspectSpks(inspector: syncScanInspector)
82+
.build()
83+
let update = try esploraClient.sync(
84+
request: syncRequest,
85+
parallelRequests: UInt64(5)
86+
)
87+
let _ = try wallet.applyUpdate(update: update)
88+
guard let connection = self.connection else {
89+
throw WalletError.dbNotFound
90+
}
91+
let _ = try wallet.persist(connection: connection)
92+
}
93+
94+
func startFullScan2(progress: @escaping FullScanProgress) async throws {
95+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
96+
let fullScanInspector = WalletFullScanScriptInspector { inspected in
97+
progress(inspected)
98+
}
99+
let esploraClient = esploraClient
100+
let fullScanRequest = try wallet.startFullScan()
101+
.inspectSpksForAllKeychains(inspector: fullScanInspector)
102+
.build()
103+
let update = try esploraClient.fullScan(
104+
request: fullScanRequest,
105+
// using https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#address-gap-limit
106+
stopGap: UInt64(20),
107+
// using https://github.com/bitcoindevkit/bdk/blob/master/example-crates/example_wallet_esplora_blocking/src/main.rs
108+
parallelRequests: UInt64(5)
109+
)
110+
let _ = try wallet.applyUpdate(update: update)
111+
guard let connection = self.connection else {
112+
throw WalletError.dbNotFound
113+
}
114+
let _ = try wallet.persist(connection: connection)
115+
}
116+
74117
func startFullScan(progress: FullScanScriptInspector) async throws {
75118
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
76119
let esploraClient = esploraClient

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoService.swift

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import Foundation
1010

1111
final class KyotoService: BDKSyncService {
1212

13+
private static let nodeHeight: UInt32 = 251_000
14+
1315
static let shared = KyotoService()
1416

1517
var connection: Connection?
@@ -21,6 +23,10 @@ final class KyotoService: BDKSyncService {
2123
private var node: CbfNode?
2224
private var connected = false
2325

26+
private var fullScanProgress: FullScanScriptInspector?
27+
private var fullScanProgress2: FullScanProgress?
28+
private var syncProgress: SyncScriptInspector?
29+
2430
init(
2531
keyClient: KeyClient = .live,
2632
network: Network = .signet,
@@ -42,28 +48,47 @@ final class KyotoService: BDKSyncService {
4248
self.wallet = wallet
4349
}
4450

45-
func startSync(progress: any SyncScriptInspector) async throws {
46-
guard let wallet = self.wallet else {
47-
throw WalletError.walletNotFound
48-
}
49-
let nodeComponents = try buildNode(
50-
from: wallet, scanType: .sync
51-
)
52-
self.client = nodeComponents.client
53-
self.node = nodeComponents.node
54-
startListen()
51+
func startSync(progress: SyncScriptInspector) async throws {
52+
// guard let wallet = self.wallet else {
53+
// throw WalletError.walletNotFound
54+
// }
55+
// let nodeComponents = try buildNode(
56+
// from: wallet, scanType: .sync
57+
// )
58+
// self.syncProgress = progress
59+
// self.client = nodeComponents.client
60+
// self.node = nodeComponents.node
61+
// await startListen()
5562
}
5663

57-
func startFullScan(progress: any FullScanScriptInspector) async throws {
64+
func startFullScan(progress: FullScanScriptInspector) async throws {
65+
// guard let wallet = self.wallet else {
66+
// throw WalletError.walletNotFound
67+
// }
68+
// let nodeComponents = try buildNode(
69+
// from: wallet, scanType: .recovery(fromHeight: 200_000)
70+
// )
71+
// self.fullScanProgress = progress
72+
// self.client = nodeComponents.client
73+
// self.node = nodeComponents.node
74+
// await startListen()
75+
}
76+
77+
func startSync2(progress: @escaping SyncScanProgress) async throws {
78+
79+
}
80+
81+
func startFullScan2(progress: @escaping FullScanProgress) async throws {
5882
guard let wallet = self.wallet else {
5983
throw WalletError.walletNotFound
6084
}
6185
let nodeComponents = try buildNode(
62-
from: wallet, scanType: .recovery(fromHeight: 200_000)
86+
from: wallet, scanType: .recovery(fromHeight: KyotoService.nodeHeight)
6387
)
88+
self.fullScanProgress2 = progress
6489
self.client = nodeComponents.client
6590
self.node = nodeComponents.node
66-
startListen()
91+
try await startListen()
6792
}
6893

6994
func send(address: String, amount: UInt64, feeRate: UInt64) async throws {
@@ -80,14 +105,25 @@ final class KyotoService: BDKSyncService {
80105
.build(wallet: wallet)
81106
}
82107

83-
private func startListen() {
108+
private func startListen() async throws {
84109
node?.run()
85-
continuallyUpdate()
86110
printLogs()
87111
updateWarn()
112+
// await continuallyUpdate()
113+
try await startUpdating()
88114
}
89115

90-
private func continuallyUpdate() {
116+
@discardableResult
117+
func startUpdating() async throws -> Bool {
118+
guard let update = await self.client?.update() else { return false }
119+
try self.wallet?.applyUpdate(update: update)
120+
let _ = try self.wallet?.persist(connection: self.connection ?? Connection.loadConnection())
121+
print("######### walletUpdated")
122+
123+
return true
124+
}
125+
126+
private func continuallyUpdate() async {
91127
Task {
92128
while true {
93129
guard let update = await self.client?.update() else { return }
@@ -106,6 +142,18 @@ final class KyotoService: BDKSyncService {
106142
while true {
107143
if let log = try? await self.client?.nextLog() {
108144
print("\(log)")
145+
switch log {
146+
case .connectionsMet:
147+
print("######### connected")
148+
self.connected = true
149+
case .progress(let progress):
150+
if let fullScanProgress = self.fullScanProgress2 {
151+
let _progress = UInt64(progress * 100.0)
152+
fullScanProgress(_progress)
153+
}
154+
default:
155+
break
156+
}
109157
}
110158
}
111159
}
@@ -117,7 +165,7 @@ final class KyotoService: BDKSyncService {
117165
if let warn = try? await self.client!.nextWarning() {
118166
switch warn {
119167
case .needConnections:
120-
print("######### connectionsChanged")
168+
print("######### disconnected")
121169
self.connected = false
122170
// DispatchQueue.main.async {
123171
// NotificationCenter.default.post(name: .connectionsChanged, object: nil)

BDKSwiftExampleWallet/View Model/WalletViewModel.swift

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,6 @@ class WalletViewModel {
7575
self.walletSyncState = walletSyncState
7676
}
7777

78-
private func fullScanWithProgress() async {
79-
self.walletSyncState = .syncing
80-
do {
81-
let inspector = WalletFullScanScriptInspector(updateProgress: updateProgressFullScan)
82-
try await bdkClient.fullScanWithInspector(inspector)
83-
self.walletSyncState = .synced
84-
} catch let error as CannotConnectError {
85-
self.walletViewError = .generic(message: error.localizedDescription)
86-
self.showingWalletViewErrorAlert = true
87-
} catch let error as EsploraError {
88-
self.walletViewError = .generic(message: error.localizedDescription)
89-
self.showingWalletViewErrorAlert = true
90-
} catch let error as PersistenceError {
91-
self.walletViewError = .generic(message: error.localizedDescription)
92-
self.showingWalletViewErrorAlert = true
93-
} catch {
94-
self.walletSyncState = .error(error)
95-
self.showingWalletViewErrorAlert = true
96-
}
97-
}
98-
9978
func getBalance() {
10079
do {
10180
let balance = try bdkClient.getBalance()
@@ -133,11 +112,28 @@ class WalletViewModel {
133112
}
134113
}
135114

115+
func syncOrFullScan() async {
116+
if bdkClient.needsFullScan() {
117+
await fullScanWithProgress()
118+
bdkClient.setNeedsFullScan(false)
119+
} else {
120+
await startSyncWithProgress()
121+
}
122+
}
123+
136124
private func startSyncWithProgress() async {
137125
self.walletSyncState = .syncing
138126
do {
139-
let inspector = WalletSyncScriptInspector(updateProgress: updateProgress)
140-
try await bdkClient.syncWithInspector(inspector)
127+
// let inspector = WalletSyncScriptInspector(updateProgress: updateProgress)
128+
// try await bdkClient.syncWithInspector(inspector)
129+
130+
try await bdkClient.syncScanWithSyncScanProgress { [weak self] inspected, total in
131+
DispatchQueue.main.async {
132+
self?.totalScripts = total
133+
self?.inspectedScripts = inspected
134+
self?.progress = total > 0 ? Float(inspected) / Float(total) : 0
135+
}
136+
}
141137
self.walletSyncState = .synced
142138
} catch let error as CannotConnectError {
143139
self.walletViewError = .generic(message: error.localizedDescription)
@@ -156,13 +152,30 @@ class WalletViewModel {
156152
self.showingWalletViewErrorAlert = true
157153
}
158154
}
159-
160-
func syncOrFullScan() async {
161-
if bdkClient.needsFullScan() {
162-
await fullScanWithProgress()
163-
bdkClient.setNeedsFullScan(false)
164-
} else {
165-
await startSyncWithProgress()
155+
156+
private func fullScanWithProgress() async {
157+
self.walletSyncState = .syncing
158+
do {
159+
// let inspector = WalletFullScanScriptInspector(updateProgress: updateProgressFullScan)
160+
// try await bdkClient.fullScanWithInspector(inspector)
161+
try await bdkClient.fullScanWithFullScanProgress { [weak self] progress in
162+
DispatchQueue.main.async {
163+
self?.inspectedScripts = progress
164+
}
165+
}
166+
self.walletSyncState = .synced
167+
} catch let error as CannotConnectError {
168+
self.walletViewError = .generic(message: error.localizedDescription)
169+
self.showingWalletViewErrorAlert = true
170+
} catch let error as EsploraError {
171+
self.walletViewError = .generic(message: error.localizedDescription)
172+
self.showingWalletViewErrorAlert = true
173+
} catch let error as PersistenceError {
174+
self.walletViewError = .generic(message: error.localizedDescription)
175+
self.showingWalletViewErrorAlert = true
176+
} catch {
177+
self.walletSyncState = .error(error)
178+
self.showingWalletViewErrorAlert = true
166179
}
167180
}
168181
}

0 commit comments

Comments
 (0)