Skip to content

feat(swift-sdk): wallet presence flag readable while the device is locked - #4992

Closed
romchornyi wants to merge 4 commits into
v5.2-devfrom
feat/swift-sdk-wallet-presence-marker
Closed

romchornyi wants to merge 4 commits into
v5.2-devfrom
feat/swift-sdk-wallet-presence-marker

Conversation

@romchornyi

@romchornyi romchornyi commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Issue being fixed or feature implemented

WalletStorage stores mnemonics and metadata as kSecAttrAccessibleWhenUnlockedThisDeviceOnly. A process launched while the device is locked (a BGAppRefresh launch) cannot enumerate them: listWalletIdsWithMnemonic() throws, and an app that folds that error into false concludes "no wallet" and shows its Create/Recover screen — the defect behind dashpay/dashwallet-ios#1147. The review of dashpay/dashwallet-ios#1088 promised a "non-secret wallet marker" follow-up in the SDK; this is that follow-up, as a UserDefaults flag.

App-side adoption (calling the new API, SDK pin bump) is a separate dashwallet-ios PR.

What was done?

All in packages/swift-sdk/Sources/SwiftDashSDK/Core/Wallet/WalletStorage.swift:

  • Presence flag: one boolean in UserDefaults.standard under WalletStorage.presenceDefaultsKey (= <keychainService>.walletPresent, so the DASH_KEYCHAIN_SERVICE override still applies). No wallet id, no secret. Readable while the device is locked once it has been unlocked since boot.
  • Coupled to the mnemonic. The flag goes on last and comes off first.
    • storeMnemonic runs in this order: clear the flag, remove the old mnemonic, add the new mnemonic, set the flag.
    • deleteMnemonic clears the flag, removes the mnemonic, and sets the flag again if other mnemonics remain.
    • An interruption or a throw between any two steps therefore leaves at most a mnemonic without the flag, which the next readable presence read repairs, and never the flag without a mnemonic.
    • storeMnemonic, deleteMnemonic and walletPresence() share one static lock, so they are serialised across every WalletStorage instance in the process (including PlatformWalletManager.deleteWallet's own instance). A presence read cannot set the flag in the middle of a deletion.
    • Every existing write/delete path goes through these two functions. cleanupLegacyItems is unaffected.
  • Presence API: WalletStorage.WalletPresence { present, absent, unknown(OSStatus) } and walletPresence(). The flag accessors readPresenceFlag() / writePresenceFlag(_:) are internal lifecycle steps, not SDK operations.
    • If the mnemonic inventory is readable, it is the source of truth: non-empty means .present, empty means .absent, and the flag is rewritten to match (the lazy backfill for wallets stored before this change, and after a reinstall, where the keychain survives and UserDefaults does not).
    • If the inventory is unreadable, the flag stands in: set means .present, unset means .unknown(status). Nothing is written then.
    • Invariant: a missing flag on its own never means "no wallet"; only a readable, empty inventory does.
  • Why UserDefaults and not a keychain item: the first presence read after any (re)install runs with the inventory readable — iOS does not launch an app in the background until the user has opened it, which takes an unlocked device — so the flag is always backfilled before a locked launch can need it. Surviving a reinstall buys nothing. Before the first unlock since boot UserDefaults reads empty, which gives .unknown, never .absent. A backup restored to another device carries the flag but not the ThisDeviceOnly mnemonic; that gives at most a stale .present on a locked launch (a deferral, not a setup screen), and the next unlocked read clears it.

Not covered on purpose: the legacy DashSync wallet.

How Has This Been Tested?

  • packages/swift-sdk/SwiftTests/SwiftDashSDKTests/WalletPresenceTests.swift (12 tests). They run the real storeMnemonic / deleteMnemonic / walletPresence() bodies against an in-memory fake keychain and flag shared by two WalletStorage instances. Covered:
    • classification: flag + unreadable inventory → present with no writes; no flag + unreadable inventory → unknown, not absent, with no writes; readable empty inventory → absent and a stale flag is cleared; readable non-empty inventory → present and the flag is backfilled;
    • ordering and interruption: store step order; a failed mnemonic add during a rewrite leaves no flag; deleting the last wallet clears the flag; deleting one of two keeps it; a failed mnemonic delete leaves an unflagged mnemonic rather than a phantom, and the next unlocked read repairs it;
    • interleaving: a presence read on a second instance waits for a paused deletion and ends absent with no flag;
    • the real flag: a round trip through UserDefaults.standard (cleared, not stored as false) and the key's derivation from keychainService.
  • Ran locally on macOS: bash build_ios.sh --target tests --profile dev, then swift test → 732 unit tests, 0 failures (3 skipped; the integration target's 14 tests are env-gated). The xcodebuild SwiftExampleApp simulator test run of run_tests.sh was not executed locally.

Breaking Changes

None. Existing APIs keep their signatures and semantics; storeMnemonic / deleteMnemonic additionally set / clear one UserDefaults boolean. Wallets stored before this change get the flag on the first walletPresence() call made while the inventory is readable.

Overlap note: #4615 (v4.2-dev) restructures the same storeMnemonic / deleteMnemonic bodies into shared primitives; when it is forward-merged the flag calls need to be carried into the new shape (a small textual conflict, no semantic one).

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed
  • If I added or changed GroveDB structure, I described it in the area's structure.rs, regenerated grovedb-structure.json, and checked the structure viewer link posted on this pull request

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

PR Hygiene · 242e382

  • Bots — thepastaclaw reviewed an earlier commit, not yet this one — /skip-bots proceeds without the ones not yet reported
  • Self-review — post /self-reviewed
  • Build running
  • Approvals — you own every area touched; none needed

When every merge requirement is met, the PR Hygiene check passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.

…vice is locked

The mnemonic and metadata items are WhenUnlockedThisDeviceOnly, so a
process launched while the device is locked (background app refresh)
cannot enumerate them; `listWalletIdsWithMnemonic()` throws and an app
that folds that into `false` concludes "no wallet" and shows its setup
screen.

Add a non-secret per-wallet presence marker: a generic-password item at
`wallet.present.<hex-walletId>` under the sibling service
`<keychainService>.presence`, AfterFirstUnlockThisDeviceOnly, payload =
the wallet id. It is written inside `storeMnemonic` and removed inside
`deleteMnemonic`, so every write and delete path (including the
manager's delete-wallet path) keeps the two in step.

`walletPresence()` answers present / absent / unknown(OSStatus): a
non-empty marker set is `present`; an empty one falls back to the
mnemonic inventory, marking every wallet it finds (lazy backfill for
wallets stored before the marker existed) and answering `absent` only
on a readable empty inventory; any read failure is `unknown`.
`markedWalletIds()` exposes the raw marker set.

The marker lives in its own service because a service-wide query over
a mix of AfterFirstUnlock and WhenUnlocked items on a locked device
either fails as a whole (marker unreadable when needed) or drops the
locked rows (the existing inventory would read empty instead of
throwing).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 49 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0fc32ab and 242e382.

Files selected for processing: 2

  • packages/swift-sdk/Sources/SwiftDashSDK/Core/Wallet/WalletStorage.swift
  • packages/swift-sdk/SwiftTests/SwiftDashSDKTests/WalletPresenceTests.swift
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@thepastaclaw

thepastaclaw commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

🔍 Review in progress (commit 242e382) · live progress

  • Request priority review — click to give the rest of this review priority.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Sep 24, 2026
@github-actions github-actions Bot added this to the v4.3.0 milestone Sep 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

@thepastaclaw review

No review for 4c6051f0 yet, so PR Hygiene is asking once. If nothing arrives, the requirement is dropped for this commit and the pull request is labelled bot-review-skipped.

@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.

Final validation — Phase 1 + Phase 2

Verified the supplied findings against head 4c6051f, the changed tests, and surrounding callers. Interrupted writes and concurrent deletion/backfill can leave persistent false-positive wallet markers, and the public marker mutators bypass the documented lifecycle invariant. These are classified as suggestions under the supplied policy for non-consensus correctness issues; verification was source-based, without independently rerunning the reported harnesses.

🟡 3 suggestion(s)

Review provenance

Source: reviewer 1: gemini-3.8-flash-high (agent: phase1-reviewer, role: general); reviewer 2: gemini-3.8-flash-high (agent: phase1-reviewer, role: architecture-layering); reviewer 3: gemini-3.8-flash-high (agent: phase1-reviewer, role: security-auditor); reviewer 4: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 5: gpt-6-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 6: gpt-6-astra (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6-astra (agent: astra-verifier, role: final-verifier)

  • Triage: critical by gpt-6-astra (effort low) — The substantial and intricate change modifies WalletStorage keychain handling and mnemonic write/delete coupling, directly affecting wallet key-material persistence and accessibility semantics.
  • Phase 1 reviewers: gemini-3.8-flash-high — general (completed, effort high); agent phase1-reviewer, gemini-3.8-flash-high — architecture-layering (completed, effort high); agent phase1-reviewer, gemini-3.8-flash-high — security-auditor (completed, effort high); agent phase1-reviewer
  • Phase 1 model: gemini-3.8-flash-high — antigravity quota: weekly 75% left, 5h 58% left
  • Fresh verifier: gpt-6-astra — final-verifier; agent astra-verifier
  • Phase 2 reviewers: gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — security-auditor (completed, effort xhigh); agent phase2-reviewer
🤖 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/Core/Wallet/WalletStorage.swift`:
- [SUGGESTION] packages/swift-sdk/Sources/SwiftDashSDK/Core/Wallet/WalletStorage.swift:109: Persisting the marker first leaves a phantom wallet after interruption
  On a first mnemonic write, this call persists the marker before the mnemonic exists. If the process terminates before the mnemonic's SecItemAdd succeeds, the error-handler cleanup never executes. Subsequent walletPresence() calls return .present from the marker without consulting the empty mnemonic inventory, even after unlocking or restarting. Rewriting an existing mnemonic has the same failure window between its deletion and replacement. An application following the documented rule that only .absent permits setup can therefore suppress Create/Recover indefinitely despite having no stored mnemonic. Make persistence interruption-safe: establish the mnemonic before publishing its marker, and ensure rewrites cannot retain a marker across a delete/add gap. A mnemonic without a marker already has a safe inventory fallback. Add regression coverage for interruption at these persistence boundaries.
- [SUGGESTION] packages/swift-sdk/Sources/SwiftDashSDK/Core/Wallet/WalletStorage.swift:453-464: Backfill can recreate a marker during a successful mnemonic deletion
  The inventory read and marker backfill are not coordinated with mnemonic mutations. With independently created WalletStorage instances, deleteMnemonic() can remove a wallet's marker and pause before removing its mnemonic; walletPresence() can then observe an empty marker list, inventory the still-existing mnemonic, and recreate its marker. Deletion subsequently removes the mnemonic and succeeds, leaving a marker that makes future presence calls return .present without checking the now-empty inventory. WalletStorage has no shared synchronization or actor isolation preventing this schedule, and PlatformWalletManager.deleteWallet creates its own storage instance. Coordinate the complete inventory-read/backfill sequence with mnemonic writes and deletes across storage instances sharing the service. Add an interleaving regression at the Keychain-operation boundary; the current scripted classification tests do not exercise this lifecycle.
- [SUGGESTION] packages/swift-sdk/Sources/SwiftDashSDK/Core/Wallet/WalletStorage.swift:350-355: Keep raw marker mutation out of the public SDK contract
  The new public storePresenceMarker(for:) can create a marker for a wallet ID with no stored mnemonic. walletPresence() then reports .present without consulting the empty mnemonic inventory, violating its documented marker-implies-wallet invariant. The comments explicitly discourage independent marker mutation, and no production caller outside WalletStorage needs these mutators. Keep storePresenceMarker and deletePresenceMarker internal or private lifecycle helpers rather than exposing unsupported independent writes as public SDK operations. Internal visibility preserves the override seam used by the @testable test target without publishing it to applications.

Review follow-up on the wallet presence marker. Three ways a marker
could assert a wallet that has no mnemonic:

- storeMnemonic wrote the marker before the mnemonic, so a process
  killed in between left a phantom; a rewrite kept the marker across
  its delete/add gap. The steps now run marker-off, mnemonic-off,
  mnemonic-on, marker-on: an interruption at any point leaves at most
  a mnemonic without a marker. A failed trailing marker write no
  longer fails the store — the mnemonic is stored and the next
  unlocked presence read backfills the marker.
- The backfill in walletPresence() was an uncoordinated read-then-
  write, so it could re-mark a wallet while deleteMnemonic (on its own
  WalletStorage instance) was between its two steps. storeMnemonic,
  deleteMnemonic and walletPresence() now share one static lock, and
  deleteMnemonic removes the mnemonic before the marker.
- A non-empty marker set was never re-validated. When the inventory
  is readable it is now the source of truth: the verdict comes from
  it and the marker set is rewritten to match, so any phantom heals on
  the first unlocked read. The marker only stands in while the
  inventory is unreadable.

storePresenceMarker / deletePresenceMarker are internal now; they are
lifecycle steps, not SDK operations. The two raw mnemonic keychain
steps are internal primitives too, so the ordering, the interruption
points and the interleaving are covered by tests against an in-memory
keychain shared between two storage instances.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@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.

Re-review — Final validation — Phase 1 + Phase 2

The complete diff at bccb27b addresses all three prior findings: marker mutation is internal, lifecycle operations share a process-wide lock, and mnemonic writes use interruption-safe ordering. One non-consensus correctness suggestion remains: interrupted deletion can leave a stale marker that produces a false presence result while the inventory is unreadable. Verification was based on source, caller, and test inspection; tests were not rerun.

🟡 1 suggestion(s)

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: architecture-layering); reviewer 3: muse-spark-1.3-contributor (agent: phase1-reviewer, role: security-auditor); reviewer 4: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 5: gpt-6-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 6: gpt-6-astra (agent: phase2-reviewer, role: security-auditor); reviewer 7: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 8: gpt-6-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 9: gpt-6-astra (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6-astra (agent: astra-verifier, role: final-verifier)

  • Triage: critical by gpt-6-astra (effort low) — The diff is large and intricate and changes WalletStorage.swift, which handles mnemonic key material and Keychain persistence, a critical key-handling and storage surface.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — architecture-layering (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — security-auditor (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity below 15% reserve: weekly 35% left, 5h 0% left), glm-5.3-flash (not used above high effort; tier asks max)
  • Fresh final gate: an independent Phase-2 review ran after iterative findings were reconciled
  • Fresh verifier: gpt-6-astra — final-verifier; agent astra-verifier
  • Phase 2 reviewers: gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — security-auditor (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — security-auditor (completed, effort xhigh); agent phase2-reviewer
🤖 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/Core/Wallet/WalletStorage.swift`:
- [SUGGESTION] packages/swift-sdk/Sources/SwiftDashSDK/Core/Wallet/WalletStorage.swift:283-284: Deleting the mnemonic before its marker can leave a phantom wallet
  These are independent keychain operations. If the process stops after mnemonic deletion, or the subsequent marker deletion throws, the durable marker survives without its mnemonic. When the mnemonic inventory is unreadable, `walletPresence()` trusts that marker and returns `.present` at line 485. There is a concrete affected caller: the example app's `deleteStoredMnemonics()` skips metadata cleanup when `deleteMnemonic()` throws, leaving protected metadata under the inventory service that can make a later locked inventory query fail even though the mnemonic is gone. Unlocked reconciliation repairs this state, but cannot prevent the false result during a locked launch.

  Remove the marker first under the existing mutation lock, then delete the mnemonic. The lock already prevents same-process backfill between those steps; an interruption then leaves at most an unmarked mnemonic rather than a phantom marker. Update the lifecycle comments and `testDeleteRemovesTheMnemonicBeforeTheMarker`, and add coverage for failure or interruption between the two deletions.

Review follow-up. deleteMnemonic removed the mnemonic first and the
marker second, so a process killed between the two steps, or a marker
delete that threw, left a marker with no mnemonic behind it — and a
locked presence read trusts the marker. The ordering was chosen to
keep a concurrent backfill from re-marking the wallet, which the
shared mutation lock now prevents on its own.

The marker now comes off first: an interruption leaves at most an
unmarked mnemonic, which a locked read reports as unknown and an
unlocked read re-marks. The section and lifecycle comments say so, the
delete-order test is renamed, and two tests cover a throw at either
step between the deletions.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@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.

Re-review — Final validation — Phase 1 + Phase 2

The complete diff and relevant lifecycle callers contain no confirmed in-scope defects; all four prior findings are fixed. Marker ordering, internal mutators, and the shared lock address the interruption and backfill scenarios without weakening mnemonic protection. All 17 WalletPresenceTests passed using the existing XCTest bundle; the full SDK was not rebuilt, and physical-device locked-keychain behavior was not independently exercised.

🔴 0 blocking | 🟡 0 suggestion(s) | 💬 0 nitpick(s)

Review provenance

Source: reviewer 1: gemini-3.8-flash-high (agent: phase1-reviewer, role: general); reviewer 2: gemini-3.8-flash-high (agent: phase1-reviewer, role: architecture-layering); reviewer 3: gemini-3.8-flash-high (agent: phase1-reviewer, role: security-auditor); reviewer 4: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 5: gpt-6-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 6: gpt-6-astra (agent: phase2-reviewer, role: security-auditor); reviewer 7: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 8: gpt-6-astra (agent: phase2-reviewer, role: architecture-layering); reviewer 9: gpt-6-astra (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6-astra (agent: astra-verifier, role: final-verifier)

  • Triage: critical by gpt-6-astra (effort low) — WalletStorage.swift intricately changes mnemonic storage and deletion in storeMnemonic/deleteMnemonic, coupling key-material lifecycle operations to durable marker reconciliation with shared locking, interruption ordering, and partial-failure handling.
  • Phase 1 reviewers: gemini-3.8-flash-high — general (completed, effort high); agent phase1-reviewer, gemini-3.8-flash-high — architecture-layering (completed, effort high); agent phase1-reviewer, gemini-3.8-flash-high — security-auditor (completed, effort high); agent phase1-reviewer
  • Phase 1 model: gemini-3.8-flash-high — antigravity quota: weekly 33% left, 5h 96% left
  • Fresh final gate: an independent Phase-2 review ran after iterative findings were reconciled
  • Fresh verifier: gpt-6-astra — final-verifier; agent astra-verifier
  • Phase 2 reviewers: gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — security-auditor (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — architecture-layering (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — security-auditor (completed, effort xhigh); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify the current code and confirm that no unresolved issues remain.

No unresolved findings remain from the prior review on this head.
Out-of-scope follow-up suggestions (1)

These are valid observations, but they are outside this PR's scope and should be handled in separate issues or author/maintainer-requested PRs rather than blocking this review.

  • Example app metadata cleanup error handling — Routine pre-existing cleanup outside this PR's scope. ContentView.deleteStoredMnemonics() intentionally tolerates metadata deletion failure after successful mnemonic deletion. The current marker-first deletion has already removed the presence marker, so surviving metadata cannot recreate the reported phantom-marker condition.
    • Follow-up: Consider creating a separate issue or author/maintainer-requested PR for this.

@romchornyi

Copy link
Copy Markdown
Contributor Author

/self-reviewed

@github-actions

Copy link
Copy Markdown
Contributor

@coderabbitai review

No review for 3428934e yet, so PR Hygiene is asking once. If nothing arrives, the requirement is dropped for this commit and the pull request is labelled bot-review-skipped.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. and removed waiting-bots Waiting for the review bots to report on this head labels Sep 26, 2026
@romchornyi

Copy link
Copy Markdown
Contributor Author

/self-reviewed

@github-actions github-actions Bot removed waiting-self-review Waiting for the author to post /self-reviewed bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. labels Sep 28, 2026
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Policy satisfied — this can merge.
Full checklist in the description.

@github-actions github-actions Bot added the bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. label Oct 2, 2026
@github-actions github-actions Bot removed the bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. label Oct 27, 2026
@romchornyi romchornyi modified the milestones: v5.1.0, v5.2.0 Nov 12, 2026
@romchornyi
romchornyi changed the base branch from v5.1-dev to v5.2-dev November 13, 2026 09:16

@llbartekll llbartekll 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.

Question on the design: does the marker need to live in the keychain at all? Could it be a plain flag in UserDefaults instead (kept by the app, or written by WalletStorage itself)?

As I understand it, the reason for the keychain is lifecycle: a keychain item survives a reinstall like the mnemonic does, while UserDefaults is wiped. So after a reinstall we would have a mnemonic with no marker.

But I don't see how that state can ever reach the user. iOS does not launch an app in the background until the user has opened it once, and to open it the device has to be unlocked. So the first presence read after an install or reinstall always runs with a readable mnemonic inventory, which is the source of truth and backfills the marker right there. By the time a locked background launch can happen, the marker is already in place.

If that holds, UserDefaults gives the same behaviour with less machinery: no sibling keychain service, and the wallet id is not exposed in an AfterFirstUnlock keychain item.

Is there a concrete scenario where a UserDefaults flag gives the wrong answer and the keychain marker does not? If so, it would be worth stating it in the doc comment on presenceKeychainService.

The keychain marker was chosen to survive a reinstall, but that buys
nothing: iOS does not launch an app in the background until the user has
opened it, so the first presence read after any (re)install runs with the
mnemonic inventory readable and backfills the flag before a locked launch
can need it.

Replace the per-wallet AfterFirstUnlock keychain items (and their sibling
service) with one boolean in UserDefaults that carries no wallet id. The
ordering (flag off first, on last), the shared lock and the reconciliation
from a readable inventory stay as they were.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@romchornyi romchornyi changed the title feat(swift-sdk): durable wallet presence marker readable while the device is locked feat(swift-sdk): wallet presence flag readable while the device is locked Dec 2, 2026
@romchornyi

Copy link
Copy Markdown
Contributor Author

You're right — switched in 242e382.

The keychain came from the original sketch's "survives reinstall" note, and I can't back it with a scenario. After a reinstall the SwiftData store is gone too, so a locked launch has nothing to start from until the user opens the app, and that first open runs unlocked and backfills the flag.

The cases I checked for a UserDefaults flag:

  • Before the first unlock since boot, UserDefaults reads empty: that gives .unknown, never .absent (a missing flag on its own never means "no wallet").
  • A backup restored to another device carries the flag but not the ThisDeviceOnly mnemonic: at most a stale .present on a locked launch — a deferral, not a setup screen — and the next unlocked read clears it.

What changed: one boolean under <keychainService>.walletPresent, no wallet id; the sibling keychain service, the per-wallet items and markedWalletIds() are gone. Ordering (flag off first, on last), the shared lock and the reconciliation from a readable inventory are unchanged. 12 tests; swift test 732/0. The description is updated.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — thepastaclaw reviewed an earlier commit, not yet this one. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Dec 2, 2026

@llbartekll llbartekll 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.

Thanks for the quick turnaround. Now that it is a single UserDefaults boolean, I think this can live entirely in dashwallet-ios, and this PR is not really needed.

The SDK already reports the locked case honestly: listWalletIdsWithMnemonic() throws instead of returning an empty list. The defect in dashpay/dashwallet-ios#1147 is the app folding that error into "no wallet". The app can fix that on its own:

  • when the listing succeeds, store the result (wallet / no wallet) in its own UserDefaults;
  • when the listing throws, fall back to the stored value, and with no stored value treat it as unknown and do not show Create/Recover;
  • update the flag when it creates or deletes a wallet, since the app drives both.

Which screen to show is an app decision, and the storage layer does not need to know about it. Keeping it in the SDK would only pay off with several apps needing the same thing.

Unless you see something the app cannot do without an SDK change, I would close this one and do it in the dashwallet-ios PR.

@romchornyi

Copy link
Copy Markdown
Contributor Author

Agreed — the app drives every create and delete (the SDK's only own mnemonic delete is PlatformWalletManager.deleteWallet, which the app's wipe calls), so a flag there needs nothing from the SDK. Closing this; the app-side flag will follow dashpay/dashwallet-ios#1151 as its own PR.

@romchornyi romchornyi closed this Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-bots Waiting for the review bots to report on this head

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants