@@ -20,16 +20,16 @@ protocol BDKSyncService {
20
20
var keyClient : KeyClient { get }
21
21
var network : Network { get }
22
22
var wallet : Wallet ? { get }
23
-
23
+
24
24
func createWallet( params: String ? ) throws
25
25
func loadWallet( ) throws
26
- func deleteWallet( ) throws
26
+ func deleteWallet( ) throws
27
27
func startSync( progress: @escaping SyncScanProgress ) async throws
28
28
func startFullScan( progress: @escaping FullScanProgress ) async throws
29
-
29
+
30
30
func updateNetwork( network: Network )
31
31
func updateEsploraURL( _ url: String )
32
-
32
+
33
33
func getTransactions( ) throws -> [ CanonicalTx ]
34
34
func getBalance( ) throws -> Balance
35
35
func sentAndReceived( tx: Transaction ) throws -> SentAndReceivedValues
@@ -47,25 +47,30 @@ extension BDKSyncService {
47
47
guard let connection = self . connection else {
48
48
throw WalletError . dbNotFound
49
49
}
50
-
51
- let backupInfo = try buildBackupInfo ( params: params ?? Mnemonic ( wordCount: WordCount . words12) . description)
50
+
51
+ let backupInfo = try buildBackupInfo (
52
+ params: params ?? Mnemonic ( wordCount: WordCount . words12) . description
53
+ )
52
54
53
55
try keyClient. saveBackupInfo ( backupInfo)
54
56
try keyClient. saveNetwork ( self . network. description)
55
57
56
58
let descriptor = try Descriptor ( descriptor: backupInfo. descriptor, network: network)
57
- let changeDescriptor = try Descriptor ( descriptor: backupInfo. changeDescriptor, network: network)
58
-
59
+ let changeDescriptor = try Descriptor (
60
+ descriptor: backupInfo. changeDescriptor,
61
+ network: network
62
+ )
63
+
59
64
let wallet = try Wallet (
60
65
descriptor: descriptor,
61
66
changeDescriptor: changeDescriptor,
62
67
network: network,
63
68
connection: connection
64
69
)
65
-
70
+
66
71
return wallet
67
72
}
68
-
73
+
69
74
func buildBackupInfo( params: String ) throws -> BackupInfo {
70
75
if isXPub ( params) {
71
76
let descriptorPublicKey = try DescriptorPublicKey . fromString ( publicKey: params)
@@ -87,15 +92,15 @@ extension BDKSyncService {
87
92
changeDescriptor: changeDescriptor. description
88
93
)
89
94
}
90
-
91
- if isDescriptor ( params) { // is a descriptor?
92
-
95
+
96
+ if isDescriptor ( params) { // is a descriptor?
97
+
93
98
let descriptorStrings = params. components ( separatedBy: " \n " )
94
99
. map { $0. split ( separator: " # " ) . first? . trimmingCharacters ( in: . whitespaces) ?? " " }
95
100
. filter { !$0. isEmpty }
96
101
let descriptor : Descriptor
97
102
let changeDescriptor : Descriptor
98
-
103
+
99
104
if descriptorStrings. count == 1 {
100
105
let parsedDescriptor = try Descriptor (
101
106
descriptor: descriptorStrings [ 0 ] ,
@@ -109,17 +114,20 @@ extension BDKSyncService {
109
114
changeDescriptor = singleDescriptors [ 1 ]
110
115
} else if descriptorStrings. count == 2 {
111
116
descriptor = try Descriptor ( descriptor: descriptorStrings [ 0 ] , network: network)
112
- changeDescriptor = try Descriptor ( descriptor: descriptorStrings [ 1 ] , network: network)
117
+ changeDescriptor = try Descriptor (
118
+ descriptor: descriptorStrings [ 1 ] ,
119
+ network: network
120
+ )
113
121
} else {
114
122
throw AppError . generic ( message: " Descriptor parsing failed " )
115
123
}
116
-
124
+
117
125
return . init(
118
126
descriptor: descriptor. toStringWithSecret ( ) ,
119
127
changeDescriptor: changeDescriptor. toStringWithSecret ( )
120
128
)
121
129
}
122
-
130
+
123
131
let words = !params. isEmpty ? params : Mnemonic ( wordCount: WordCount . words12) . description
124
132
guard let mnemonic = try ? Mnemonic . fromString ( mnemonic: words) else {
125
133
throw AppError . generic ( message: " Invalid mnemonic " )
@@ -145,34 +153,34 @@ extension BDKSyncService {
145
153
changeDescriptor: changeDescriptor. toStringWithSecret ( )
146
154
)
147
155
}
148
-
156
+
149
157
func deleteWallet( ) throws {
150
158
try deleteData ( )
151
159
}
152
-
160
+
153
161
func deleteData( ) throws {
154
162
do {
155
163
try keyClient. deleteAllData ( )
156
-
164
+
157
165
if let bundleID = Bundle . main. bundleIdentifier {
158
166
UserDefaults . standard. removePersistentDomain ( forName: bundleID)
159
167
}
160
-
168
+
161
169
let walletDataDirectoryURL = URL . walletDataDirectoryURL
162
170
if FileManager . default. fileExists ( atPath: walletDataDirectoryURL. path) {
163
171
try FileManager . default. removeItem ( at: walletDataDirectoryURL)
164
172
}
165
-
173
+
166
174
} catch {
167
175
throw AppError . generic ( message: " Failed to remove Keychain data " )
168
176
}
169
177
}
170
-
178
+
171
179
func loadWalleFromBackup( ) throws -> Wallet {
172
180
guard let connection = self . connection else {
173
181
throw WalletError . dbNotFound
174
182
}
175
-
183
+
176
184
let backupInfo = try keyClient. getBackupInfo ( )
177
185
let descriptor = try Descriptor ( descriptor: backupInfo. descriptor, network: self . network)
178
186
let changeDescriptor = try Descriptor (
@@ -184,40 +192,40 @@ extension BDKSyncService {
184
192
changeDescriptor: changeDescriptor,
185
193
connection: connection
186
194
)
187
-
195
+
188
196
return wallet
189
197
}
190
-
198
+
191
199
func getBalance( ) throws -> Balance {
192
200
guard let wallet = self . wallet else { throw WalletError . walletNotFound }
193
201
let balance = wallet. balance ( )
194
202
return balance
195
203
}
196
-
204
+
197
205
func sentAndReceived( tx: Transaction ) throws -> SentAndReceivedValues {
198
206
guard let wallet = self . wallet else {
199
207
throw WalletError . walletNotFound
200
208
}
201
209
let values = wallet. sentAndReceived ( tx: tx)
202
210
return values
203
211
}
204
-
212
+
205
213
func calculateFeeRate( tx: Transaction ) throws -> UInt64 {
206
214
guard let wallet = self . wallet else {
207
215
throw WalletError . walletNotFound
208
216
}
209
217
let feeRate = try wallet. calculateFeeRate ( tx: tx)
210
218
return feeRate. toSatPerVbCeil ( )
211
219
}
212
-
220
+
213
221
func calculateFee( tx: Transaction ) throws -> Amount {
214
222
guard let wallet = self . wallet else {
215
223
throw WalletError . walletNotFound
216
224
}
217
225
let fee = try wallet. calculateFee ( tx: tx)
218
226
return fee
219
227
}
220
-
228
+
221
229
func buildTransaction(
222
230
address: String ,
223
231
amount: UInt64 ,
@@ -235,15 +243,15 @@ extension BDKSyncService {
235
243
. finish ( wallet: wallet)
236
244
return txBuilder
237
245
}
238
-
246
+
239
247
func listUnspent( ) throws -> [ LocalOutput ] {
240
248
guard let wallet = self . wallet else {
241
249
throw WalletError . walletNotFound
242
250
}
243
251
let localOutputs = wallet. listUnspent ( )
244
252
return localOutputs
245
253
}
246
-
254
+
247
255
func getAddress( ) throws -> String {
248
256
guard let wallet = self . wallet else {
249
257
throw WalletError . walletNotFound
@@ -255,7 +263,7 @@ extension BDKSyncService {
255
263
let _ = try wallet. persist ( connection: connection)
256
264
return addressInfo. address. description
257
265
}
258
-
266
+
259
267
func getTransactions( ) throws -> [ CanonicalTx ] {
260
268
guard let wallet = self . wallet else {
261
269
throw WalletError . walletNotFound
@@ -266,25 +274,24 @@ extension BDKSyncService {
266
274
}
267
275
return sortedTransactions
268
276
}
269
-
277
+
270
278
// MARK: - Optionals methods
271
-
272
- func updateEsploraURL( _ url: String ) { }
273
-
274
- func updateNetwork( network: Network ) { }
275
-
276
- func stopService( ) async throws { }
277
-
279
+
280
+ func updateEsploraURL( _ url: String ) { }
281
+
282
+ func updateNetwork( network: Network ) { }
283
+
284
+ func stopService( ) async throws { }
285
+
278
286
// MARK: - Private
279
-
287
+
280
288
private func isDescriptor( _ param: String ) -> Bool {
281
- param. hasPrefix ( " tr( " ) ||
282
- param. hasPrefix ( " wpkh( " ) ||
283
- param. hasPrefix ( " wsh( " ) ||
284
- param. hasPrefix ( " sh( " )
289
+ param. hasPrefix ( " tr( " ) || param. hasPrefix ( " wpkh( " ) || param. hasPrefix ( " wsh( " )
290
+ || param. hasPrefix ( " sh( " )
285
291
}
286
-
292
+
287
293
private func isXPub( _ param: String ) -> Bool {
288
- param. hasPrefix ( " xpub " ) || param. hasPrefix ( " tpub " ) || param. hasPrefix ( " vpub " ) || param. hasPrefix ( " zpub " )
294
+ param. hasPrefix ( " xpub " ) || param. hasPrefix ( " tpub " ) || param. hasPrefix ( " vpub " )
295
+ || param. hasPrefix ( " zpub " )
289
296
}
290
297
}
0 commit comments