Skip to content

Commit 92ae5f6

Browse files
authored
chore: add migration
1 parent 3c92c49 commit 92ae5f6

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

BDKSwiftExampleWallet/Extensions/FileManager+Extensions.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,27 @@ extension FileManager {
2020
try removeItem(at: fileURL)
2121
}
2222
}
23+
24+
func ensureDirectoryExists(at url: URL) throws {
25+
var isDir: ObjCBool = false
26+
if fileExists(atPath: url.path, isDirectory: &isDir) {
27+
if !isDir.boolValue {
28+
try removeItem(at: url)
29+
}
30+
}
31+
if !fileExists(atPath: url.path) {
32+
try createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
33+
}
34+
}
35+
36+
func removeOldFlatFileIfNeeded(at directoryURL: URL) throws {
37+
let flatFileURL = directoryURL.appendingPathComponent("wallet_data")
38+
var isDir: ObjCBool = false
39+
if fileExists(atPath: flatFileURL.path, isDirectory: &isDir) {
40+
if !isDir.boolValue {
41+
try removeItem(at: flatFileURL)
42+
}
43+
}
44+
}
45+
2346
}

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ private class BDKService {
100100

101101
let documentsDirectoryURL = URL.documentsDirectory
102102
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")
103+
try FileManager.default.ensureDirectoryExists(at: walletDataDirectoryURL)
104+
try FileManager.default.removeOldFlatFileIfNeeded(at: documentsDirectoryURL)
103105
let persistenceBackendPath = walletDataDirectoryURL.appendingPathComponent("wallet.sqlite")
104106
.path
105107
let sqliteStore = try SqliteStore(path: persistenceBackendPath)
@@ -117,6 +119,8 @@ private class BDKService {
117119
private func loadWallet(descriptor: Descriptor, changeDescriptor: Descriptor) throws {
118120
let documentsDirectoryURL = URL.documentsDirectory
119121
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")
122+
try FileManager.default.ensureDirectoryExists(at: walletDataDirectoryURL)
123+
try FileManager.default.removeOldFlatFileIfNeeded(at: documentsDirectoryURL)
120124
let persistenceBackendPath = walletDataDirectoryURL.appendingPathComponent("wallet.sqlite")
121125
.path
122126
let sqliteStore = try SqliteStore(path: persistenceBackendPath)
@@ -187,10 +191,14 @@ private class BDKService {
187191

188192
private func signAndBroadcast(psbt: Psbt) throws {
189193
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
190-
let _ = try wallet.sign(psbt: psbt)
191-
let transaction = try psbt.extractTx()
192-
let client = self.esploraClient
193-
try client.broadcast(transaction: transaction)
194+
let isSigned = try wallet.sign(psbt: psbt)
195+
if isSigned {
196+
let transaction = try psbt.extractTx()
197+
let client = self.esploraClient
198+
try client.broadcast(transaction: transaction)
199+
} else {
200+
throw WalletError.notSigned
201+
}
194202
}
195203

196204
func syncWithInspector(inspector: SyncScriptInspector) async throws {

BDKSwiftExampleWallet/Service/BDK Service/BDKSwiftExampleWalletError.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ enum WalletError: Error {
1111
case walletNotFound
1212
case blockchainConfigNotFound
1313
case dbNotFound
14+
case notSigned
1415
}

0 commit comments

Comments
 (0)