Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 22 additions & 23 deletions BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -214,32 +211,34 @@ 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 {
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion BDKSwiftExampleWallet/View Model/TabHomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion BDKSwiftExampleWallet/View Model/WalletViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
)
Expand Down
6 changes: 3 additions & 3 deletions BDKSwiftExampleWallet/View/Activity/TransactionItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct TransactionItemView: View {
.foregroundStyle(
{
switch canonicalTx.chainPosition {
case .confirmed(_, _):
case .confirmed(_):
Color.bitcoinOrange
case .unconfirmed(_):
Color.gray.opacity(0.5)
Expand All @@ -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
)
Expand Down