Skip to content

Commit a0708dd

Browse files
committed
Added Kyoto sync
1 parent ebe05e9 commit a0708dd

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

BDKSwiftExampleWallet/Extensions/BDK+Extensions/Connection+Extensions.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import BitcoinDevKit
22
import Foundation
33

44
extension Connection {
5+
static var dataDir: String {
6+
let documentsDirectoryURL = URL.documentsDirectory
7+
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")
8+
return walletDataDirectoryURL.path()
9+
}
10+
511
static func createConnection() throws -> Connection {
612
let documentsDirectoryURL = URL.documentsDirectory
713
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

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

14-
private let service: BDKSyncService = EsploraService()
14+
private let service: BDKSyncService = KyotoService()
1515

1616
private var balance: Balance?
1717
private var connection: Connection?

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoService.swift

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ final class KyotoService: BDKSyncService {
1717
var network: Network
1818
var wallet: Wallet?
1919

20+
private var client: CbfClient?
21+
private var node: CbfNode?
22+
private var connected = false
23+
2024
init(
2125
keyClient: KeyClient = .live,
2226
network: Network = .signet,
@@ -28,11 +32,19 @@ final class KyotoService: BDKSyncService {
2832
}
2933

3034
func createWallet(params: String?) throws {
31-
35+
self.connection = try Connection.createConnection()
36+
self.wallet = try buildWallet(params: params)
3237
}
3338

3439
func loadWallet() throws {
40+
self.connection = try Connection.loadConnection()
41+
let wallet = try loadWalleFromBackup()
42+
self.wallet = wallet
3543

44+
let nodeComponents = try buildNode(from: wallet)
45+
self.client = nodeComponents.client
46+
self.node = nodeComponents.node
47+
startListen()
3648
}
3749

3850
func deleteWallet() throws {
@@ -82,4 +94,66 @@ final class KyotoService: BDKSyncService {
8294
func getAddress() throws -> String {
8395
""
8496
}
97+
98+
// MARK: - Private
99+
100+
private func buildNode(from wallet: Wallet) throws -> CbfComponents {
101+
try CbfBuilder()
102+
.dataDir(dataDir: Connection.dataDir)
103+
.logLevel(logLevel: .debug)
104+
.scanType(scanType: .recovery(fromHeight: 200_000))
105+
.build(wallet: wallet)
106+
}
107+
108+
private func startListen() {
109+
node?.run()
110+
continuallyUpdate()
111+
printLogs()
112+
updateWarn()
113+
}
114+
115+
private func continuallyUpdate() {
116+
Task {
117+
while true {
118+
guard let update = await self.client?.update() else { return }
119+
try self.wallet?.applyUpdate(update: update)
120+
let _ = try self.wallet?.persist(connection: self.connection ?? Connection.loadConnection())
121+
print("######### walletUpdated")
122+
// DispatchQueue.main.async {
123+
// NotificationCenter.default.post(name: .walletUpdated, object: nil)
124+
// }
125+
}
126+
}
127+
}
128+
129+
private func printLogs() {
130+
Task {
131+
while true {
132+
if let log = try? await self.client?.nextLog() {
133+
print("\(log)")
134+
}
135+
}
136+
}
137+
}
138+
139+
private func updateWarn() {
140+
Task {
141+
while true {
142+
if let warn = try? await self.client!.nextWarning() {
143+
switch warn {
144+
case .needConnections:
145+
print("######### connectionsChanged")
146+
self.connected = false
147+
// DispatchQueue.main.async {
148+
// NotificationCenter.default.post(name: .connectionsChanged, object: nil)
149+
// }
150+
default:
151+
#if DEBUG
152+
print(warn)
153+
#endif
154+
}
155+
}
156+
}
157+
}
158+
}
85159
}

0 commit comments

Comments
 (0)