@@ -10,7 +10,7 @@ import Foundation
10
10
11
11
final class KyotoService: BDKSyncService {
12
12
13
- private static let nodeHeight : UInt32 = 300_000
13
+ private static let nodeHeight : UInt32 = 200_000
14
14
15
15
static let shared = KyotoService ( )
16
16
@@ -22,6 +22,7 @@ final class KyotoService: BDKSyncService {
22
22
private var client : CbfClient ?
23
23
private var node : CbfNode ?
24
24
private var connected = false
25
+ private var isScanRunning = false
25
26
26
27
private var fullScanProgress : FullScanProgress ?
27
28
private var syncProgress : SyncScanProgress ?
@@ -48,6 +49,7 @@ final class KyotoService: BDKSyncService {
48
49
}
49
50
50
51
func startSync( progress: @escaping SyncScanProgress ) async throws {
52
+ if isScanRunning { return }
51
53
guard let wallet = self . wallet else {
52
54
throw WalletError . walletNotFound
53
55
}
@@ -57,10 +59,12 @@ final class KyotoService: BDKSyncService {
57
59
self . syncProgress = progress
58
60
self . client = nodeComponents. client
59
61
self . node = nodeComponents. node
62
+ isScanRunning = true
60
63
try await startListen ( )
61
64
}
62
65
63
66
func startFullScan( progress: @escaping FullScanProgress ) async throws {
67
+ if isScanRunning { return }
64
68
guard let wallet = self . wallet else {
65
69
throw WalletError . walletNotFound
66
70
}
@@ -70,19 +74,37 @@ final class KyotoService: BDKSyncService {
70
74
self . fullScanProgress = progress
71
75
self . client = nodeComponents. client
72
76
self . node = nodeComponents. node
77
+ isScanRunning = true
73
78
try await startListen ( )
74
79
}
75
80
76
81
func send( address: String , amount: UInt64 , feeRate: UInt64 ) async throws {
77
-
82
+ let psbt = try buildTransaction (
83
+ address: address,
84
+ amount: amount,
85
+ feeRate: feeRate
86
+ )
87
+ try await signAndBroadcast ( psbt: psbt)
78
88
}
79
89
80
90
func stopService( ) async throws {
91
+ isScanRunning = false
81
92
try await client? . shutdown ( )
82
93
}
83
94
84
95
// MARK: - Private
85
96
97
+ private func signAndBroadcast( psbt: Psbt ) async throws {
98
+ guard let wallet = self . wallet else { throw WalletError . walletNotFound }
99
+ let isSigned = try wallet. sign ( psbt: psbt)
100
+ if isSigned {
101
+ let transaction = try psbt. extractTx ( )
102
+ try await client? . broadcast ( transaction: transaction)
103
+ } else {
104
+ throw WalletError . notSigned
105
+ }
106
+ }
107
+
86
108
private func buildNode( from wallet: Wallet , scanType: ScanType ) throws -> CbfComponents {
87
109
try CbfBuilder ( )
88
110
. dataDir ( dataDir: Connection . dataDir)
@@ -100,11 +122,14 @@ final class KyotoService: BDKSyncService {
100
122
101
123
@discardableResult
102
124
func startUpdating( ) async throws -> Bool {
103
- guard let update = await self . client? . update ( ) else { return false }
125
+ guard let update = await self . client? . update ( ) else {
126
+ isScanRunning = false
127
+ return false
128
+ }
104
129
try self . wallet? . applyUpdate ( update: update)
105
130
let _ = try self . wallet? . persist ( connection: self . connection ?? Connection . loadConnection ( ) )
106
131
print ( " ######### walletUpdated " )
107
-
132
+ isScanRunning = false
108
133
return true
109
134
}
110
135
@@ -133,7 +158,7 @@ final class KyotoService: BDKSyncService {
133
158
private func updateWarn( ) {
134
159
Task {
135
160
while true {
136
- if let warn = try ? await self . client! . nextWarning ( ) {
161
+ if let warn = try ? await self . client? . nextWarning ( ) {
137
162
switch warn {
138
163
case . needConnections:
139
164
print ( " ######### disconnected " )
0 commit comments