Skip to content

feat(nostr): track which account owns a local key on shared phones - #727

Merged
islandbitcoin merged 3 commits into
mainfrom
feat/nostr-multi-account-keys
Sep 24, 2026
Merged

islandbitcoin merged 3 commits into
mainfrom
feat/nostr-multi-account-keys

Conversation

@islandbitcoin

@islandbitcoin islandbitcoin commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Part of ENG-601 (multi-account support). Stacked on #720. Base is fix/nostr-npub-auto-relink; retarget to main once #720 merges. Draft until then.

Problem

The keychain entry holding the Nostr nsec survives logout (use-logout.ts never clears it). On a phone shared by two Flash accounts, account B starts with the key account A generated. #720 registers an unregistered local key silently, so B would claim A's key. The backend then refuses A (NPUB_NOT_AVAILABLE) and A's DMs go to a key B advertises.

This is the first piece of multi-account support: two or more Flash accounts sharing one phone.

Change

  • app/nostr/key-owner.ts: records the owning account next to each local key, keyed on the npub. generateAndStoreKey and saveNewNostrKey stamp it. Deleting keys clears it.
  • NostrKeyEnsurer:
    • unregistered with a key owned by another account → asks once per (account, key) instead of registering.
    • linked → adopts ownership, so keys from before this record and imported nsecs get an owner.
    • mismatch with a foreign-owned key → names the other account, and a refusal never advises deleting that account's key.
    • Runs once per account instead of once per mount. Nothing above it remounts on logout, so B was never checked after A in the same process.
  • keyForeignMessage / keyForeignRelinkRefused, translated in all locales.

Tests

  • __tests__/nostr/key-owner.spec.ts, __tests__/nostr/signer.spec.ts (owner stamp).
  • nostr-key-ensurer.spec.tsx: foreign-key prompt, take over on confirm, decline remembered, per-key marker, refusal copy, once-per-account across logout/login, account payload without id.
  • use-nostr-profile-relink.spec.ts: never registers a foreign key, owner stamp on fresh generation, delete clears owners.

62 tests pass on this branch.

Out of scope, still open for full multi-account

  • Keychain holds one nsec for the whole device, not one per account. Switching accounts still swaps which account can decrypt.
  • Chat caches (giftwraps, rumors, lastSeen_*) are device-global and survive logout, so B can see A's decrypted history.

🤖 Generated with Claude Code

https://claude.ai/code/session_014etuRoG7S3DxsAmWMP9jAX

@linear

linear Bot commented Sep 15, 2026

Copy link
Copy Markdown

ENG-601

Rebased as one commit onto main after #720 merged; content unchanged from
the previous branch head (ee94b11).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@islandbitcoin
islandbitcoin force-pushed the feat/nostr-multi-account-keys branch from ee94b11 to 56a0b10 Compare September 23, 2026 18:20
Dread and others added 2 commits September 24, 2026 12:33
…reign keys

Review fixes for #727:

- importNsec: a backend refusal (NPUB_NOT_AVAILABLE) is now an error, not
  a success. The form no longer shows "nsec imported successfully!" for a
  key the account cannot advertise. Parameters folded into an options
  object (max-params). Form-level test covers accepted/refused and asserts
  the owner stamp through the rendered form.
- NostrKeyEnsurer: mismatch + foreign key uses a new
  keyForeignMismatchMessage that keeps the "differs from the one on your
  account" warning, so "Use this device" is not mistaken for merely
  borrowing another account's key. Added to all 23 locales. Dead
  `if (!accountId)` guard removed; doc comments rewrapped.
- keyOwnerState: no account id -> "unknown", never "other" (the id comes
  from a cache-first query that can be unloaded).
- Reconnect spec asserts the owner record after a successful relink
  (pins the accountId wiring in advanced-settings.tsx); stray blank lines
  in beforeEach removed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Review fixes for #727:

- importNsec now calls the backend first and only writes the keychain,
  clears the replaced key's owner record, and stamps the new owner once
  the key is accepted. A refusal (NPUB_NOT_AVAILABLE) is now a true no-op:
  the account's own nsec and its owner record survive, so on a shared
  phone B importing A's key no longer destroys B's key.
- updateFlashBackend is typed Promise<boolean> with a fail-closed guard;
  the void-result-counts-as-registered test is removed with it.
- Tests: refused case seeds a previous key + owner and asserts both are
  untouched (util and rendered form); a new test pins backend-before-
  keychain ordering.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@islandbitcoin
islandbitcoin marked this pull request as ready for review September 24, 2026 20:11
@islandbitcoin
islandbitcoin merged commit 15e79c0 into main Sep 24, 2026
10 checks passed
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