@@ -20,16 +20,16 @@ protocol BDKSyncService {
2020 var keyClient : KeyClient { get }
2121 var network : Network { get }
2222 var wallet : Wallet ? { get }
23-
23+
2424 func createWallet( params: String ? ) throws
2525 func loadWallet( ) throws
26- func deleteWallet( ) throws
26+ func deleteWallet( ) throws
2727 func startSync( progress: @escaping SyncScanProgress ) async throws
2828 func startFullScan( progress: @escaping FullScanProgress ) async throws
29-
29+
3030 func updateNetwork( network: Network )
3131 func updateEsploraURL( _ url: String )
32-
32+
3333 func getTransactions( ) throws -> [ CanonicalTx ]
3434 func getBalance( ) throws -> Balance
3535 func sentAndReceived( tx: Transaction ) throws -> SentAndReceivedValues
@@ -47,25 +47,30 @@ extension BDKSyncService {
4747 guard let connection = self . connection else {
4848 throw WalletError . dbNotFound
4949 }
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+ )
5254
5355 try keyClient. saveBackupInfo ( backupInfo)
5456 try keyClient. saveNetwork ( self . network. description)
5557
5658 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+
5964 let wallet = try Wallet (
6065 descriptor: descriptor,
6166 changeDescriptor: changeDescriptor,
6267 network: network,
6368 connection: connection
6469 )
65-
70+
6671 return wallet
6772 }
68-
73+
6974 func buildBackupInfo( params: String ) throws -> BackupInfo {
7075 if isXPub ( params) {
7176 let descriptorPublicKey = try DescriptorPublicKey . fromString ( publicKey: params)
@@ -87,15 +92,15 @@ extension BDKSyncService {
8792 changeDescriptor: changeDescriptor. description
8893 )
8994 }
90-
91- if isDescriptor ( params) { // is a descriptor?
92-
95+
96+ if isDescriptor ( params) { // is a descriptor?
97+
9398 let descriptorStrings = params. components ( separatedBy: " \n " )
9499 . map { $0. split ( separator: " # " ) . first? . trimmingCharacters ( in: . whitespaces) ?? " " }
95100 . filter { !$0. isEmpty }
96101 let descriptor : Descriptor
97102 let changeDescriptor : Descriptor
98-
103+
99104 if descriptorStrings. count == 1 {
100105 let parsedDescriptor = try Descriptor (
101106 descriptor: descriptorStrings [ 0 ] ,
@@ -109,17 +114,20 @@ extension BDKSyncService {
109114 changeDescriptor = singleDescriptors [ 1 ]
110115 } else if descriptorStrings. count == 2 {
111116 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+ )
113121 } else {
114122 throw AppError . generic ( message: " Descriptor parsing failed " )
115123 }
116-
124+
117125 return . init(
118126 descriptor: descriptor. toStringWithSecret ( ) ,
119127 changeDescriptor: changeDescriptor. toStringWithSecret ( )
120128 )
121129 }
122-
130+
123131 let words = !params. isEmpty ? params : Mnemonic ( wordCount: WordCount . words12) . description
124132 guard let mnemonic = try ? Mnemonic . fromString ( mnemonic: words) else {
125133 throw AppError . generic ( message: " Invalid mnemonic " )
@@ -145,34 +153,34 @@ extension BDKSyncService {
145153 changeDescriptor: changeDescriptor. toStringWithSecret ( )
146154 )
147155 }
148-
156+
149157 func deleteWallet( ) throws {
150158 try deleteData ( )
151159 }
152-
160+
153161 func deleteData( ) throws {
154162 do {
155163 try keyClient. deleteAllData ( )
156-
164+
157165 if let bundleID = Bundle . main. bundleIdentifier {
158166 UserDefaults . standard. removePersistentDomain ( forName: bundleID)
159167 }
160-
168+
161169 let walletDataDirectoryURL = URL . walletDataDirectoryURL
162170 if FileManager . default. fileExists ( atPath: walletDataDirectoryURL. path) {
163171 try FileManager . default. removeItem ( at: walletDataDirectoryURL)
164172 }
165-
173+
166174 } catch {
167175 throw AppError . generic ( message: " Failed to remove Keychain data " )
168176 }
169177 }
170-
178+
171179 func loadWalleFromBackup( ) throws -> Wallet {
172180 guard let connection = self . connection else {
173181 throw WalletError . dbNotFound
174182 }
175-
183+
176184 let backupInfo = try keyClient. getBackupInfo ( )
177185 let descriptor = try Descriptor ( descriptor: backupInfo. descriptor, network: self . network)
178186 let changeDescriptor = try Descriptor (
@@ -184,40 +192,40 @@ extension BDKSyncService {
184192 changeDescriptor: changeDescriptor,
185193 connection: connection
186194 )
187-
195+
188196 return wallet
189197 }
190-
198+
191199 func getBalance( ) throws -> Balance {
192200 guard let wallet = self . wallet else { throw WalletError . walletNotFound }
193201 let balance = wallet. balance ( )
194202 return balance
195203 }
196-
204+
197205 func sentAndReceived( tx: Transaction ) throws -> SentAndReceivedValues {
198206 guard let wallet = self . wallet else {
199207 throw WalletError . walletNotFound
200208 }
201209 let values = wallet. sentAndReceived ( tx: tx)
202210 return values
203211 }
204-
212+
205213 func calculateFeeRate( tx: Transaction ) throws -> UInt64 {
206214 guard let wallet = self . wallet else {
207215 throw WalletError . walletNotFound
208216 }
209217 let feeRate = try wallet. calculateFeeRate ( tx: tx)
210218 return feeRate. toSatPerVbCeil ( )
211219 }
212-
220+
213221 func calculateFee( tx: Transaction ) throws -> Amount {
214222 guard let wallet = self . wallet else {
215223 throw WalletError . walletNotFound
216224 }
217225 let fee = try wallet. calculateFee ( tx: tx)
218226 return fee
219227 }
220-
228+
221229 func buildTransaction(
222230 address: String ,
223231 amount: UInt64 ,
@@ -235,15 +243,15 @@ extension BDKSyncService {
235243 . finish ( wallet: wallet)
236244 return txBuilder
237245 }
238-
246+
239247 func listUnspent( ) throws -> [ LocalOutput ] {
240248 guard let wallet = self . wallet else {
241249 throw WalletError . walletNotFound
242250 }
243251 let localOutputs = wallet. listUnspent ( )
244252 return localOutputs
245253 }
246-
254+
247255 func getAddress( ) throws -> String {
248256 guard let wallet = self . wallet else {
249257 throw WalletError . walletNotFound
@@ -255,7 +263,7 @@ extension BDKSyncService {
255263 let _ = try wallet. persist ( connection: connection)
256264 return addressInfo. address. description
257265 }
258-
266+
259267 func getTransactions( ) throws -> [ CanonicalTx ] {
260268 guard let wallet = self . wallet else {
261269 throw WalletError . walletNotFound
@@ -266,25 +274,24 @@ extension BDKSyncService {
266274 }
267275 return sortedTransactions
268276 }
269-
277+
270278 // 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+
278286 // MARK: - Private
279-
287+
280288 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( " )
285291 }
286-
292+
287293 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 " )
289296 }
290297}
0 commit comments