Skip to content

Commit e1543e2

Browse files
committed
no message
1 parent 93562f2 commit e1543e2

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

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

14-
private let service: BDKSyncService = KyotoService()
15-
// private let service: BDKSyncService = EsploraService()
14+
// private let service: BDKSyncService = KyotoService()
15+
private let service: BDKSyncService = EsploraService()
1616

1717
private var balance: Balance?
1818
private var connection: Connection?
@@ -92,6 +92,9 @@ private class BDKService {
9292
}
9393

9494
func deleteWallet() throws {
95+
Task {
96+
try await service.stopService()
97+
}
9598
try service.deleteWallet()
9699
needsFullScan = true
97100
}

BDKSwiftExampleWallet/Service/BDKSyncService/KyotoService.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
final class KyotoService: BDKSyncService {
1212

13-
private static let nodeHeight: UInt32 = 300_000
13+
private static let nodeHeight: UInt32 = 200_000
1414

1515
static let shared = KyotoService()
1616

@@ -22,6 +22,7 @@ final class KyotoService: BDKSyncService {
2222
private var client: CbfClient?
2323
private var node: CbfNode?
2424
private var connected = false
25+
private var isScanRunning = false
2526

2627
private var fullScanProgress: FullScanProgress?
2728
private var syncProgress: SyncScanProgress?
@@ -48,6 +49,7 @@ final class KyotoService: BDKSyncService {
4849
}
4950

5051
func startSync(progress: @escaping SyncScanProgress) async throws {
52+
if isScanRunning { return }
5153
guard let wallet = self.wallet else {
5254
throw WalletError.walletNotFound
5355
}
@@ -57,10 +59,12 @@ final class KyotoService: BDKSyncService {
5759
self.syncProgress = progress
5860
self.client = nodeComponents.client
5961
self.node = nodeComponents.node
62+
isScanRunning = true
6063
try await startListen()
6164
}
6265

6366
func startFullScan(progress: @escaping FullScanProgress) async throws {
67+
if isScanRunning { return }
6468
guard let wallet = self.wallet else {
6569
throw WalletError.walletNotFound
6670
}
@@ -70,19 +74,37 @@ final class KyotoService: BDKSyncService {
7074
self.fullScanProgress = progress
7175
self.client = nodeComponents.client
7276
self.node = nodeComponents.node
77+
isScanRunning = true
7378
try await startListen()
7479
}
7580

7681
func send(address: String, amount: UInt64, feeRate: UInt64) async throws {
77-
82+
let psbt = try buildTransaction(
83+
address: address,
84+
amount: amount,
85+
feeRate: feeRate
86+
)
87+
try await signAndBroadcast(psbt: psbt)
7888
}
7989

8090
func stopService() async throws {
91+
isScanRunning = false
8192
try await client?.shutdown()
8293
}
8394

8495
// MARK: - Private
8596

97+
private func signAndBroadcast(psbt: Psbt) async throws {
98+
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
99+
let isSigned = try wallet.sign(psbt: psbt)
100+
if isSigned {
101+
let transaction = try psbt.extractTx()
102+
try await client?.broadcast(transaction: transaction)
103+
} else {
104+
throw WalletError.notSigned
105+
}
106+
}
107+
86108
private func buildNode(from wallet: Wallet, scanType: ScanType) throws -> CbfComponents {
87109
try CbfBuilder()
88110
.dataDir(dataDir: Connection.dataDir)
@@ -100,11 +122,14 @@ final class KyotoService: BDKSyncService {
100122

101123
@discardableResult
102124
func startUpdating() async throws -> Bool {
103-
guard let update = await self.client?.update() else { return false }
125+
guard let update = await self.client?.update() else {
126+
isScanRunning = false
127+
return false
128+
}
104129
try self.wallet?.applyUpdate(update: update)
105130
let _ = try self.wallet?.persist(connection: self.connection ?? Connection.loadConnection())
106131
print("######### walletUpdated")
107-
132+
isScanRunning = false
108133
return true
109134
}
110135

@@ -133,7 +158,7 @@ final class KyotoService: BDKSyncService {
133158
private func updateWarn() {
134159
Task {
135160
while true {
136-
if let warn = try? await self.client!.nextWarning() {
161+
if let warn = try? await self.client?.nextWarning() {
137162
switch warn {
138163
case .needConnections:
139164
print("######### disconnected")

BDKSwiftExampleWallet/Utilities/Constants.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ struct Constants {
2929
struct Signet {
3030
static let bdk = "http://signet.bitcoindevkit.net"
3131
static let mutiny = "https://mutinynet.com/api"
32+
static let mempoolspace = "https://mempool.space/signet/api"
3233
static let allValues = [
3334
mutiny,
3435
bdk,
36+
mempoolspace,
3537
]
3638
}
3739
struct Testnet {

0 commit comments

Comments
 (0)