Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -128,14 +128,24 @@ extension CbfClient {
if Task.isCancelled { break }
do {
let warning = try await self.nextWarning()
if case .needConnections = warning {
switch warning {
case .needConnections:
await MainActor.run {
NotificationCenter.default.post(
name: NSNotification.Name("KyotoConnectionUpdate"),
object: nil,
userInfo: ["connected": false]
)
}
case let .transactionRejected(wtxid, reason):
BDKService.shared.handleKyotoRejectedTransaction(wtxidHex: wtxid)
if let reason {
print("Kyoto rejected tx \(wtxid): \(reason)")
} else {
print("Kyoto rejected tx \(wtxid)")
}
default:
break
}
} catch is CancellationError {
break
Expand Down
40 changes: 39 additions & 1 deletion BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ extension BlockchainClient {
}
}

private class BDKService {
final class BDKService {
static let shared: BDKService = BDKService()

private var balance: Balance?
Expand All @@ -110,6 +110,8 @@ private class BDKService {
private(set) var network: Network
private var blockchainURL: String
internal private(set) var wallet: Wallet?
private var kyotoPendingTxs: [String: Txid] = [:]
private let kyotoPendingTxQueue = DispatchQueue(label: "bdk.service.kyoto.pending")

init(keyClient: KeyClient = .live) {
self.keyClient = keyClient
Expand Down Expand Up @@ -532,6 +534,7 @@ private class BDKService {
try? keyClient.deleteEsplora()

needsFullScan = true
clearKyotoTrackedTransactions()
}

func getBackupInfo() throws -> BackupInfo {
Expand Down Expand Up @@ -576,6 +579,7 @@ private class BDKService {
try await self.blockchainClient.broadcast(transaction)

if self.clientType == .kyoto {
trackKyotoBroadcast(transaction)
let lastSeen = UInt64(Date().timeIntervalSince1970)
let unconfirmedTx = UnconfirmedTx(tx: transaction, lastSeen: lastSeen)
wallet.applyUnconfirmedTxs(unconfirmedTxs: [unconfirmedTx])
Expand All @@ -589,6 +593,40 @@ private class BDKService {
}
}

private func trackKyotoBroadcast(_ transaction: Transaction) {
let wtxidData = transaction.computeWtxid().serialize()
let wtxidHex = [UInt8](wtxidData).hexString.lowercased()
let txid = transaction.computeTxid()
kyotoPendingTxQueue.sync {
kyotoPendingTxs[wtxidHex] = txid
}
}

private func takeKyotoTx(for wtxidHex: String) -> Txid? {
kyotoPendingTxQueue.sync {
kyotoPendingTxs.removeValue(forKey: wtxidHex.lowercased())
}
}

private func clearKyotoTrackedTransactions() {
kyotoPendingTxQueue.sync {
kyotoPendingTxs.removeAll()
}
}

func handleKyotoRejectedTransaction(wtxidHex: String) {
guard let txid = takeKyotoTx(for: wtxidHex) else { return }
guard let wallet = self.wallet else { return }
let evictedTx = EvictedTx(
txid: txid,
evictedAt: UInt64(Date().timeIntervalSince1970)
)
wallet.applyEvictedTxs(evictedTxs: [evictedTx])
if let persister = self.persister {
try? wallet.persist(persister: persister)
}
}

func syncWithInspector(inspector: SyncScriptInspector) async throws {
guard let wallet = self.wallet else { throw WalletError.walletNotFound }
let syncRequest = try wallet.startSyncWithRevealedSpks()
Expand Down