From f305b9862c0858d3be4dcfd42b2a92547eaa98fe Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 7 Aug 2026 23:44:55 +0700 Subject: [PATCH 1/2] feat(wallet): fund sends from every spendable account MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adopts `.allSpendable` (platform #4329) on both send paths, replacing the explicit `.bip44` pin. The home balance has always been wallet-wide — `refreshBalanceBridge` publishes `coreWallet().balance()` and the initial seed uses `getWalletBalance(walletId:)`, neither of which is per-account — while sends could only spend BIP44. A user holding funds on BIP32 or a DashPay contact-receiving account saw them in the total and got "insufficient funds" trying to spend them. `maxSendable`, derived from that same balance, was over-optimistic for the same reason; it is now correct. `.allSpendable` resolves to SEND_FUNDING_SOURCES — BIP44 + BIP32 + AllDashpayReceivingFunds. CoinJoin is excluded by construction: spending mixed outputs beside transparent ones links them and undoes the mixing. A contact's watch-only external coins are excluded too — the selector takes only the receiving side the local seed can sign. Change returns to BIP44, the first pooled source. The MAYA swap deposit takes the same funding. `changeToFirstInput` is unaffected by pooling: it routes to whichever input BIP-69 puts at VIN0 regardless of source account, and the builder already sizes the change output for the largest eligible routing script. Its shape assertion still holds because every pooled source is P2PKH; the comment claiming BIP44 is the only possible source is corrected, and now records that VIN0 decides the MAYA refund address, which under pooling can be a DashPay receiving address — still this wallet's own seed-signable address, still counted in its balance. Two call sites keep their explicit pins: the CoinJoin sweep must drain exactly that account, and the CrowdNode selected-input send must keep inputs and change on the address CrowdNode identifies the user by. Requires a DashSDKFFI.xcframework rebuilt from platform v4.2-dev at or past #4329, which adds CORE_ACCOUNT_TYPE_FFI_ALL_SPENDABLE. Co-Authored-By: Claude Fable 5 --- .../SwiftDashSDKTransactionSender.swift | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift b/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift index aa1c5f8c76..a9209b3b52 100644 --- a/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift +++ b/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift @@ -98,7 +98,7 @@ final class SwiftDashSDKTransactionSender: NSObject { let network = SwiftDashSDKHost.shared.runningNetwork else { throw SendError.walletNotReady("PlatformWalletManager wallet is not available") } - // Build + sign a standard BIP44 payment via the core + // Build + sign a standard payment via the core // TransactionBuilder. `finalizeAtomic` selects + reserves the // inputs and routes change to the account's next internal address // in one native operation (single tx — normal sends don't need @@ -107,7 +107,14 @@ final class SwiftDashSDKTransactionSender: NSObject { for recipient in recipients { try builder.addOutput(address: recipient.address, amountDuffs: recipient.amountDuffs) } - return try builder.finalizeAtomic(wallet: wallet, accountType: .bip44, accountIndex: 0) + // `.allSpendable` pools BIP44 + BIP32 + every DashPay + // contact-receiving account — the same set the home balance + // already totals, so a send can spend everything the user is + // shown. CoinJoin is excluded by construction (spending mixed + // outputs beside transparent ones would undo the mixing), as are + // a contact's watch-only external coins. Change returns to BIP44, + // the first pooled source. + return try builder.finalizeAtomic(wallet: wallet, accountType: .allSpendable) } let tx: FinalizedCoreTransaction @@ -153,7 +160,12 @@ final class SwiftDashSDKTransactionSender: NSObject { try builder.addOpReturn(memoData) try builder.preserveOutputOrder() try builder.changeToFirstInput() - let tx = try builder.finalizeAtomic(wallet: wallet, accountType: .bip44, accountIndex: 0) + // Same pooled funding as a plain send — see `buildAndSign`. + // `changeToFirstInput` stays correct under pooling: it routes to + // whichever input BIP-69 puts at VIN0, whatever account it came + // from, and the builder sizes the change output for the largest + // eligible routing script. + let tx = try builder.finalizeAtomic(wallet: wallet, accountType: .allSpendable) return (tx, network) } @@ -618,10 +630,13 @@ final class SwiftDashSDKTransactionSender: NSObject { } if decoded.outputs.count == 3 { - // `DecodedTransaction.Input.address` is recovered from a P2PKH-shaped scriptSig and - // is nil for anything else. Every BIP44 account UTXO this builder can spend is - // P2PKH, so nil here means the transaction is not the shape we asked for — refuse - // rather than skip the check. + // `DecodedTransaction.Input.address` is recovered from a P2PKH-shaped scriptSig + // and is nil for anything else. Every UTXO the pooled funding set can spend — + // BIP44, BIP32, and DashPay contact-receiving — is P2PKH, so nil here means the + // transaction is not the shape we asked for: refuse rather than skip the check. + // Note VIN0 decides where MAYA sends a refund, so under pooling that can be a + // DashPay receiving address rather than a BIP44 one. Still this wallet's own + // seed-signable address, and still counted in its balance. guard let inputAddress = decoded.inputs.first?.address else { throw SendError.invalidInput("swap deposit VIN0 address could not be recovered") } From f8c03da6135e86d68dda3fdcf1766bfc98804130 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 7 Aug 2026 23:54:00 +0700 Subject: [PATCH 2/2] fix(wallet): size the Max-send reserve off the pooled funding set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up, and a regression this PR introduced. maxSendFeeReserveDuffs() enumerated BIP44 account 0 only. That matched the old BIP44-pinned send exactly, but once finalizeAtomic pools BIP44 + BIP32 + DashPay receiving, a real send can carry inputs the reserve never counted — more inputs, larger transaction, higher fee than reserved. feeAwareMaxSendable() subtracts that reserve from a wallet-wide balance, so Max could price itself above what the builder can fund and fail to build. The +50 % margin absorbs jitter, not a systematic undercount: BIP44 with 2 UTXOs and a DashPay account with 30 reserves for 2 inputs and spends 32. The reserve now enumerates the same set `.allSpendable` spends — BIP44 and BIP32 at the funding index plus every DashPay receiving account — and still excludes CoinJoin and a contact's watch-only external coins (tag 13), which the builder cannot sign. The flat-reserve fallback and the safety margin are unchanged. Co-Authored-By: Claude Fable 5 --- .../SwiftDashSDKTransactionSender.swift | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift b/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift index a9209b3b52..72ae9232ba 100644 --- a/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift +++ b/DashWallet/Sources/Infrastructure/SwiftDashSDK/SwiftDashSDKTransactionSender.swift @@ -60,6 +60,13 @@ final class SwiftDashSDKTransactionSender: NSObject { private static let bip44TypeTag: UInt8 = 0 /// `AccountBalance.standardTag` discriminant for BIP44 within Standard. private static let bip44StandardTag: UInt8 = 0 + /// `StandardAccountTypeTagFFI.Bip32` — the other half of the Standard + /// family that `.allSpendable` pools. + private static let bip32StandardTag: UInt8 = 1 + /// `AccountTypeTagFFI.DashpayReceivingFunds`. Its sibling tag 13 + /// (`DashpayExternalAccount`) is a contact's watch-only coins and is NOT + /// pooled — the local seed cannot sign them. + private static let dashpayReceivingTypeTag: UInt8 = 12 /// Signal sends spend from the primary BIP44 account. private static let bip44AccountIndex: UInt32 = 0 /// How long to wait for a just-broadcast funding output to appear in the @@ -452,7 +459,7 @@ final class SwiftDashSDKTransactionSender: NSObject { } /// Fee reserve for a "Max" / all-funds core send, sized from the BIP44 - /// account's actual spendable-UTXO count instead of a flat constant. + /// pooled accounts' actual spendable-UTXO count instead of a flat constant. /// /// The flat `WalletBalance.sendFeeReserveDuffs` (100_000 duffs) is a large /// worst-case over-estimate — a typical send costs ~1–2k duffs, so Max used @@ -461,23 +468,39 @@ final class SwiftDashSDKTransactionSender: NSObject { /// inputs) plus a **+50 % safety margin** so it can never *under*-reserve — /// an under-estimate would make the Max send fail to build. /// - /// Fail-safe: any inability to enumerate the account (no wallet/manager, no - /// UTXOs) falls back to the flat reserve, i.e. the previous behaviour — the - /// change never makes Max worse than before, only tighter when it can. + /// The enumerated set must stay in step with the `.allSpendable` funding + /// `finalizeAtomic` uses: BIP44 + BIP32 at the funding index plus every + /// DashPay receiving account, and never CoinJoin or a contact's watch-only + /// external coins. + /// + /// Fail-safe: any inability to enumerate the accounts (no wallet/manager, + /// no UTXOs) falls back to the flat reserve, i.e. the previous behaviour — + /// the change never makes Max worse than before, only tighter when it can. static func maxSendFeeReserveDuffs() -> UInt64 { let read = { @MainActor () -> UInt64? in let host = SwiftDashSDKHost.shared guard let manager = host.manager, let wallet = host.wallet else { return nil } let walletId = wallet.walletId - // `typeTag == 0` is the whole Standard family; account 0 can exist as - // both BIP44 (`standardTag == 0`) and BIP32 (`standardTag == 1`), so - // require the standard tag too — mirrors `addressUtxos(script:)`. - guard let bip44 = manager.accountBalances(for: walletId).first(where: { - $0.typeTag == Self.bip44TypeTag - && $0.standardTag == Self.bip44StandardTag - && $0.index == Self.bip44AccountIndex - }) else { return nil } - let count = manager.accountUtxos(for: walletId, balance: bip44).count + // Enumerate exactly what `.allSpendable` spends — BIP44 + BIP32 at + // the funding index, plus every DashPay receiving account. Sizing + // the reserve off BIP44 alone would under-reserve whenever the + // other sources contribute inputs, and Max would then price itself + // above what the builder can actually fund. + let pooled = manager.accountBalances(for: walletId).filter { balance in + if balance.typeTag == Self.bip44TypeTag { + // Standard family: BIP44 and BIP32 both resolve to the + // single account at the funding index. + let isStandardPooled = balance.standardTag == Self.bip44StandardTag + || balance.standardTag == Self.bip32StandardTag + return isStandardPooled && balance.index == Self.bip44AccountIndex + } + // Every DashPay receiving account, whatever its index. + return balance.typeTag == Self.dashpayReceivingTypeTag + } + guard !pooled.isEmpty else { return nil } + let count = pooled.reduce(0) { total, balance in + total + manager.accountUtxos(for: walletId, balance: balance).count + } guard count > 0 else { return nil } let base = estimatedSignalFee(inputCount: count) return base + base / 2 // +50 % margin — must never under-reserve