Skip to content

Commit 37046bf

Browse files
committed
feat: added option to in onboard to synchronise using kyoto and saving in keychain
1 parent a6ee9bf commit 37046bf

File tree

8 files changed

+91
-32
lines changed

8 files changed

+91
-32
lines changed

BDKSwiftExampleWallet/Resources/Localizable.xcstrings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@
691691
}
692692
}
693693
}
694+
},
695+
"Kyoto" : {
696+
694697
},
695698
"Navigation Title" : {
696699
"extractionState" : "stale",
@@ -1143,6 +1146,9 @@
11431146
}
11441147
}
11451148
}
1149+
},
1150+
"Sync type" : {
1151+
11461152
},
11471153
"Syncing" : {
11481154
"extractionState" : "manual",

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ import Foundation
1111
private class BDKService {
1212
static var shared: BDKService = BDKService()
1313

14-
// private let service: BDKSyncService = KyotoService.live
15-
private let service: BDKSyncService = EsploraService.live
14+
private var syncMode: SyncMode?
15+
private var service: BDKSyncService {
16+
switch try? keyClient.getSyncMode() {
17+
case .kyoto:
18+
return KyotoService.live
19+
default:
20+
return EsploraService.live
21+
}
22+
}
1623
private let keyClient: KeyClient
1724
private var needsFullScan: Bool = false
1825
private(set) var network: Network
@@ -46,6 +53,13 @@ private class BDKService {
4653
try? keyClient.saveEsploraURL(newURL)
4754
}
4855
}
56+
57+
func updateSyncMode(_ mode: SyncMode) {
58+
if syncMode != mode {
59+
self.syncMode = mode
60+
try? keyClient.saveSyncMode(mode)
61+
}
62+
}
4963

5064
func getAddress() throws -> String {
5165
try service.getAddress()
@@ -114,15 +128,15 @@ private class BDKService {
114128
try await service.startFullScan(progress: progress)
115129
}
116130

117-
func calculateFee(tx: Transaction) throws -> Amount {
131+
func calculateFee(tx: BitcoinDevKit.Transaction) throws -> Amount {
118132
try service.calculateFee(tx: tx)
119133
}
120134

121-
func calculateFeeRate(tx: Transaction) throws -> UInt64 {
135+
func calculateFeeRate(tx: BitcoinDevKit.Transaction) throws -> UInt64 {
122136
try service.calculateFeeRate(tx: tx)
123137
}
124138

125-
func sentAndReceived(tx: Transaction) throws -> SentAndReceivedValues {
139+
func sentAndReceived(tx: BitcoinDevKit.Transaction) throws -> SentAndReceivedValues {
126140
try service.sentAndReceived(tx: tx)
127141
}
128142

@@ -154,9 +168,9 @@ struct BDKClient {
154168
let fullScanWithFullScanProgress: (@escaping FullScanProgress) async throws -> Void
155169
let getAddress: () throws -> String
156170
let send: (String, UInt64, UInt64) throws -> Void
157-
let calculateFee: (Transaction) throws -> Amount
158-
let calculateFeeRate: (Transaction) throws -> UInt64
159-
let sentAndReceived: (Transaction) throws -> SentAndReceivedValues
171+
let calculateFee: (BitcoinDevKit.Transaction) throws -> Amount
172+
let calculateFeeRate: (BitcoinDevKit.Transaction) throws -> UInt64
173+
let sentAndReceived: (BitcoinDevKit.Transaction) throws -> SentAndReceivedValues
160174
let buildTransaction: (String, UInt64, UInt64) throws -> Psbt
161175
let getBackupInfo: () throws -> BackupInfo
162176
let needsFullScan: () -> Bool
@@ -166,6 +180,7 @@ struct BDKClient {
166180
let updateNetwork: (Network) -> Void
167181
let updateEsploraURL: (String) -> Void
168182
let stop: () async throws -> Void
183+
let upateSyncMode: (SyncMode) -> Void
169184
}
170185

171186
extension BDKClient {
@@ -221,6 +236,9 @@ extension BDKClient {
221236
},
222237
stop: {
223238
try await BDKService.shared.stop()
239+
},
240+
upateSyncMode: { mode in
241+
BDKService.shared.updateSyncMode(mode)
224242
}
225243
)
226244
}
@@ -278,7 +296,8 @@ extension BDKClient {
278296
getEsploraURL: { Constants.Config.EsploraServerURLNetwork.Signet.mutiny },
279297
updateNetwork: { _ in },
280298
updateEsploraURL: { _ in },
281-
stop: { }
299+
stop: { },
300+
upateSyncMode: { _ in }
282301
)
283302
}
284303
#endif

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import Foundation
1111
typealias FullScanProgress = (UInt64) -> Void
1212
typealias SyncScanProgress = (UInt64, UInt64) -> Void
1313

14+
enum SyncMode: String {
15+
case esplora, kyoto
16+
}
17+
1418
protocol BDKSyncService {
1519
var connection: Connection? { get }
1620
var keyClient: KeyClient { get }

BDKSwiftExampleWallet/Service/BDKSyncService/EsploraService.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ extension EsploraService {
1414

1515
final class EsploraService: BDKSyncService {
1616

17-
static let shared = EsploraService()
18-
1917
var connection: Connection?
2018
var keyClient: KeyClient
2119
var network: Network

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoService.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ extension KyotoService {
1313
}
1414

1515
final class KyotoService: BDKSyncService {
16-
17-
// private static let nodeHeight: UInt32 = 250_000
18-
private static let nodeHeight: UInt32 = 800_000
19-
20-
static let shared = KyotoService()
2116

2217
var connection: Connection?
2318
var keyClient: KeyClient

BDKSwiftExampleWallet/Service/Key Service/KeyService.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ private struct KeyService {
6868
try deleteBackupInfo()
6969
try deleteEsploraURL()
7070
}
71+
72+
func saveSyncMode(_ mode: SyncMode) throws {
73+
keychain[string: "SyncMode"] = mode.rawValue
74+
}
75+
76+
func getSyncMode() throws -> SyncMode? {
77+
guard let mode = keychain[string: "SyncMode"] else {
78+
return nil
79+
}
80+
return SyncMode(rawValue: mode)
81+
}
7182
}
7283

7384
struct KeyClient {
@@ -81,6 +92,8 @@ struct KeyClient {
8192
let saveBackupInfo: (BackupInfo) throws -> Void
8293
let saveNetwork: (String) throws -> Void
8394
let deleteAllData: () throws -> Void
95+
let saveSyncMode: (SyncMode) throws -> Void
96+
let getSyncMode: () throws -> SyncMode?
8497

8598
private init(
8699
deleteBackupInfo: @escaping () throws -> Void,
@@ -92,7 +105,9 @@ struct KeyClient {
92105
saveBackupInfo: @escaping (BackupInfo) throws -> Void,
93106
saveEsploraURL: @escaping (String) throws -> Void,
94107
saveNetwork: @escaping (String) throws -> Void,
95-
deleteAllData: @escaping () throws -> Void
108+
deleteAllData: @escaping () throws -> Void,
109+
saveSyncMode: @escaping (SyncMode) throws -> Void,
110+
getSyncMode: @escaping () throws -> SyncMode?
96111
) {
97112
self.deleteBackupInfo = deleteBackupInfo
98113
self.deleteEsplora = deleteEsplora
@@ -104,6 +119,8 @@ struct KeyClient {
104119
self.saveEsploraURL = saveEsploraURL
105120
self.saveNetwork = saveNetwork
106121
self.deleteAllData = deleteAllData
122+
self.saveSyncMode = saveSyncMode
123+
self.getSyncMode = getSyncMode
107124
}
108125
}
109126

@@ -118,7 +135,9 @@ extension KeyClient {
118135
saveBackupInfo: { backupInfo in try KeyService().saveBackupInfo(backupInfo: backupInfo) },
119136
saveEsploraURL: { url in try KeyService().saveEsploraURL(url: url) },
120137
saveNetwork: { network in try KeyService().saveNetwork(network: network) },
121-
deleteAllData: { try KeyService().deletaAllData() }
138+
deleteAllData: { try KeyService().deletaAllData() },
139+
saveSyncMode: { mode in try KeyService().saveSyncMode(mode) },
140+
getSyncMode: { try KeyService().getSyncMode() }
122141
)
123142
}
124143

@@ -159,7 +178,9 @@ extension KeyClient {
159178
saveBackupInfo: { _ in },
160179
saveEsploraURL: { _ in },
161180
saveNetwork: { _ in },
162-
deleteAllData: { }
181+
deleteAllData: { },
182+
saveSyncMode: { _ in },
183+
getSyncMode: { .esplora }
163184
)
164185
}
165186
#endif

BDKSwiftExampleWallet/View Model/OnboardingViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class OnboardingViewModel: ObservableObject {
1616
let bdkClient: BDKClient
1717

1818
@AppStorage("isOnboarding") var isOnboarding: Bool?
19+
@Published var syncMode: SyncMode? {
20+
didSet {
21+
bdkClient.upateSyncMode(syncMode ?? .esplora)
22+
}
23+
}
1924
@Published var createWithPersistError: CreateWithPersistError?
2025
var isDescriptor: Bool {
2126
words.hasPrefix("tr(") || words.hasPrefix("wpkh(") || words.hasPrefix("wsh(")

BDKSwiftExampleWallet/View/OnboardingView.swift

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,35 @@ struct OnboardingView: View {
122122
.opacity(animateContent ? 1 : 0)
123123
.animation(.easeOut(duration: 0.5).delay(1.5), value: animateContent)
124124

125-
Picker("Esplora Server", selection: $viewModel.selectedURL) {
126-
ForEach(viewModel.availableURLs, id: \.self) { url in
127-
Text(
128-
url.replacingOccurrences(
129-
of: "https://",
130-
with: ""
131-
).replacingOccurrences(
132-
of: "http://",
133-
with: ""
134-
)
135-
)
136-
.tag(url)
137-
}
125+
Picker("Sync type", selection: $viewModel.syncMode) {
126+
Text("Esplora Server").tag(SyncMode.esplora)
127+
Text("Kyoto").tag(SyncMode.kyoto)
138128
}
139129
.pickerStyle(.automatic)
140130
.tint(.primary)
141131
.opacity(animateContent ? 1 : 0)
142132
.animation(.easeOut(duration: 0.5).delay(1.5), value: animateContent)
133+
134+
if viewModel.syncMode == nil || viewModel.syncMode == .esplora {
135+
Picker("Esplora Server", selection: $viewModel.selectedURL) {
136+
ForEach(viewModel.availableURLs, id: \.self) { url in
137+
Text(
138+
url.replacingOccurrences(
139+
of: "https://",
140+
with: ""
141+
).replacingOccurrences(
142+
of: "http://",
143+
with: ""
144+
)
145+
)
146+
.tag(url)
147+
}
148+
}
149+
.pickerStyle(.automatic)
150+
.tint(.primary)
151+
.opacity(animateContent ? 1 : 0)
152+
.animation(.easeOut(duration: 0.5).delay(1.5), value: animateContent)
153+
}
143154
}
144155

145156
if !viewModel.words.isEmpty {

0 commit comments

Comments
 (0)