Skip to content

feat(swift-sdk): make wallet deletion asynchronous#4170

Draft
llbartekll wants to merge 1 commit into
v4.1-devfrom
codex/async-wallet-deletion
Draft

feat(swift-sdk): make wallet deletion asynchronous#4170
llbartekll wants to merge 1 commit into
v4.1-devfrom
codex/async-wallet-deletion

Conversation

@llbartekll

@llbartekll llbartekll commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • make PlatformWalletManager.deleteWallet async so SwiftData and Keychain deletion no longer block the main actor
  • run app-facing persistence sweeps through an awaitable serial-queue bridge while keeping Rust/in-memory mutations actor-isolated
  • reject create, load, or overlapping delete operations while wallet deletion is in progress
  • update the Swift example caller and add regression coverage for main-actor responsiveness and concurrent deletion rejection

Testing

  • added focused WalletDeletionTests coverage for the async persistence barrier and overlapping deletion
  • verified the downstream dashpay arm64 Simulator build with the async API
  • the downstream dashwallet test scheme remains blocked by the local watchOS runtime mismatch (23S303 runtime vs 23T570 SDK)

Summary by CodeRabbit

  • Improvements
    • Wallet deletion is now asynchronous, helping keep the app responsive during cleanup.
    • Wallet creation and loading are temporarily unavailable while deletion is in progress, preventing conflicting actions.
    • Deletion now completes in an orderly sequence, removing associated wallet data and stored credentials when appropriate.
    • The wallet detail screen and related flows now support the updated deletion process.

@github-actions github-actions Bot added this to the v4.1.0 milestone Jul 20, 2026
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Wallet deletion APIs now run asynchronously through the persistence serial queue, sequence Keychain and SwiftData cleanup phases, and block wallet creation or loading during deletion. The example app and deletion tests now await the updated APIs.

Changes

Wallet deletion

Layer / File(s) Summary
Async persistence queue
packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift
Persistence lookup and deletion APIs dispatch work asynchronously on the existing serial queue and propagate failures.
Deletion orchestration and concurrency guard
packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift
Deletion becomes async throws, sequences Keychain and SwiftData cleanup, logs phases, and blocks creation/loading while deletion is active.
Async caller integration and deletion coverage
packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/WalletDetailView.swift, packages/swift-sdk/SwiftExampleApp/SwiftExampleAppTests/WalletDeletionTests.swift
The wallet detail flow awaits deletion, while tests cover asynchronous deletion, idempotence, actor responsiveness, and preservation of sibling records.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant WalletDetailView
  participant PlatformWalletManager
  participant PlatformWalletPersistenceHandler
  participant Keychain
  WalletDetailView->>PlatformWalletManager: await deleteWallet(walletId)
  PlatformWalletManager->>PlatformWalletPersistenceHandler: await identityIdsForWallet(walletId)
  PlatformWalletManager->>Keychain: delete wallet and identity keys
  PlatformWalletManager->>PlatformWalletPersistenceHandler: await deleteWalletData(walletId)
  PlatformWalletManager->>Keychain: delete mnemonic and metadata if no rows remain
  PlatformWalletManager-->>WalletDetailView: deletion result
Loading

Possibly related PRs

Suggested reviewers: shumkov, quantumexplorer, zocolini

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 43.48% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the primary change: wallet deletion is now asynchronous in the Swift SDK.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/async-wallet-deletion

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@thepastaclaw

thepastaclaw commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

⛔ Blockers found — Sonnet deferred (commit ed72039)
Canonical validated blockers: 1

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift (1)

1012-1042: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Remove the Rust wallet before the Keychain sweep.
identityIds is captured before platform_wallet_manager_remove_wallet, so any identity/key material written while deletion is awaiting the Keychain task can miss the purge and remain in Keychain after the wipe completes.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift`
around lines 1012 - 1042, Reorder the deletion flow so
platform_wallet_manager_remove_wallet executes before capturing identityIds and
starting the detached Keychain cleanup task. Keep the existing wallet removal
and in-memory cleanup behavior, then perform the Keychain sweep using
identityIds obtained after the Rust wallet removal completes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift`:
- Around line 1012-1042: Reorder the deletion flow so
platform_wallet_manager_remove_wallet executes before capturing identityIds and
starting the detached Keychain cleanup task. Keep the existing wallet removal
and in-memory cleanup behavior, then perform the Keychain sweep using
identityIds obtained after the Rust wallet removal completes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6aa2d6b9-d07f-4db7-934a-225c9221425e

📥 Commits

Reviewing files that changed from the base of the PR and between c311c7e and ed72039.

📒 Files selected for processing (4)
  • packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift
  • packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift
  • packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/WalletDetailView.swift
  • packages/swift-sdk/SwiftExampleApp/SwiftExampleAppTests/WalletDeletionTests.swift

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preliminary review — Codex only

The async persistence bridge preserves serial SwiftData access and keeps the main actor responsive, but wallet deletion now suspends while independently retained Rust wallet handles remain operational. Those handles can persist new state after the final sweep, so the deletion lifecycle requires an FFI-level quiescence barrier before this PR is safe. The manager-level reentrancy gate is also not exercised by the added tests.

Validated blockers were found in the Codex precheck. Sonnet is deferred until a fresh Codex revalidation clears the blocker gate.

Review provenance

  • Codex reviewers: gpt-5.6-sol — general (failed), gpt-5.6-sol — ffi-engineer (failed), gpt-5.6-sol — general (failed), gpt-5.6-sol — ffi-engineer (failed), gpt-5.6-sol — general (failed), gpt-5.6-sol — ffi-engineer (failed), gpt-5.6-sol — general (completed), gpt-5.6-sol — ffi-engineer (completed)
  • Verifier: gpt-5.6-sol — verifier
  • Sonnet: not run (deferred by blocker gate)

🔴 1 blocking | 🟡 1 suggestion(s)

🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift`:
- [BLOCKING] packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift:1013-1021: Quiesce child FFI handles before awaiting deletion
  Deletion now yields before removing the wallet from `wallets`, allowing another main-actor turn to obtain its `ManagedPlatformWallet`; previously retained wrappers are also unaffected by the manager gate. Each wrapper owns an independent handle whose Rust `Arc<PlatformWallet>` remains in `PLATFORM_WALLET_STORAGE`, and `platform_wallet_manager_remove_wallet` removes only the manager's references. Because `ManagedPlatformWallet` operations run through these handles and several async operations persist changes through the same callback-backed persister, an operation started from a stale handle can reach the synchronous persistence callbacks after `deleteWalletData` has completed and recreate wallet rows after deletion reports success. Add a shared FFI deletion/quiescence state that rejects new child-handle operations and waits for already-running operations before the final persistence sweep; removing the published Swift wrapper before the first await reduces exposure but does not protect wrappers already retained elsewhere.
- [SUGGESTION] packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift:210-214: Add coverage for the manager-level deletion gate
  The new `walletDeletionInProgress` flag and its checks in both `createWallet` overloads, `loadFromPersistor`, and `deleteWallet` are the only protection against manager actor reentrancy while deletion is suspended. The added tests call `PlatformWalletPersistenceHandler.deleteWalletData` directly, and the idempotence test performs two completed deletions sequentially, so removing `beginWalletDeletion` or any create/load guard would not fail the suite. Add a manager-level test that blocks the first deletion on the persistence queue, verifies that an overlapping delete and create/load attempts are rejected, then releases the first deletion and verifies the gate is cleared.

Comment on lines +1013 to +1021
let identityIds = try await persistenceHandler.identityIdsForWallet(
walletId: walletId)

try await Task.detached(priority: .userInitiated) {
for identityId in identityIds {
try KeychainManager.shared.deleteAllKeychainItems(forIdentityId: identityId)
}
try KeychainManager.shared.deleteAllIdentityPrivateKeys(forWalletId: walletId)
}.value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocking: Quiesce child FFI handles before awaiting deletion

Deletion now yields before removing the wallet from wallets, allowing another main-actor turn to obtain its ManagedPlatformWallet; previously retained wrappers are also unaffected by the manager gate. Each wrapper owns an independent handle whose Rust Arc<PlatformWallet> remains in PLATFORM_WALLET_STORAGE, and platform_wallet_manager_remove_wallet removes only the manager's references. Because ManagedPlatformWallet operations run through these handles and several async operations persist changes through the same callback-backed persister, an operation started from a stale handle can reach the synchronous persistence callbacks after deleteWalletData has completed and recreate wallet rows after deletion reports success. Add a shared FFI deletion/quiescence state that rejects new child-handle operations and waits for already-running operations before the final persistence sweep; removing the published Swift wrapper before the first await reduces exposure but does not protect wrappers already retained elsewhere.

source: ['codex']

Comment on lines +210 to +214
/// Async deletion yields while the persistence handler sweeps SwiftData.
/// Reject create/load/another delete during that window so actor
/// reentrancy cannot recreate a deterministic wallet id that the in-flight
/// deletion is about to remove from persistence.
private var walletDeletionInProgress = false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Suggestion: Add coverage for the manager-level deletion gate

The new walletDeletionInProgress flag and its checks in both createWallet overloads, loadFromPersistor, and deleteWallet are the only protection against manager actor reentrancy while deletion is suspended. The added tests call PlatformWalletPersistenceHandler.deleteWalletData directly, and the idempotence test performs two completed deletions sequentially, so removing beginWalletDeletion or any create/load guard would not fail the suite. Add a manager-level test that blocks the first deletion on the persistence queue, verifies that an overlapping delete and create/load attempts are rejected, then releases the first deletion and verifies the gate is cleared.

source: ['codex']

@llbartekll
llbartekll marked this pull request as draft July 21, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants