Skip to content

Commit 24e0c51

Browse files
committed
no message
1 parent 04e9d7f commit 24e0c51

9 files changed

Lines changed: 176 additions & 39 deletions

File tree

BDKSwiftExampleWallet/App/BDKSwiftExampleWalletApp.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@ struct BDKSwiftExampleWalletApp: App {
1616
var body: some Scene {
1717
WindowGroup {
1818
NavigationStack(path: $navigationPath) {
19+
let keyClient = KeyClient.live
20+
var syncService: BDKSyncService = EsploraServerSyncService(
21+
keyClient: keyClient,
22+
network: .bitcoin
23+
)
1924
if let _ = try? KeyClient.live.getBackupInfo() {
20-
HomeView(viewModel: .init(bdkClient: .live), navigationPath: $navigationPath)
25+
HomeView(
26+
viewModel: .init(
27+
bdkClient: .live,
28+
bdkSyncService: syncService
29+
),
30+
navigationPath: $navigationPath
31+
)
2132
} else {
2233
OnboardingView(
2334
viewModel: .init(
24-
bdkSyncService: EsploraServerSyncService(
25-
network: .bitcoin
26-
)
35+
bdkSyncService: syncService
2736
)
2837
)
29-
// OnboardingView(viewModel: .init(bdkClient: .live))
3038
}
3139
}
3240
.onChange(of: isOnboarding) { oldValue, newValue in

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ protocol BDKSyncService {
1818
func createWallet(params: String?) throws
1919
func loadWallet() throws
2020
func deleteWallet() throws
21+
22+
func updateNetwork(network: Network)
23+
func updateEsploraURL(_ url: String)
2124
}
2225

2326
extension BDKSyncService {
@@ -28,7 +31,7 @@ extension BDKSyncService {
2831
throw WalletError.dbNotFound
2932
}
3033

31-
let backupInfo = try createBackInfo(params: params ?? Mnemonic(wordCount: WordCount.words12).description)
34+
let backupInfo = try buildBackupInfo(params: params ?? Mnemonic(wordCount: WordCount.words12).description)
3235

3336
try keyClient.saveBackupInfo(backupInfo)
3437
try keyClient.saveNetwork(self.network.description)
@@ -46,7 +49,7 @@ extension BDKSyncService {
4649
return wallet
4750
}
4851

49-
func createBackInfo(params: String) throws -> BackupInfo {
52+
func buildBackupInfo(params: String) throws -> BackupInfo {
5053
if isXPub(params) {
5154
let descriptorPublicKey = try DescriptorPublicKey.fromString(publicKey: params)
5255
let fingerprint = descriptorPublicKey.masterFingerprint()
@@ -127,6 +130,54 @@ extension BDKSyncService {
127130
)
128131
}
129132

133+
func deleteData() throws {
134+
do {
135+
try keyClient.deleteAllData()
136+
137+
if let bundleID = Bundle.main.bundleIdentifier {
138+
UserDefaults.standard.removePersistentDomain(forName: bundleID)
139+
}
140+
141+
let walletDataDirectoryURL = URL.walletDataDirectoryURL
142+
if FileManager.default.fileExists(atPath: walletDataDirectoryURL.path) {
143+
try FileManager.default.removeItem(at: walletDataDirectoryURL)
144+
}
145+
146+
} catch {
147+
throw AppError.generic(message: "Failed to remove Keychain data")
148+
}
149+
}
150+
151+
func loadWalleFromBackup() throws -> Wallet {
152+
let backupInfo = try keyClient.getBackupInfo()
153+
let descriptor = try Descriptor(descriptor: backupInfo.descriptor, network: self.network)
154+
let changeDescriptor = try Descriptor(
155+
descriptor: backupInfo.changeDescriptor,
156+
network: self.network
157+
)
158+
159+
try FileManager.default.ensureDirectoryExists(at: URL.walletDataDirectoryURL)
160+
try FileManager.default.removeOldFlatFileIfNeeded(at: URL.defaultWalletDirectory)
161+
let persistenceBackendPath = URL.persistenceBackendPath
162+
let connection = try Connection(path: persistenceBackendPath)
163+
164+
let wallet = try Wallet.load(
165+
descriptor: descriptor,
166+
changeDescriptor: changeDescriptor,
167+
connection: connection
168+
)
169+
170+
return wallet
171+
}
172+
173+
// MARK: - Optionals methods
174+
175+
func updateEsploraURL(_ url: String) {
176+
// Optional implementation
177+
}
178+
179+
// MARK: - Private
180+
130181
private func isDescriptor(_ param: String) -> Bool {
131182
param.hasPrefix("tr(") ||
132183
param.hasPrefix("wpkh(") ||

BDKSwiftExampleWallet/Service/BDKSyncService/EsploraServerSyncService.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,21 @@ final class EsploraServerSyncService: BDKSyncService {
3838
}
3939

4040
func loadWallet() throws {
41-
41+
let wallet = try loadWalleFromBackup()
42+
self.wallet = wallet
4243
}
4344

4445
func deleteWallet() throws {
45-
46+
try deleteData()
47+
needsFullScan = true
48+
}
49+
50+
func updateNetwork(network: Network) {
51+
self.network = network
52+
}
53+
54+
func updateEsploraURL(_ url: String) {
55+
try? keyClient.saveEsploraURL(url)
56+
self.esploraClient = .init(url: url)
4657
}
4758
}

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoSyncService.swift

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
1+
////
2+
//// KyotoSyncService.swift
3+
//// BDKSwiftExampleWallet
4+
////
5+
//// Created by Rubens Machion on 16/05/25.
6+
////
17
//
2-
// KyotoSyncService.swift
8+
//import BitcoinDevKit
9+
//import Foundation
10+
//
11+
//final class KyotoSyncService: BDKSyncService {
12+
//
13+
// var connection: Connection?
14+
// var keyClient: KeyClient
15+
// var network: Network
16+
// var wallet: Wallet?
17+
// var needsFullScan = false
18+
//
19+
// private var node: CbfNode?
20+
// private var client: CbfClient?
21+
//
22+
// init(
23+
// keyClient: KeyClient = .live,
24+
// network: Network = .signet,
25+
// connection: Connection? = nil
26+
// ) {
27+
// self.connection = connection
28+
// self.keyClient = keyClient
29+
// self.network = network
30+
// }
31+
//
32+
// func createWallet(params: String?) throws {
33+
// self.wallet = try buildWallet(params: params)
34+
// }
35+
//
36+
// func loadWallet() throws {
37+
//
38+
// }
39+
//
40+
// func deleteWallet() throws {
41+
//
42+
// }
43+
//
44+
// func updateNetwork(network: Network) {
45+
// self.network = network
46+
// }
47+
//}
48+
49+
//
50+
// Untitled.swift
351
// BDKSwiftExampleWallet
452
//
553
// Created by Rubens Machion on 16/05/25.
@@ -16,8 +64,7 @@ final class KyotoSyncService: BDKSyncService {
1664
var wallet: Wallet?
1765
var needsFullScan = false
1866

19-
private var node: CbfNode?
20-
private var client: CbfClient?
67+
private var esploraClient: EsploraClient
2168

2269
init(
2370
keyClient: KeyClient = .live,
@@ -27,21 +74,33 @@ final class KyotoSyncService: BDKSyncService {
2774
self.connection = connection
2875
self.keyClient = keyClient
2976
self.network = network
77+
78+
let url = (try? keyClient.getEsploraURL()) ?? network.url
79+
self.esploraClient = .init(
80+
url: url
81+
)
3082
}
3183

3284
func createWallet(params: String?) throws {
3385
self.wallet = try buildWallet(params: params)
3486
}
3587

3688
func loadWallet() throws {
37-
89+
let wallet = try loadWalleFromBackup()
90+
self.wallet = wallet
3891
}
3992

4093
func deleteWallet() throws {
41-
94+
try deleteData()
95+
needsFullScan = true
4296
}
4397

4498
func updateNetwork(network: Network) {
4599
self.network = network
46100
}
101+
102+
func updateEsploraURL(_ url: String) {
103+
try? keyClient.saveEsploraURL(url)
104+
self.esploraClient = .init(url: url)
105+
}
47106
}

BDKSwiftExampleWallet/View Model/HomeViewModel.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ import Foundation
1212
@Observable
1313
class HomeViewModel: ObservableObject {
1414
let bdkClient: BDKClient
15+
16+
private let bdkSyncService: BDKSyncService
1517

1618
var homeViewError: AppError?
1719
var isWalletLoaded = false
1820
var showingHomeViewErrorAlert = false
1921

20-
init(bdkClient: BDKClient = .live) {
22+
init(
23+
bdkClient: BDKClient = .live,
24+
bdkSyncService: BDKSyncService
25+
) {
2126
self.bdkClient = bdkClient
27+
self.bdkSyncService = bdkSyncService
2228
}
2329

2430
func loadWallet() {
2531
do {
32+
try bdkSyncService.loadWallet()
33+
2634
try bdkClient.loadWallet()
2735
isWalletLoaded = true
2836
} catch let error as DescriptorError {

BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class OnboardingViewModel: ObservableObject {
4747
}
4848
@Published var selectedURL: String = "" {
4949
didSet {
50-
bdkSyncService.updateEsploraURL(selectedURL)
50+
// bdkClient.updateEsploraURL(selectedURL)
5151
}
5252
}
5353
@Published var words: String = ""
@@ -92,7 +92,7 @@ class OnboardingViewModel: ObservableObject {
9292
) {
9393
self.bdkSyncService = bdkSyncService
9494
self.selectedNetwork = bdkSyncService.network
95-
// self.selectedURL = bdkSyncService.network.url
95+
self.selectedURL = bdkSyncService.network.url
9696
}
9797

9898
func createWallet() {

BDKSwiftExampleWallet/View/HomeView.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ enum NavigationDestination: Hashable {
4848
case buildTransaction(amount: String, address: String, fee: Int)
4949
}
5050

51-
#if DEBUG
52-
#Preview {
53-
HomeView(
54-
viewModel: .init(bdkClient: .mock),
55-
navigationPath: .constant(.init())
56-
)
57-
}
58-
#endif
51+
//#if DEBUG
52+
// #Preview {
53+
// HomeView(
54+
// viewModel: .init(bdkClient: .mock),
55+
// navigationPath: .constant(.init())
56+
// )
57+
// }
58+
//#endif

BDKSwiftExampleWallet/View/OnboardingView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import BitcoinDevKit
99
import BitcoinUI
1010
import SwiftUI
1111

12-
struct OnboardingView: View {
12+
struct OnboardingView: View {
1313
@AppStorage("isOnboarding") var isOnboarding: Bool?
1414
@ObservedObject var viewModel: OnboardingViewModel
1515
@State private var showingOnboardingViewErrorAlert = false

BDKSwiftExampleWallet/View/WalletView.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,16 @@ struct WalletView: View {
185185
}
186186
}
187187

188-
#if DEBUG
189-
#Preview("WalletView - en") {
190-
WalletView(
191-
viewModel: .init(
192-
bdkClient: .mock,
193-
priceClient: .mock,
194-
transactions: [.mock],
195-
walletSyncState: .synced
196-
),
197-
sendNavigationPath: .constant(.init())
198-
)
199-
}
200-
#endif
188+
//#if DEBUG
189+
// #Preview("WalletView - en") {
190+
// WalletView(
191+
// viewModel: .init(
192+
// bdkClient: .mock,
193+
// priceClient: .mock,
194+
// transactions: [.mock],
195+
// walletSyncState: .synced
196+
// ),
197+
// sendNavigationPath: .constant(.init())
198+
// )
199+
// }
200+
//#endif

0 commit comments

Comments
 (0)