Skip to content

chore: add migration #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions BDKSwiftExampleWallet/Extensions/FileManager+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,27 @@ extension FileManager {
try removeItem(at: fileURL)
}
}

func ensureDirectoryExists(at url: URL) throws {
var isDir: ObjCBool = false
if fileExists(atPath: url.path, isDirectory: &isDir) {
if !isDir.boolValue {
try removeItem(at: url)
}
}
if !fileExists(atPath: url.path) {
try createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
}
}

func removeOldFlatFileIfNeeded(at directoryURL: URL) throws {
let flatFileURL = directoryURL.appendingPathComponent("wallet_data")
var isDir: ObjCBool = false
if fileExists(atPath: flatFileURL.path, isDirectory: &isDir) {
if !isDir.boolValue {
try removeItem(at: flatFileURL)
}
}
}

}
16 changes: 12 additions & 4 deletions BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ private class BDKService {

let documentsDirectoryURL = URL.documentsDirectory
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")
try FileManager.default.ensureDirectoryExists(at: walletDataDirectoryURL)
try FileManager.default.removeOldFlatFileIfNeeded(at: documentsDirectoryURL)
let persistenceBackendPath = walletDataDirectoryURL.appendingPathComponent("wallet.sqlite")
.path
let sqliteStore = try SqliteStore(path: persistenceBackendPath)
Expand All @@ -117,6 +119,8 @@ private class BDKService {
private func loadWallet(descriptor: Descriptor, changeDescriptor: Descriptor) throws {
let documentsDirectoryURL = URL.documentsDirectory
let walletDataDirectoryURL = documentsDirectoryURL.appendingPathComponent("wallet_data")
try FileManager.default.ensureDirectoryExists(at: walletDataDirectoryURL)
try FileManager.default.removeOldFlatFileIfNeeded(at: documentsDirectoryURL)
let persistenceBackendPath = walletDataDirectoryURL.appendingPathComponent("wallet.sqlite")
.path
let sqliteStore = try SqliteStore(path: persistenceBackendPath)
Expand Down Expand Up @@ -187,10 +191,14 @@ private class BDKService {

private func signAndBroadcast(psbt: Psbt) throws {
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
let _ = try wallet.sign(psbt: psbt)
let transaction = try psbt.extractTx()
let client = self.esploraClient
try client.broadcast(transaction: transaction)
let isSigned = try wallet.sign(psbt: psbt)
if isSigned {
let transaction = try psbt.extractTx()
let client = self.esploraClient
try client.broadcast(transaction: transaction)
} else {
throw WalletError.notSigned
}
}

func syncWithInspector(inspector: SyncScriptInspector) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ enum WalletError: Error {
case walletNotFound
case blockchainConfigNotFound
case dbNotFound
case notSigned
}
Loading