Skip to content

Commit 6bf15b9

Browse files
committed
fix(review): lazy init
1 parent 0e75258 commit 6bf15b9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,6 @@ final class BDKService {
562562
throw WalletError.sweepEsploraOnly
563563
}
564564

565-
let destinationAddress = try getAddress()
566-
let destinationScript = try Address(address: destinationAddress, network: self.network)
567-
.scriptPubkey()
568-
569565
let candidates = [
570566
"pkh(\(wif))",
571567
"wpkh(\(wif))",
@@ -575,6 +571,9 @@ final class BDKService {
575571

576572
var sweptTxids: [Txid] = []
577573
var lastWIFOperationError: Error?
574+
575+
var destinationScript: Script?
576+
578577
for descriptorString in candidates {
579578
guard
580579
let descriptor = try? Descriptor(
@@ -607,6 +606,16 @@ final class BDKService {
607606
continue
608607
}
609608

609+
if destinationScript == nil {
610+
let destinationAddress = try getAddress()
611+
destinationScript = try Address(address: destinationAddress, network: self.network)
612+
.scriptPubkey()
613+
}
614+
615+
guard let destinationScript else {
616+
throw WalletError.noSweepableFunds
617+
}
618+
610619
do {
611620
let psbt = try TxBuilder()
612621
.drainTo(script: destinationScript)

0 commit comments

Comments
 (0)