Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -37,6 +37,11 @@ final class WalletLifecycleTransitionState: ObservableObject {
case switchingWallet(targetName: String?)
/// Per-wallet removal in flight (`deleteLogicalWallet`).
case removingWallet
/// Add-wallet provisioning in flight (`SwiftDashSDKHost.addWallet` —
/// blocking wallet creation on the current network plus the other
/// network's mirror). `isImport` picks the card copy
/// (Creating/Importing wallet…).
case addingWallet(isImport: Bool)
/// The wallet switch failed. The previous wallet is NOT guaranteed
/// active afterwards (the registry is repointed before the rebuild
/// and the host may have bound a fallback wallet), so the card
Expand Down Expand Up @@ -64,6 +69,7 @@ final class WalletLifecycleTransitionState: ObservableObject {
case .failedNetworkSwitch(_, let target, _): return "failedNetworkSwitch(\(target))"
case .switchingWallet: return "switchingWallet"
case .removingWallet: return "removingWallet"
case .addingWallet: return "addingWallet"
case .failedWalletSwitch: return "failedWalletSwitch"
case .failedWalletRemoval: return "failedWalletRemoval"
case .wiping: return "wiping"
Expand Down Expand Up @@ -91,9 +97,16 @@ final class WalletLifecycleTransitionState: ObservableObject {
case (.idle, .switchingNetwork),
(.idle, .switchingWallet),
(.idle, .removingWallet),
(.idle, .addingWallet),
(.idle, .wiping),
(.failedNetworkSwitch, .switchingNetwork),
(.failedWalletSwitch, .switchingWallet),
// Composite add → switch: the add flow's own continuation into
// its post-add switch, without dropping the window through idle.
// Deliberately not owner-scoped — in practice only the add flow
// can reach it, because the blocking overlay window covers every
// other interactive entry point for `.addingWallet`'s lifetime.
(.addingWallet, .switchingWallet),
// The wipe/reset route stays reachable from EVERY failure card:
// without this, a persistently failing switch (destination
// network down) would block the whole app behind the overlay
Expand Down
10 changes: 9 additions & 1 deletion DashWallet/Sources/UI/Main/WalletLifecycleOverlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class WalletLifecycleOverlayPresenter {
overlayWindow?.isHidden = true
overlayWindow = nil
case .switchingNetwork, .failedNetworkSwitch,
.switchingWallet, .removingWallet,
.switchingWallet, .removingWallet, .addingWallet,
.failedWalletSwitch, .failedWalletRemoval,
.wiping:
presentIfNeeded()
Expand Down Expand Up @@ -189,6 +189,14 @@ struct WalletLifecycleOverlayView: View {
progressCard(
title: NSLocalizedString("Removing wallet…", comment: "Wallets"),
subtitle: nil)
case let .addingWallet(isImport):
progressCard(
title: isImport
? NSLocalizedString("Importing wallet…", comment: "Wallets")
: NSLocalizedString("Creating wallet…", comment: "Wallets"),
subtitle: NSLocalizedString(
"Preparing your wallet. This may take a few seconds.",
comment: "Wallets"))
case let .wiping(title):
// Falls back to the Delete All copy — the same key the root
// controller's HUD used, so translations carry over.
Expand Down
38 changes: 38 additions & 0 deletions DashWallet/Sources/UI/Menu/Security/Wallets/WalletsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,28 +265,66 @@ final class WalletsViewModel: ObservableObject {
addInProgress = true
defer { addInProgress = false }

// Blocking overlay from the FIRST moment: provisioning is seconds of
// blocking MainActor work (wallet creation FFI on both networks, plus
// the other network's SDK build), and without this phase the app
// looked frozen until the post-add switch finally raised the window.
WalletLifecycleOverlayPresenter.shared.ensureActive()
let state = WalletLifecycleTransitionState.shared
guard state.tryBegin(.addingWallet(isImport: isImported)) else {
errorMessage = SwiftDashSDKWalletRuntime.SwitchError.switchInProgress.localizedDescription
return nil
}
// Give UIKit one runloop turn to commit the overlay window before the
// blocking provisioning starts (the Obj-C wipe's 0.1 s dispatch_after
// trick): the phase-apply Task was already enqueued by the sink, and
// both it and the CoreAnimation commit run while this sleeps.
try? await Task.sleep(for: .milliseconds(100))

let opID = String(UUID().uuidString.prefix(8))
let started = CFAbsoluteTimeGetCurrent()
DWLogger.log("🔁 WALLETOP [\(opID)] add begin import=\(isImported)")

let result: SwiftDashSDKHost.AddWalletResult
do {
result = try await SwiftDashSDKHost.shared.addWallet(
mnemonic: normalized,
isImported: isImported)
} catch {
// The ONE exit where a forgotten finish() would strand the
// blocking overlay with no owner.
state.finish()
let ms = Int((CFAbsoluteTimeGetCurrent() - started) * 1000)
DWLogger.log("🔁 WALLETOP [\(opID)] add FAILED after \(ms)ms: \(error.localizedDescription)")
Self.logger.error("addWallet failed: \(String(describing: error), privacy: .public)")
errorMessage = error.localizedDescription
return nil
}
let ms = Int((CFAbsoluteTimeGetCurrent() - started) * 1000)
DWLogger.log("🔁 WALLETOP [\(opID)] add done in \(ms)ms")

switch result {
case .alreadyExists(let walletId):
state.finish()
return .alreadyOnDevice(walletId: walletId)
case .added(let walletId):
switchInProgress = true
do {
// Admitted from `.addingWallet` by the composite rule — the
// window advances Creating → Switching without dropping
// through idle; from here the helper owns finish/fail.
try await Self.gatedSwitchWallet(
targetId: walletId,
targetName: Self.displayName(for: walletId))
} catch {
switchInProgress = false
// Defense in depth: a rejected admission (throws BEFORE any
// fail-phase is set) must not strand `.addingWallet`; a real
// switch failure has already moved to `.failedWalletSwitch`,
// which this deliberately leaves alone.
if case .addingWallet = state.phase {
state.finish()
}
Self.logger.error("post-add switch failed: \(String(describing: error), privacy: .public)")
// The wallet was added but the switch failed; leave it on
// device — the overlay's blocking card owns recovery (Retry /
Expand Down
19 changes: 15 additions & 4 deletions DashWalletTests/WalletLifecycleTransitionStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class WalletLifecycleTransitionStateTests: XCTestCase {
("switchingNetwork", .switchingNetwork(from: .mainnet, to: .testnet)),
("switchingWallet", .switchingWallet(targetName: "A")),
("removingWallet", .removingWallet),
("addingWallet", .addingWallet(isImport: false)),
("wiping", .wiping(title: nil)),
]

Expand Down Expand Up @@ -56,18 +57,28 @@ final class WalletLifecycleTransitionStateTests: XCTestCase {
}
}

/// No begin is admitted while any operation is busy.
/// No begin is admitted while any operation is busy — except the add
/// flow's own composite continuation into its post-add switch.
func testBusyPhasesRejectEveryBegin() {
for (busyLabel, busy) in Self.begins {
for (nextLabel, next) in Self.begins {
let state = makeState(in: busy)
XCTAssertFalse(
state.tryBegin(next),
"\(nextLabel) must be rejected while \(busyLabel) is in flight")
let expected = busyLabel == "addingWallet" && nextLabel == "switchingWallet"
XCTAssertEqual(
state.tryBegin(next), expected,
"\(busyLabel) → \(nextLabel): expected admitted=\(expected)")
}
}
}

/// The add flow's window advances Creating → Switching without dropping
/// through idle.
func testAddFlowCompositeAdvancesToSwitch() {
let state = makeState(in: .addingWallet(isImport: false))
XCTAssertTrue(state.tryBegin(.switchingWallet(targetName: "A")))
XCTAssertEqual(state.phase, .switchingWallet(targetName: "A"))
}

/// Failure phases admit exactly their own retry — and a wipe.
func testFailurePhaseAdmissions() {
let retryOf: [String: String] = [
Expand Down
Loading