Skip to content

Commit 0540621

Browse files
committed
swift-format
1 parent 699385a commit 0540621

18 files changed

+132
-124
lines changed

BDKSwiftExampleWallet/Actor/WalletFullScanScriptInspector.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Created by Rubens Machion on 23/04/25.
66
//
77

8-
98
import BitcoinDevKit
109

1110
actor WalletFullScanScriptInspector: @preconcurrency FullScanScriptInspector {

BDKSwiftExampleWallet/App/BDKSwiftExampleWalletApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftUI
1212
struct BDKSwiftExampleWalletApp: App {
1313
@AppStorage("isOnboarding") var isOnboarding: Bool = true
1414
@State private var navigationPath = NavigationPath()
15-
15+
1616
var body: some Scene {
1717
WindowGroup {
1818
NavigationStack(path: $navigationPath) {

BDKSwiftExampleWallet/Extensions/BDK+Extensions/Connection+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension Connection {
77
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")
88
return walletDataDirectoryURL.path()
99
}
10-
10+
1111
static func createConnection() throws -> Connection {
1212
let documentsDirectoryURL = URL.documentsDirectory
1313
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")
@@ -23,7 +23,7 @@ extension Connection {
2323
let connection = try Connection(path: persistenceBackendPath)
2424
return connection
2525
}
26-
26+
2727
static func loadConnection() throws -> Connection {
2828
let persistenceBackendPath = URL.persistenceBackendPath
2929
let connection = try Connection(path: persistenceBackendPath)

BDKSwiftExampleWallet/Extensions/URL+Extensions.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
import Foundation
99

1010
extension URL {
11-
11+
1212
static var defaultWalletDirectory: URL {
1313
URL.documentsDirectory
1414
}
15-
15+
1616
static var walletDirectoryName: String {
1717
"wallet_data"
1818
}
19-
19+
2020
static var walletDBName: String {
2121
"wallet.sqlite"
2222
}
23-
23+
2424
static var walletDataDirectoryURL: URL {
2525
defaultWalletDirectory.appendingPathComponent(walletDirectoryName)
2626
}
27-
27+
2828
static var persistenceBackendPath: String {
2929
walletDataDirectoryURL.appendingPathComponent(walletDBName).path
3030
}

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
private class BDKService {
1212
static var shared: BDKService = BDKService()
13-
13+
1414
private var syncMode: SyncMode?
1515
private var service: BDKSyncService {
1616
switch try? keyClient.getSyncMode() {
@@ -53,14 +53,14 @@ private class BDKService {
5353
try? keyClient.saveEsploraURL(newURL)
5454
}
5555
}
56-
56+
5757
func updateSyncMode(_ mode: SyncMode) {
5858
if syncMode != mode {
5959
self.syncMode = mode
6060
try? keyClient.saveSyncMode(mode)
6161
}
6262
}
63-
63+
6464
func getSyncMode() -> SyncMode? {
6565
try? keyClient.getSyncMode()
6666
}
@@ -123,11 +123,11 @@ private class BDKService {
123123
{
124124
try service.buildTransaction(address: address, amount: amount, feeRate: feeRate)
125125
}
126-
126+
127127
func syncWithInspector(progress: @escaping SyncScanProgress) async throws {
128128
try await service.startSync(progress: progress)
129129
}
130-
130+
131131
func fullScanWithInspector(progress: @escaping FullScanProgress) async throws {
132132
try await service.startFullScan(progress: progress)
133133
}
@@ -143,7 +143,7 @@ private class BDKService {
143143
func sentAndReceived(tx: BitcoinDevKit.Transaction) throws -> SentAndReceivedValues {
144144
try service.sentAndReceived(tx: tx)
145145
}
146-
146+
147147
func stop() async throws {
148148
try await service.stopService()
149149
}
@@ -304,7 +304,7 @@ extension BDKClient {
304304
getEsploraURL: { Constants.Config.EsploraServerURLNetwork.Signet.mutiny },
305305
updateNetwork: { _ in },
306306
updateEsploraURL: { _ in },
307-
stop: { },
307+
stop: {},
308308
upateSyncMode: { _ in },
309309
getSyncMode: { .esplora }
310310
)

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)