Skip to content

fix(web): store a new browser key before registering it, and finish interrupted renewals (D-016) - #94

Merged
PastaPastaPasta merged 2 commits into
masterfrom
fix/web-key-store-first
Sep 27, 2026
Merged

PastaPastaPasta merged 2 commits into
masterfrom
fix/web-key-store-first

Conversation

@PastaPastaPasta

Copy link
Copy Markdown
Owner

Summary

Fixes D-016: a browser key import or renewal could lock the user out. Before this change, the identity update registering the new key was signed and broadcast before the key was stored. If storing failed (quota, private mode, a closed tab), the old key was already disabled on chain and the new one existed nowhere.

The key is now stored first, and the chain changes second:

  1. Stage, read back, then sign. registerLimitedKey takes a persist hook. It is called with the new key before the update is signed. stageInVault writes a vault-staged: record (carrying replaces, the main keyId it will replace) and reads it back. A write the browser didn't keep throws, and nothing changes on chain. After the update lands, storeInVault commits the key as the main record and drops the stage.
  2. Finish interrupted renewals. Every unlock and restored session runs finishStagedRenewal → recoverStaged, which returns a RecoverResult:
    • adopted: Platform shows the staged WIF under its keyId, whether live, expired or disabled. The staged key becomes the main record, and the storage and encryption blobs are re-sealed under it.
    • discarded: another key holds that id, which proves the stage was never registered. The stage is dropped.
    • pending: the key is not visible yet. The stage is kept.
    • locked: the method used opens the current key but not the renewal. The session opens with a notice explaining how to finish the renewal.
    • conflict: the main record changed since staging, for example after a newer sign-in. Nothing is overwritten. The check is repeated inside the finishing IndexedDB transaction via the new idbUpdate(…, alsoRead).
  3. Unlock tries both records. Each record is opened with its own passphrase or passkey slots, so a renewal protected with a different passphrase is never a dead end. When only the staged key opens and it wasn't adopted, the message depends on the outcome: "try again in a minute", "never reached Platform, discarded", or "finish or discard in Settings → Keys".
  4. Wallet sign-in with a pending renewal throws PendingRenewalChoiceError, and the flow offers two options:
    • Finish the renewal (unlock);
    • Continue with the wallet, which discards the renewal. The UI says the renewal's key stays registered but unused and can be disabled later in Settings → Keys.
  5. Settings → Keys lists a pending renewal (key id, start date, protection methods), with Finish it (lock and unlock) and Discard.
  6. Identity creation, "exists → renew" path: it stages through the same path before its update.
  7. Small fix (lib/spend.ts): the spend ledger key now includes the event kind. This fixes a same-millisecond overwrite that made key-spend.test.ts flaky.

Tests

  • lib/auth/key-safety.test.ts (vault):
    • stage, read back, then commit;
    • a read-back refusal;
    • a quota failure stopping before the chain;
    • the registered, never and unknown outcomes;
    • listing staged-only identities;
    • another passphrase returning locked and then adopting;
    • clobbering refused, and the conflict result;
    • a store racing the finishing transaction returning conflict;
    • abandoning a stage;
    • blob carry-over.
  • lib/auth/key-recovery.test.ts (controller):
    • adopted on unlock;
    • a staged-only first import;
    • an unknown stage kept;
    • the renewal's passphrase finishing it;
    • the old passphrase opening with a notice;
    • an expired registered stage adopted;
    • abandoning;
    • never dropped, with the "discarded" message.
  • Gates: tsc and lint are clean, and 1921 tests pass (10 skipped).

Review

code-review-validator ran three rounds on the vault and controller changes. Every round's blockers were fixed, and the last verdict was clear. Its two notes, the outcome-specific unlock message and the in-transaction conflict recheck, are included here.

Live

The companion write-refusal work (#91) was re-run live on the beta.5 devnet (moutai, master 98c04d4) and passed 6/6. This PR changes only local vault ordering and recovery; the on-chain update it signs is the same one as before.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 32 minutes.

Check out review usage here.

View limit details

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

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

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 2fba84f5-d091-4d7e-a3e6-0bbb132a9a32

📥 Commits

Reviewing files that changed from the base of the PR and between 2b6fe46 and d0a6384.

📒 Files selected for processing (13)
  • forge-web/components/auth/create-identity-flow.tsx
  • forge-web/components/auth/wallet-connect-flow.tsx
  • forge-web/components/keys-panel.tsx
  • forge-web/components/pending-renewal.tsx
  • forge-web/contexts/auth-context.tsx
  • forge-web/lib/auth/controller.ts
  • forge-web/lib/auth/create-identity.ts
  • forge-web/lib/auth/key-recovery.test.ts
  • forge-web/lib/auth/key-safety.test.ts
  • forge-web/lib/auth/limited-key.ts
  • forge-web/lib/auth/vault.ts
  • forge-web/lib/idb.ts
  • forge-web/lib/spend.ts

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.

PastaPastaPasta and others added 2 commits September 27, 2026 17:07
…nterrupted renewals (D-016)

Importing an identity or renewing its browser key now stages the new key in the vault and reads it back before the identity update is signed. A storage failure therefore stops before anything changes on chain. The staged record is committed as the main one only after the update lands.

An interrupted renewal (the tab closed, or storing failed after registering) is finished on the next unlock or restored session, once Platform shows the staged key under its id. Unlock tries both records' own protections, so a renewal protected with another passphrase or passkey is never a dead end. A stage that Platform proves was never registered is dropped. A stage whose main record has changed since is kept as a conflict, rechecked inside the finishing transaction. Storage and encryption blobs carry across.

A wallet sign-in with a pending renewal offers a choice: finish it (unlock) or continue with the wallet, which discards it; the key stays registered but unused. Settings → Keys lists a pending renewal with Finish and Discard. Also keys the spend ledger by kind to fix a same-millisecond overwrite.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…iscarded (D-016)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@PastaPastaPasta
PastaPastaPasta merged commit 320ce41 into master Sep 27, 2026
5 checks passed
PastaPastaPasta added a commit that referenced this pull request Sep 27, 2026
Resolve with #87 (per-facade retryable reads; pathElements added under system) and #94 (staged key persist in the renewal, D-016; the sheet remembers only a committed key).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

1 participant