Skip to content

Commit 91acf93

Browse files
committed
no message
1 parent a0708dd commit 91acf93

File tree

3 files changed

+103
-128
lines changed

3 files changed

+103
-128
lines changed

BDKSwiftExampleWallet/Service/BDKSyncService/BDKSyncService.swift

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ extension BDKSyncService {
138138
)
139139
}
140140

141+
func deleteWallet() throws {
142+
try deleteData()
143+
}
144+
141145
func deleteData() throws {
142146
do {
143147
try keyClient.deleteAllData()
@@ -176,6 +180,85 @@ extension BDKSyncService {
176180
return wallet
177181
}
178182

183+
func getBalance() throws -> Balance {
184+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
185+
let balance = wallet.balance()
186+
return balance
187+
}
188+
189+
func sentAndReceived(tx: Transaction) throws -> SentAndReceivedValues {
190+
guard let wallet = self.wallet else {
191+
throw WalletError.walletNotFound
192+
}
193+
let values = wallet.sentAndReceived(tx: tx)
194+
return values
195+
}
196+
197+
func calculateFeeRate(tx: Transaction) throws -> UInt64 {
198+
guard let wallet = self.wallet else {
199+
throw WalletError.walletNotFound
200+
}
201+
let feeRate = try wallet.calculateFeeRate(tx: tx)
202+
return feeRate.toSatPerVbCeil()
203+
}
204+
205+
func calculateFee(tx: Transaction) throws -> Amount {
206+
guard let wallet = self.wallet else {
207+
throw WalletError.walletNotFound
208+
}
209+
let fee = try wallet.calculateFee(tx: tx)
210+
return fee
211+
}
212+
213+
func buildTransaction(
214+
address: String,
215+
amount: UInt64,
216+
feeRate: UInt64
217+
) throws -> Psbt {
218+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
219+
let script = try Address(address: address, network: self.network)
220+
.scriptPubkey()
221+
let txBuilder = try TxBuilder()
222+
.addRecipient(
223+
script: script,
224+
amount: Amount.fromSat(satoshi: amount)
225+
)
226+
.feeRate(feeRate: FeeRate.fromSatPerVb(satVb: feeRate))
227+
.finish(wallet: wallet)
228+
return txBuilder
229+
}
230+
231+
func listUnspent() throws -> [LocalOutput] {
232+
guard let wallet = self.wallet else {
233+
throw WalletError.walletNotFound
234+
}
235+
let localOutputs = wallet.listUnspent()
236+
return localOutputs
237+
}
238+
239+
func getAddress() throws -> String {
240+
guard let wallet = self.wallet else {
241+
throw WalletError.walletNotFound
242+
}
243+
guard let connection = self.connection else {
244+
throw WalletError.dbNotFound
245+
}
246+
let addressInfo = wallet.revealNextAddress(keychain: .external)
247+
let _ = try wallet.persist(connection: connection)
248+
return addressInfo.address.description
249+
}
250+
251+
func getTransactions() throws -> [CanonicalTx] {
252+
guard let wallet = self.wallet else {
253+
throw WalletError.walletNotFound
254+
}
255+
let transactions = wallet.transactions()
256+
let sortedTransactions = transactions.sorted { (tx1, tx2) in
257+
return tx1.chainPosition.isBefore(tx2.chainPosition)
258+
}
259+
return sortedTransactions
260+
}
261+
179262
// MARK: - Optionals methods
180263

181264
func updateEsploraURL(_ url: String) {

BDKSwiftExampleWallet/Service/BDKSyncService/EsploraService.swift

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ final class EsploraService: BDKSyncService {
4545
self.wallet = wallet
4646
}
4747

48-
func deleteWallet() throws {
49-
try deleteData()
50-
}
51-
5248
func updateNetwork(network: Network) {
5349
self.network = network
5450
}
@@ -95,65 +91,6 @@ final class EsploraService: BDKSyncService {
9591
let _ = try wallet.persist(connection: connection)
9692
}
9793

98-
func getTransactions() throws -> [CanonicalTx] {
99-
guard let wallet = self.wallet else {
100-
throw WalletError.walletNotFound
101-
}
102-
let transactions = wallet.transactions()
103-
let sortedTransactions = transactions.sorted { (tx1, tx2) in
104-
return tx1.chainPosition.isBefore(tx2.chainPosition)
105-
}
106-
return sortedTransactions
107-
}
108-
109-
func getBalance() throws -> Balance {
110-
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
111-
let balance = wallet.balance()
112-
return balance
113-
}
114-
115-
func sentAndReceived(tx: Transaction) throws -> SentAndReceivedValues {
116-
guard let wallet = self.wallet else {
117-
throw WalletError.walletNotFound
118-
}
119-
let values = wallet.sentAndReceived(tx: tx)
120-
return values
121-
}
122-
123-
func calculateFeeRate(tx: Transaction) throws -> UInt64 {
124-
guard let wallet = self.wallet else {
125-
throw WalletError.walletNotFound
126-
}
127-
let feeRate = try wallet.calculateFeeRate(tx: tx)
128-
return feeRate.toSatPerVbCeil()
129-
}
130-
131-
func calculateFee(tx: Transaction) throws -> Amount {
132-
guard let wallet = self.wallet else {
133-
throw WalletError.walletNotFound
134-
}
135-
let fee = try wallet.calculateFee(tx: tx)
136-
return fee
137-
}
138-
139-
func buildTransaction(
140-
address: String,
141-
amount: UInt64,
142-
feeRate: UInt64
143-
) throws -> Psbt {
144-
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
145-
let script = try Address(address: address, network: self.network)
146-
.scriptPubkey()
147-
let txBuilder = try TxBuilder()
148-
.addRecipient(
149-
script: script,
150-
amount: Amount.fromSat(satoshi: amount)
151-
)
152-
.feeRate(feeRate: FeeRate.fromSatPerVb(satVb: feeRate))
153-
.finish(wallet: wallet)
154-
return txBuilder
155-
}
156-
15794
func send(
15895
address: String,
15996
amount: UInt64,
@@ -167,26 +104,6 @@ final class EsploraService: BDKSyncService {
167104
try signAndBroadcast(psbt: psbt)
168105
}
169106

170-
func listUnspent() throws -> [LocalOutput] {
171-
guard let wallet = self.wallet else {
172-
throw WalletError.walletNotFound
173-
}
174-
let localOutputs = wallet.listUnspent()
175-
return localOutputs
176-
}
177-
178-
func getAddress() throws -> String {
179-
guard let wallet = self.wallet else {
180-
throw WalletError.walletNotFound
181-
}
182-
guard let connection = self.connection else {
183-
throw WalletError.dbNotFound
184-
}
185-
let addressInfo = wallet.revealNextAddress(keychain: .external)
186-
let _ = try wallet.persist(connection: connection)
187-
return addressInfo.address.description
188-
}
189-
190107
// MARK: - Private
191108

192109
private func signAndBroadcast(psbt: Psbt) throws {

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoService.swift

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,68 +40,43 @@ final class KyotoService: BDKSyncService {
4040
self.connection = try Connection.loadConnection()
4141
let wallet = try loadWalleFromBackup()
4242
self.wallet = wallet
43-
44-
let nodeComponents = try buildNode(from: wallet)
45-
self.client = nodeComponents.client
46-
self.node = nodeComponents.node
47-
startListen()
48-
}
49-
50-
func deleteWallet() throws {
51-
5243
}
5344

5445
func startSync(progress: any SyncScriptInspector) async throws {
55-
46+
guard let wallet = self.wallet else {
47+
throw WalletError.walletNotFound
48+
}
49+
let nodeComponents = try buildNode(
50+
from: wallet, scanType: .sync
51+
)
52+
self.client = nodeComponents.client
53+
self.node = nodeComponents.node
54+
startListen()
5655
}
5756

5857
func startFullScan(progress: any FullScanScriptInspector) async throws {
59-
60-
}
61-
62-
func getTransactions() throws -> [CanonicalTx] {
63-
[]
64-
}
65-
66-
func getBalance() throws -> Balance {
67-
.mock
68-
}
69-
70-
func sentAndReceived(tx: Transaction) throws -> SentAndReceivedValues {
71-
.mock
72-
}
73-
74-
func calculateFeeRate(tx: Transaction) throws -> UInt64 {
75-
.zero
76-
}
77-
78-
func calculateFee(tx: Transaction) throws -> Amount {
79-
try .fromBtc(btc: .zero)
80-
}
81-
82-
func buildTransaction(address: String, amount: UInt64, feeRate: UInt64) throws -> Psbt {
83-
.init(noPointer: .init())
58+
guard let wallet = self.wallet else {
59+
throw WalletError.walletNotFound
60+
}
61+
let nodeComponents = try buildNode(
62+
from: wallet, scanType: .recovery(fromHeight: 200_000)
63+
)
64+
self.client = nodeComponents.client
65+
self.node = nodeComponents.node
66+
startListen()
8467
}
8568

8669
func send(address: String, amount: UInt64, feeRate: UInt64) async throws {
8770

8871
}
8972

90-
func listUnspent() throws -> [LocalOutput] {
91-
[]
92-
}
93-
94-
func getAddress() throws -> String {
95-
""
96-
}
97-
9873
// MARK: - Private
9974

100-
private func buildNode(from wallet: Wallet) throws -> CbfComponents {
75+
private func buildNode(from wallet: Wallet, scanType: ScanType) throws -> CbfComponents {
10176
try CbfBuilder()
10277
.dataDir(dataDir: Connection.dataDir)
10378
.logLevel(logLevel: .debug)
104-
.scanType(scanType: .recovery(fromHeight: 200_000))
79+
.scanType(scanType: scanType)
10580
.build(wallet: wallet)
10681
}
10782

0 commit comments

Comments
 (0)