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).
60 lines
2.3 KiB
Dart
60 lines
2.3 KiB
Dart
import '../security/secret_store.dart';
|
|
|
|
/// The per-identity key scope for [account]: empty for the original identity
|
|
/// (account 0, so its data keeps the legacy un-namespaced keys), otherwise a
|
|
/// distinct namespace. Used by the per-identity stores (chats, profile, name
|
|
/// cache) so switching identity never mixes one identity's data into another.
|
|
String socialAccountScope(int account) => account == 0 ? '' : 'acct$account';
|
|
|
|
/// Remembers which social identity ("account") of the root seed is active, and
|
|
/// how many the user has created, so identities can be switched and listed.
|
|
///
|
|
/// Each account is a pseudonymous Nostr identity deterministically derived from
|
|
/// the SAME root seed (see `NostrKeyDerivation.deriveFromSeed`), so switching
|
|
/// never adds anything to back up — the one seed still regenerates them all.
|
|
/// Keystore-backed: no plaintext at rest.
|
|
class SocialAccountStore {
|
|
SocialAccountStore(this._store);
|
|
|
|
final SecretStore _store;
|
|
|
|
static const _activeKey = 'tane.social.account.active';
|
|
static const _maxKey = 'tane.social.account.max';
|
|
|
|
/// The active account index (0 = the original identity).
|
|
Future<int> active() async => _readInt(_activeKey);
|
|
|
|
/// The highest account index ever created (so the list is 0..max and a new
|
|
/// identity takes max + 1). At least the active one always exists.
|
|
Future<int> maxCreated() async {
|
|
final max = await _readInt(_maxKey);
|
|
final activeIdx = await active();
|
|
return max > activeIdx ? max : activeIdx;
|
|
}
|
|
|
|
/// Switches the active identity to [account] (must already be within
|
|
/// 0..maxCreated, or be maxCreated + 1 for a freshly created one).
|
|
Future<void> setActive(int account) async {
|
|
if (account < 0) {
|
|
throw ArgumentError.value(account, 'account', 'must be >= 0');
|
|
}
|
|
await _store.write(_activeKey, '$account');
|
|
if (account > await _readInt(_maxKey)) {
|
|
await _store.write(_maxKey, '$account');
|
|
}
|
|
}
|
|
|
|
/// Creates a brand-new identity (the next index) and makes it active. Returns
|
|
/// the new account index.
|
|
Future<int> createNew() async {
|
|
final next = await maxCreated() + 1;
|
|
await setActive(next);
|
|
return next;
|
|
}
|
|
|
|
Future<int> _readInt(String key) async {
|
|
final raw = await _store.read(key);
|
|
if (raw == null || raw.isEmpty) return 0;
|
|
return int.tryParse(raw) ?? 0;
|
|
}
|
|
}
|