|
| 1 | +// |
| 2 | +// CbfClient+Extensions.swift |
| 3 | +// BDKSwiftExampleWallet |
| 4 | +// |
| 5 | +// Created by Matthew Ramsden on 7/30/25. |
| 6 | +// |
| 7 | + |
| 8 | +import BitcoinDevKit |
| 9 | +import Foundation |
| 10 | + |
| 11 | +extension CbfClient { |
| 12 | + static func createComponents(wallet: Wallet) -> (client: CbfClient, node: CbfNode) { |
| 13 | + do { |
| 14 | + let components = try CbfBuilder() |
| 15 | + .logLevel(logLevel: .debug) |
| 16 | + .scanType(scanType: .sync) |
| 17 | + .dataDir(dataDir: Constants.Config.Kyoto.dbPath) |
| 18 | + .peers(peers: Constants.Networks.Signet.Regular.kyotoPeers) |
| 19 | + .build(wallet: wallet) |
| 20 | + Task { |
| 21 | + do { |
| 22 | + try await components.node.run() |
| 23 | + } catch { |
| 24 | + // Kyoto: Failed to start node |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + components.client.startBackgroundMonitoring() |
| 29 | + |
| 30 | + return (client: components.client, node: components.node) |
| 31 | + } catch { |
| 32 | + fatalError("Failed to create CBF components: \(error)") |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + func startBackgroundMonitoring() { |
| 37 | + Task { |
| 38 | + var isConnected = false |
| 39 | + while true { |
| 40 | + if let log = try? await self.nextLog() { |
| 41 | + // Parse specific sync stage messages |
| 42 | + if log.contains("Attempting to load headers from the database") { |
| 43 | + await MainActor.run { |
| 44 | + NotificationCenter.default.post( |
| 45 | + name: NSNotification.Name("KyotoProgressUpdate"), |
| 46 | + object: nil, |
| 47 | + userInfo: ["progress": Float(0.2)] |
| 48 | + ) |
| 49 | + } |
| 50 | + } else if log.contains("]: headers") { |
| 51 | + await MainActor.run { |
| 52 | + NotificationCenter.default.post( |
| 53 | + name: NSNotification.Name("KyotoProgressUpdate"), |
| 54 | + object: nil, |
| 55 | + userInfo: ["progress": Float(0.4)] |
| 56 | + ) |
| 57 | + } |
| 58 | + } else if log.contains("Chain updated") { |
| 59 | + let components = log.components(separatedBy: " ") |
| 60 | + if components.count >= 4, |
| 61 | + components[0] == "Chain" && components[1] == "updated", |
| 62 | + let height = UInt32(components[2]) |
| 63 | + { |
| 64 | + await MainActor.run { |
| 65 | + NotificationCenter.default.post( |
| 66 | + name: NSNotification.Name("KyotoChainHeightUpdate"), |
| 67 | + object: nil, |
| 68 | + userInfo: ["height": height] |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if !isConnected { |
| 74 | + isConnected = true |
| 75 | + await MainActor.run { |
| 76 | + NotificationCenter.default.post( |
| 77 | + name: NSNotification.Name("KyotoConnectionUpdate"), |
| 78 | + object: nil, |
| 79 | + userInfo: ["connected": true] |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if log.contains("Established an encrypted connection") && !isConnected { |
| 86 | + isConnected = true |
| 87 | + await MainActor.run { |
| 88 | + NotificationCenter.default.post( |
| 89 | + name: NSNotification.Name("KyotoConnectionUpdate"), |
| 90 | + object: nil, |
| 91 | + userInfo: ["connected": true] |
| 92 | + ) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if log.contains("Need connections") && isConnected { |
| 97 | + isConnected = false |
| 98 | + await MainActor.run { |
| 99 | + NotificationCenter.default.post( |
| 100 | + name: NSNotification.Name("KyotoConnectionUpdate"), |
| 101 | + object: nil, |
| 102 | + userInfo: ["connected": false] |
| 103 | + ) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + try? await Task.sleep(nanoseconds: 100_000_000) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + Task { |
| 112 | + var hasEstablishedConnection = false |
| 113 | + while true { |
| 114 | + if let info = try? await self.nextInfo() { |
| 115 | + switch info { |
| 116 | + case let .progress(progress): |
| 117 | + await MainActor.run { |
| 118 | + NotificationCenter.default.post( |
| 119 | + name: NSNotification.Name("KyotoProgressUpdate"), |
| 120 | + object: nil, |
| 121 | + userInfo: ["progress": progress] |
| 122 | + ) |
| 123 | + } |
| 124 | + case let .newChainHeight(height): |
| 125 | + await MainActor.run { |
| 126 | + NotificationCenter.default.post( |
| 127 | + name: NSNotification.Name("KyotoChainHeightUpdate"), |
| 128 | + object: nil, |
| 129 | + userInfo: ["height": height] |
| 130 | + ) |
| 131 | + |
| 132 | + if !hasEstablishedConnection { |
| 133 | + hasEstablishedConnection = true |
| 134 | + NotificationCenter.default.post( |
| 135 | + name: NSNotification.Name("KyotoConnectionUpdate"), |
| 136 | + object: nil, |
| 137 | + userInfo: ["connected": true] |
| 138 | + ) |
| 139 | + } |
| 140 | + } |
| 141 | + case .connectionsMet: |
| 142 | + await MainActor.run { |
| 143 | + hasEstablishedConnection = true |
| 144 | + NotificationCenter.default.post( |
| 145 | + name: NSNotification.Name("KyotoConnectionUpdate"), |
| 146 | + object: nil, |
| 147 | + userInfo: ["connected": true] |
| 148 | + ) |
| 149 | + } |
| 150 | + default: |
| 151 | + break |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + Task { |
| 158 | + while true { |
| 159 | + if let warning = try? await self.nextWarning() { |
| 160 | + switch warning { |
| 161 | + case .needConnections: |
| 162 | + await MainActor.run { |
| 163 | + NotificationCenter.default.post( |
| 164 | + name: NSNotification.Name("KyotoConnectionUpdate"), |
| 165 | + object: nil, |
| 166 | + userInfo: ["connected": false] |
| 167 | + ) |
| 168 | + } |
| 169 | + default: |
| 170 | + break |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments