|
1 | 1 | import BitkitCore |
2 | 2 | import Foundation |
3 | 3 |
|
| 4 | +/// Watcher-related service calls, extracted as a protocol so unit tests can |
| 5 | +/// substitute a mock (mirrors bitkit-android's mocked TrezorRepo in TrezorViewModelTest). |
| 6 | +protocol TrezorWatcherServicing { |
| 7 | + func startWatcher(params: WatcherParams, listener: EventListener) async throws |
| 8 | + func stopWatcher(watcherId: String) throws |
| 9 | + func stopAllWatchers() |
| 10 | +} |
| 11 | + |
| 12 | +extension TrezorService: TrezorWatcherServicing {} |
| 13 | + |
4 | 14 | /// Service layer wrapper for Trezor FFI functions |
5 | 15 | /// All operations run on ServiceQueue.background(.core) to ensure thread safety |
6 | 16 | class TrezorService { |
@@ -65,13 +75,17 @@ class TrezorService { |
65 | 75 |
|
66 | 76 | // MARK: - Connection Management |
67 | 77 |
|
68 | | - /// Connect to a Trezor device by its ID |
69 | | - /// - Parameter deviceId: The device identifier (path) |
| 78 | + /// Connect to a Trezor device by its ID, opening the wallet given by `selection`. |
| 79 | + /// On THP devices (Safe 5/7) the passphrase is bound to the session at creation, so |
| 80 | + /// it is supplied per-connect rather than cached between calls. |
| 81 | + /// - Parameters: |
| 82 | + /// - deviceId: The device identifier (path) |
| 83 | + /// - selection: Which wallet to open (standard / hidden / on-device passphrase) |
70 | 84 | /// - Returns: Device features after successful connection |
71 | | - func connect(deviceId: String) async throws -> TrezorFeatures { |
| 85 | + func connect(deviceId: String, selection: WalletSelection) async throws -> TrezorFeatures { |
72 | 86 | try await ServiceQueue.background(.core) { [self] in |
73 | 87 | ensureCallbacksRegistered() |
74 | | - return try await trezorConnect(deviceId: deviceId, selection: .standard) |
| 88 | + return try await trezorConnect(deviceId: deviceId, selection: selection) |
75 | 89 | } |
76 | 90 | } |
77 | 91 |
|
@@ -268,6 +282,27 @@ class TrezorService { |
268 | 282 | } |
269 | 283 | } |
270 | 284 |
|
| 285 | + // MARK: - Event Watcher (No Device Required) |
| 286 | + |
| 287 | + /// Start watching an extended public key for on-chain transaction activity. |
| 288 | + /// Events are delivered to `listener` until the watcher is stopped. |
| 289 | + /// Does NOT require a connected Trezor device — it subscribes to Electrum directly. |
| 290 | + func startWatcher(params: WatcherParams, listener: EventListener) async throws { |
| 291 | + try await ServiceQueue.background(.core) { |
| 292 | + try await onchainStartWatcher(params: params, listener: listener) |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + /// Stop a specific watcher by its id. |
| 297 | + func stopWatcher(watcherId: String) throws { |
| 298 | + try onchainStopWatcher(watcherId: watcherId) |
| 299 | + } |
| 300 | + |
| 301 | + /// Stop all active watchers. |
| 302 | + func stopAllWatchers() { |
| 303 | + onchainStopAllWatchers() |
| 304 | + } |
| 305 | + |
271 | 306 | // MARK: - Helpers |
272 | 307 |
|
273 | 308 | /// Convert TrezorCoinType to the Network enum used by onchain FFI functions |
|
0 commit comments