feat(identity): switch social identity, all from the one backup

Adds pseudonymous, switchable social identities derived from the SAME root
seed via an account index (NostrKeyDerivation.deriveFromSeed(seed, account)).
HKDF is one-way so accounts are unlinkable to the Ğ1 key; account 0 is the
original identity, byte-for-byte unchanged (no rotation for current users),
and every account regenerates from the single seed — so switching adds
nothing to back up.

- SocialAccountStore: keystore-backed active account + max created.
- Per-identity stores (chats, profile, name cache) namespaced by account
  scope (account 0 = legacy keys, no migration) so identities never mix.
- switchSocialAccount() re-derives the identity, re-scopes the stores and
  restarts the inbox listener; RestartWidget rebuilds the tree to pick up
  the new social singletons. DB/inventory untouched.
- Profile 'Your identities' switcher: list, create, switch (with a note
  that messages/contacts are kept separate per identity). i18n en/es/pt/ast.

Tests: account-indexed derivation (legacy 0 unchanged, accounts distinct
yet deterministic, negatives rejected); SocialAccountStore; per-identity
store scope isolation. Resolves the flagged 'change identity' decision
(open-decisions §B).

Known: switching resets navigation to home (full tree rebuild).
This commit is contained in:
vjrj 2026-07-10 20:20:04 +02:00
parent d36fd05741
commit 993f7b37ab
25 changed files with 597 additions and 33 deletions

View file

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'package:commons_core/commons_core.dart';
@ -34,6 +35,7 @@ import '../services/message_store.dart';
import '../services/offer_outbox.dart';
import '../services/profile_cache.dart';
import '../services/profile_store.dart';
import '../services/social_account_store.dart';
import '../services/social_service.dart';
import '../services/social_settings.dart';
@ -75,6 +77,12 @@ Future<void> configureDependencies() async {
final dbKeyHex = await keyStore.databaseKeyHex();
final rootSeedHex = await keyStore.rootSeedHex();
// Which social identity ("account") of the root seed is active. Namespaces the
// per-identity stores (chats, profile, name cache) so identities never mix.
final accounts = SocialAccountStore(secretStore);
final activeAccount = await accounts.active();
final scope = socialAccountScope(activeAccount);
final database = AppDatabase(
openEncryptedExecutor(await _databaseFile(), dbKeyHex),
);
@ -91,7 +99,8 @@ Future<void> configureDependencies() async {
// profile stay hidden) rather than blanking the whole app.
SocialService? socialService;
try {
socialService = await SocialService.fromRootSeedHex(rootSeedHex);
socialService =
await SocialService.fromRootSeedHex(rootSeedHex, account: activeAccount);
} catch (e, s) {
debugPrint('Social identity derivation failed; inventory-only mode: $e\n$s');
}
@ -136,10 +145,15 @@ Future<void> configureDependencies() async {
..registerSingleton<OnboardingStore>(OnboardingStore(secretStore))
..registerSingleton<LocaleStore>(LocaleStore(secretStore))
..registerSingleton<SocialSettings>(SocialSettings(secretStore))
..registerSingleton<SocialAccountStore>(accounts)
..registerSingleton<OfferOutbox>(OfferOutbox(secretStore))
..registerSingleton<MessageStore>(MessageStore(secretStore))
..registerSingleton<ProfileStore>(ProfileStore(secretStore))
..registerSingleton<ProfileCache>(ProfileCache(secretStore))
// Per-identity stores are namespaced by the active account's scope.
..registerSingleton<MessageStore>(
MessageStore(secretStore, accountScope: scope))
..registerSingleton<ProfileStore>(
ProfileStore(secretStore, accountScope: scope))
..registerSingleton<ProfileCache>(
ProfileCache(secretStore, accountScope: scope))
..registerSingleton<ExportImportService>(
ExportImportService(
repository: varietyRepository,
@ -192,6 +206,53 @@ Future<void> configureDependencies() async {
getIt.registerSingleton<_DepsReady>(const _DepsReady());
}
/// Switches the active social identity to [account], re-deriving the Nostr key
/// from the SAME root seed and re-scoping the per-identity stores (chats,
/// profile, name cache) so nothing leaks between identities. Restarts the inbox
/// listener on the new identity. The caller must rebuild the widget tree
/// (`RestartWidget.restart`) so screens and router pick up the new singletons.
/// The DB, inventory and relay settings are untouched only the social slice.
Future<void> switchSocialAccount(int account) async {
final secretStore = FlutterSecretStore();
await getIt<SocialAccountStore>().setActive(account);
final scope = socialAccountScope(account);
final rootSeedHex = await getIt<SecureKeyStore>().rootSeedHex();
// Tear down the old identity's live listener/sessions before replacing.
if (getIt.isRegistered<InboxService>()) {
await getIt<InboxService>().stop();
await getIt.unregister<InboxService>();
}
if (getIt.isRegistered<SocialService>()) {
await getIt.unregister<SocialService>();
}
await getIt.unregister<MessageStore>();
await getIt.unregister<ProfileStore>();
await getIt.unregister<ProfileCache>();
// Re-register the per-identity stores under the new scope, then the identity.
getIt
..registerSingleton<MessageStore>(
MessageStore(secretStore, accountScope: scope))
..registerSingleton<ProfileStore>(
ProfileStore(secretStore, accountScope: scope))
..registerSingleton<ProfileCache>(
ProfileCache(secretStore, accountScope: scope));
final social =
await SocialService.fromRootSeedHex(rootSeedHex, account: account);
final inbox = InboxService(
social: social,
settings: getIt<SocialSettings>(),
store: getIt<MessageStore>(),
profileCache: getIt<ProfileCache>(),
);
getIt
..registerSingleton<SocialService>(social)
..registerSingleton<InboxService>(inbox);
unawaited(inbox.start());
}
Future<Directory> _backupsDir() async {
final dir = await getApplicationSupportDirectory();
return Directory(p.join(dir.path, 'backups'));