diff --git a/BDKSwiftExampleWallet/Extensions/BDK+Extensions/BDKError+Extensions.swift b/BDKSwiftExampleWallet/Extensions/BDK+Extensions/BDKError+Extensions.swift index 658c9693..d7d95bcf 100644 --- a/BDKSwiftExampleWallet/Extensions/BDK+Extensions/BDKError+Extensions.swift +++ b/BDKSwiftExampleWallet/Extensions/BDK+Extensions/BDKError+Extensions.swift @@ -138,21 +138,8 @@ extension SignerError { return errorMessage case .TxInputsIndexError(let errorMessage): return errorMessage - } - } -} - -extension WalletCreationError { - var description: String { - switch self { - case .Descriptor: - return "descriptor" - case .LoadedGenesisDoesNotMatch: - return "loaded genesis does not match" - case .LoadedNetworkDoesNotMatch(let expected, let got): - return "got: \(String(describing: got)), expected \(expected)" - case .LoadedDescriptorDoesNotMatch(let got, let keychain): - return "got: \(String(describing: got)), keychain \(keychain)" + case .Psbt(let errorMessage): + return errorMessage } } } diff --git a/BDKSwiftExampleWallet/Extensions/BDK+Extensions/CanonicalTx+Extensions.swift b/BDKSwiftExampleWallet/Extensions/BDK+Extensions/CanonicalTx+Extensions.swift index e6410687..05dbd672 100644 --- a/BDKSwiftExampleWallet/Extensions/BDK+Extensions/CanonicalTx+Extensions.swift +++ b/BDKSwiftExampleWallet/Extensions/BDK+Extensions/CanonicalTx+Extensions.swift @@ -12,7 +12,15 @@ import Foundation extension CanonicalTx { static var mock = Self( transaction: .mock!, - chainPosition: .confirmed(height: UInt32(1_127_972), timestamp: UInt64(1_716_927_886)) + chainPosition: .confirmed( + confirmationBlockTime: .init( + blockId: .init( + height: UInt32(12), + hash: "hash" + ), + confirmationTime: UInt64(21) + ) + ) ) } //#endif diff --git a/BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift b/BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift index a12950ac..653f79e7 100644 --- a/BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift +++ b/BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift @@ -16,7 +16,7 @@ private class BDKService { private let keyService: KeyClient private let esploraClient: EsploraClient private var needsFullScan: Bool = false - private var dbService: SqliteStore? + private var connection: Connection? init( keyService: KeyClient = .live @@ -35,11 +35,11 @@ private class BDKService { guard let wallet = self.wallet else { throw WalletError.walletNotFound } - let addressInfo = wallet.revealNextAddress(keychain: .external) - guard let db = self.dbService else { throw WalletError.dbNotFound } - if let changeSet = wallet.takeStaged() { - try db.write(changeSet: changeSet) + guard let connection = self.connection else { + throw WalletError.dbNotFound } + let addressInfo = wallet.revealNextAddress(keychain: .external) + let _ = try wallet.persist(connection: connection) return addressInfo.address.description } @@ -112,14 +112,13 @@ private class BDKService { try FileManager.default.removeOldFlatFileIfNeeded(at: documentsDirectoryURL) let persistenceBackendPath = walletDataDirectoryURL.appendingPathComponent("wallet.sqlite") .path - let sqliteStore = try SqliteStore(path: persistenceBackendPath) - self.dbService = sqliteStore - let changeSet = try sqliteStore.read() - let wallet = try Wallet.newOrLoad( + let connection = try Connection(path: persistenceBackendPath) + self.connection = connection + let wallet = try Wallet( descriptor: descriptor, changeDescriptor: changeDescriptor, - changeSet: changeSet, - network: network + network: network, + connection: connection ) self.wallet = wallet } @@ -131,14 +130,12 @@ private class BDKService { try FileManager.default.removeOldFlatFileIfNeeded(at: documentsDirectoryURL) let persistenceBackendPath = walletDataDirectoryURL.appendingPathComponent("wallet.sqlite") .path - let sqliteStore = try SqliteStore(path: persistenceBackendPath) - self.dbService = sqliteStore - let changeSet = try sqliteStore.read() - let wallet = try Wallet.newOrLoad( + let connection = try Connection(path: persistenceBackendPath) + self.connection = connection + let wallet = try Wallet.load( descriptor: descriptor, changeDescriptor: changeDescriptor, - changeSet: changeSet, - network: network + connection: connection ) self.wallet = wallet } @@ -214,15 +211,16 @@ private class BDKService { let esploraClient = self.esploraClient let syncRequest = try wallet.startSyncWithRevealedSpks() .inspectSpks(inspector: inspector) + .build() let update = try esploraClient.sync( syncRequest: syncRequest, parallelRequests: UInt64(5) ) let _ = try wallet.applyUpdate(update: update) - guard let db = self.dbService else { throw WalletError.dbNotFound } - if let changeSet = wallet.takeStaged() { - try db.write(changeSet: changeSet) + guard let connection = self.connection else { + throw WalletError.dbNotFound } + let _ = try wallet.persist(connection: connection) } func fullScanWithInspector(inspector: FullScanScriptInspector) async throws { @@ -230,16 +228,17 @@ private class BDKService { let esploraClient = esploraClient let fullScanRequest = try wallet.startFullScan() .inspectSpksForAllKeychains(inspector: inspector) + .build() let update = try esploraClient.fullScan( fullScanRequest: fullScanRequest, stopGap: UInt64(150), // should we default value this for folks? parallelRequests: UInt64(5) // should we default value this for folks? ) let _ = try wallet.applyUpdate(update: update) - guard let db = self.dbService else { throw WalletError.dbNotFound } - if let changeSet = wallet.takeStaged() { - try db.write(changeSet: changeSet) + guard let connection = self.connection else { + throw WalletError.dbNotFound } + let _ = try wallet.persist(connection: connection) } func calculateFee(tx: Transaction) throws -> Amount { diff --git a/BDKSwiftExampleWallet/View Model/Activity/ActivityListViewModel.swift b/BDKSwiftExampleWallet/View Model/Activity/ActivityListViewModel.swift index 528edcbe..7e00fa85 100644 --- a/BDKSwiftExampleWallet/View Model/Activity/ActivityListViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/Activity/ActivityListViewModel.swift @@ -74,7 +74,7 @@ class ActivityListViewModel { } catch let error as EsploraError { self.walletViewError = .generic(message: error.localizedDescription) self.showingWalletViewErrorAlert = true - } catch let error as InspectError { + } catch let error as RequestBuilderError { self.walletViewError = .generic(message: error.localizedDescription) self.showingWalletViewErrorAlert = true } catch let error as PersistenceError { diff --git a/BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift b/BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift index 21adc5a3..c101853d 100644 --- a/BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift @@ -19,7 +19,7 @@ class OnboardingViewModel: ObservableObject { @Published var networkColor = Color.gray @Published var onboardingViewError: AppError? - @Published var walletCreationError: WalletCreationError? + @Published var createWithPersistError: CreateWithPersistError? @Published var words: String = "" { didSet { updateWordArray() @@ -102,9 +102,9 @@ class OnboardingViewModel: ObservableObject { do { try bdkClient.createWallet(words) isOnboarding = false - } catch let error as WalletCreationError { + } catch let error as CreateWithPersistError { DispatchQueue.main.async { - self.walletCreationError = error + self.createWithPersistError = error } } catch { DispatchQueue.main.async { diff --git a/BDKSwiftExampleWallet/View Model/TabHomeViewModel.swift b/BDKSwiftExampleWallet/View Model/TabHomeViewModel.swift index 6dc7a048..67d46f9e 100644 --- a/BDKSwiftExampleWallet/View Model/TabHomeViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/TabHomeViewModel.swift @@ -26,7 +26,7 @@ class TabHomeViewModel: ObservableObject { } catch let error as DescriptorError { self.tabViewError = .generic(message: error.localizedDescription) self.showingTabViewErrorAlert = true - } catch let error as WalletCreationError { + } catch let error as LoadWithPersistError { self.tabViewError = .generic(message: error.localizedDescription) self.showingTabViewErrorAlert = true } catch { diff --git a/BDKSwiftExampleWallet/View Model/WalletViewModel.swift b/BDKSwiftExampleWallet/View Model/WalletViewModel.swift index 3ee250d2..6916d0db 100644 --- a/BDKSwiftExampleWallet/View Model/WalletViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/WalletViewModel.swift @@ -96,7 +96,7 @@ class WalletViewModel { } catch let error as EsploraError { self.walletViewError = .generic(message: error.localizedDescription) self.showingWalletViewErrorAlert = true - } catch let error as InspectError { + } catch let error as RequestBuilderError { self.walletViewError = .generic(message: error.localizedDescription) self.showingWalletViewErrorAlert = true } catch let error as PersistenceError { diff --git a/BDKSwiftExampleWallet/View/Activity/TransactionDetailView.swift b/BDKSwiftExampleWallet/View/Activity/TransactionDetailView.swift index b33f093e..d90e982b 100644 --- a/BDKSwiftExampleWallet/View/Activity/TransactionDetailView.swift +++ b/BDKSwiftExampleWallet/View/Activity/TransactionDetailView.swift @@ -46,8 +46,8 @@ struct TransactionDetailView: View { .fontWeight(.semibold) switch canonicalTx.chainPosition { - case .confirmed(let height, _): - Text("Block \(height.delimiter)") + case .confirmed(let confirmationBlockTime): + Text("Block \(confirmationBlockTime.blockId.height.delimiter)") .foregroundColor(.secondary) case .unconfirmed(_): Text("Unconfirmed") @@ -72,9 +72,9 @@ struct TransactionDetailView: View { .fontDesign(.rounded) VStack(spacing: 4) { switch canonicalTx.chainPosition { - case .confirmed(_, let timestamp): + case .confirmed(let confirmationBlockTime): Text( - timestamp.toDate().formatted( + confirmationBlockTime.confirmationTime.toDate().formatted( date: .abbreviated, time: Date.FormatStyle.TimeStyle.shortened ) diff --git a/BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift b/BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift index 1900a7fa..15de72ea 100644 --- a/BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift +++ b/BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift @@ -44,7 +44,7 @@ struct TransactionItemView: View { .foregroundStyle( { switch canonicalTx.chainPosition { - case .confirmed(_, _): + case .confirmed(_): Color.bitcoinOrange case .unconfirmed(_): Color.gray.opacity(0.5) @@ -63,9 +63,9 @@ struct TransactionItemView: View { .font(.title) .foregroundColor(.primary) switch canonicalTx.chainPosition { - case .confirmed(_, let timestamp): + case .confirmed(let confirmationBlockTime): Text( - timestamp.toDate().formatted( + confirmationBlockTime.confirmationTime.toDate().formatted( date: .abbreviated, time: .shortened )