mirror of
https://github.com/block/buzz.git
synced 2026-10-27 12:40:30 +00:00
Foreground reconnects rebuild preference managers and repeatedly derive the same NIP-44 keys on the UI isolate. Retain the six crypto objects in providers scoped to relay configuration and identity while preserving manager refresh, subscription, and publication behavior. Community changes, key rotation, and sign-out invalidate the retained objects. RelayConfig normalizes the signing key once at its shared accessor, so identity, theme encryption, event signing, and NIP-42 authentication receive the same value, including when an active configuration key has surrounding whitespace. The identity-export authorization snapshot uses the same normalized key for the active-identity check and the outgoing recovery payload. The branch incorporates main's relay-scoped preference managers. ### Validation - Regression exercises six production crypto providers across reconnects, configuration/key changes, and sign-out. Reintroducing a session dependency makes it fail. - Added a loopback WebSocket regression that exercises actual RelaySocket NIP-42 authentication with a padded key and verifies its challenge, relay, pubkey and signature. It fails before shared key normalization. - Added a connected-theme regression for whitespace-padded keys, encryption round-trip, and a valid published event signature. It fails before the normalization fix. - Added padded-key identity export coverage through authorization, SAS confirmation, exactly one normalized payload, and successful completion. Separate relay-change and key-rotation tests verify both fresh and expired grants fail closed. The padded-key regression failed before the fix. - Current head: all 48 focused pairing/crypto/auth tests and mobile analysis passed. All [CI checks](https://github.com/block/buzz/actions/runs/39497098787) passed, including mobile test shards, Android, native Swift tests and release builds. Codex feedback is addressed; Jude approved this exact head after independent regression and mutation checks. Fresh [Codex security review](https://github.com/block/buzz/pull/8442#issuecomment-6300273942) found no concrete issues. - Previous head: 122 config/socket/crypto/theme tests, 44 read-state tests, and all [GitHub CI checks](https://github.com/block/buzz/actions/runs/39491550425) passed, including mobile test shards, Android, native Swift tests and release builds. - Original implementation: native iOS Debug build and full local repository checks passed. A paired simulator background/resume capture had no samples in the targeted crypto constructors or key derivation; an 82 ms UI frame remained. Debug/JIT, profiler overhead and shared-host activity prevent an overall speedup claim. The later normalization change is covered by the production-provider regression above. - Two image decode timing failures occurred in the initial local full suite; direct baseline/patch runs, the subsequent full repository run, and the latest full mobile suite passed. Cold-start credential validation is unchanged: padded persisted keys are rejected by AuthNotifier before RelayConfig is constructed. The normalization regressions cover active configurations and identity export, not broader persisted-key acceptance. <!-- buzz-review-completed --> --------- Signed-off-by: Tom Brow <tomb@block.xyz>
240 lines
7.7 KiB
Dart
240 lines
7.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
import '../crypto/nip44.dart';
|
|
import '../relay/relay.dart';
|
|
import 'accent_colors.dart';
|
|
import 'community_theme_preference.dart';
|
|
import 'community_theme_sync.dart';
|
|
import 'theme_provider.dart';
|
|
|
|
class CommunityThemeNotifier extends Notifier<CommunityThemePreference> {
|
|
CommunityThemeSyncManager? _manager;
|
|
late CommunityThemeStorage _storage;
|
|
String? _pubkey;
|
|
String? _relayUrl;
|
|
Future<void> _persistenceQueue = Future<void>.value();
|
|
int _localRevision = 0;
|
|
CommunityThemePreference? _scopedLocalPreference;
|
|
String? _scopedLocalPubkey;
|
|
String? _scopedLocalRelayUrl;
|
|
|
|
@override
|
|
CommunityThemePreference build() {
|
|
_manager?.dispose();
|
|
_manager = null;
|
|
|
|
_storage = ref.watch(communityThemeStorageProvider);
|
|
final config = ref.watch(relayConfigProvider);
|
|
final session = ref.watch(relaySessionProvider);
|
|
final pubkey = ref.watch(myPubkeyProvider);
|
|
_pubkey = pubkey;
|
|
_relayUrl = config.baseUrl;
|
|
|
|
if (pubkey == null || config.nsec == null) {
|
|
final legacy = _storage.legacyPreference();
|
|
unawaited(_storage.writeLegacy(legacy));
|
|
return legacy;
|
|
}
|
|
|
|
final cached = _storage.read(pubkey, config.baseUrl);
|
|
final dirty = _storage.readOutbox(pubkey, config.baseUrl);
|
|
final inMemoryLocal =
|
|
_scopedLocalPubkey == pubkey && _scopedLocalRelayUrl == config.baseUrl
|
|
? _scopedLocalPreference
|
|
: null;
|
|
if (inMemoryLocal == null) {
|
|
_scopedLocalPreference = null;
|
|
_scopedLocalPubkey = pubkey;
|
|
_scopedLocalRelayUrl = config.baseUrl;
|
|
}
|
|
final fallback = _storage.hasMigrated(pubkey)
|
|
? defaultCommunityTheme
|
|
: _storage.legacyPreference();
|
|
final initial = inMemoryLocal ?? dirty ?? cached ?? fallback;
|
|
|
|
if (session.status == SessionStatus.connected) {
|
|
late final CommunityThemeSyncManager manager;
|
|
manager = CommunityThemeSyncManager(
|
|
pubkey: pubkey,
|
|
relaySession: ref.read(relaySessionProvider.notifier),
|
|
signedEventRelay: SignedEventRelay(
|
|
session: ref.read(relaySessionProvider.notifier),
|
|
nsec: config.nsec!,
|
|
),
|
|
crypto: ref.watch(communityThemeCryptoProvider)!,
|
|
onRemote: (remote) => _applyRemote(manager, remote),
|
|
onPublished: (preference) {
|
|
if (_scopedLocalPubkey == pubkey &&
|
|
_scopedLocalRelayUrl == config.baseUrl &&
|
|
_scopedLocalPreference == preference) {
|
|
_scopedLocalPreference = null;
|
|
}
|
|
unawaited(_storage.clearOutbox(pubkey, config.baseUrl, preference));
|
|
},
|
|
);
|
|
_manager = manager;
|
|
final pending = inMemoryLocal ?? dirty;
|
|
if (pending != null) manager.publish(pending);
|
|
Future.microtask(() async {
|
|
final result = await manager.initialize();
|
|
if (_manager != manager) return;
|
|
if (result.status == CommunityThemeRemoteStatus.absent) {
|
|
final seedRevision = _localRevision;
|
|
await _enqueuePersistence(() async {
|
|
if (_manager != manager || _localRevision != seedRevision) return;
|
|
final currentDirty = _storage.readOutbox(pubkey, config.baseUrl);
|
|
final seed =
|
|
currentDirty ?? _storage.read(pubkey, config.baseUrl) ?? state;
|
|
if (!await _storage.write(pubkey, config.baseUrl, seed)) return;
|
|
if (_manager != manager || _localRevision != seedRevision) return;
|
|
if (!await _storage.writeOutbox(pubkey, config.baseUrl, seed)) {
|
|
return;
|
|
}
|
|
if (_manager == manager && _localRevision == seedRevision) {
|
|
manager.publish(seed);
|
|
}
|
|
});
|
|
}
|
|
if (result.status == CommunityThemeRemoteStatus.valid ||
|
|
result.status == CommunityThemeRemoteStatus.absent) {
|
|
await _storage.markMigrated(pubkey);
|
|
}
|
|
});
|
|
ref.onDispose(manager.dispose);
|
|
}
|
|
return initial;
|
|
}
|
|
|
|
void setMode(ThemeMode mode) {
|
|
var theme = state.theme;
|
|
if (mode == ThemeMode.system) {
|
|
theme = schemeForAppearanceMode(theme, mode) ?? theme;
|
|
} else {
|
|
final effective = effectiveTheme(theme, mode);
|
|
if (effective != null) theme = effective.name;
|
|
}
|
|
_save(
|
|
CommunityThemePreference(
|
|
theme: theme,
|
|
accent: state.accent,
|
|
followSystem: mode == ThemeMode.system,
|
|
),
|
|
);
|
|
}
|
|
|
|
void setTheme(String? theme) {
|
|
_save(
|
|
CommunityThemePreference(
|
|
theme: theme ?? defaultSchemeName,
|
|
accent: state.accent,
|
|
followSystem: state.followSystem,
|
|
),
|
|
);
|
|
}
|
|
|
|
void setAccent(int index) {
|
|
if (index < 0 || index >= accentColors.length) return;
|
|
_save(
|
|
CommunityThemePreference(
|
|
theme: state.theme,
|
|
accent: accentColors[index].wireValue,
|
|
followSystem: state.followSystem,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Persists a complete theme-picker selection as one durable snapshot.
|
|
void setPreference(CommunityThemePreference preference) {
|
|
_save(preference);
|
|
}
|
|
|
|
void _save(CommunityThemePreference preference) {
|
|
if (preference == state) return;
|
|
state = preference;
|
|
_localRevision++;
|
|
final pubkey = _pubkey;
|
|
final relayUrl = _relayUrl;
|
|
if (pubkey == null || relayUrl == null) {
|
|
unawaited(_storage.writeLegacy(preference));
|
|
return;
|
|
}
|
|
_scopedLocalPreference = preference;
|
|
_scopedLocalPubkey = pubkey;
|
|
_scopedLocalRelayUrl = relayUrl;
|
|
_manager?.stage(preference);
|
|
unawaited(
|
|
_enqueuePersistence(
|
|
() => _persistAndPublish(pubkey, relayUrl, preference),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _enqueuePersistence(Future<void> Function() operation) {
|
|
final result = _persistenceQueue.then((_) => operation());
|
|
_persistenceQueue = result.catchError((Object _) {});
|
|
return result;
|
|
}
|
|
|
|
Future<void> _persistAndPublish(
|
|
String pubkey,
|
|
String relayUrl,
|
|
CommunityThemePreference preference,
|
|
) async {
|
|
if (!await _storage.write(pubkey, relayUrl, preference)) return;
|
|
if (!await _storage.writeOutbox(pubkey, relayUrl, preference)) return;
|
|
if (_pubkey == pubkey && _relayUrl == relayUrl) {
|
|
final manager = _manager;
|
|
if (manager != null) {
|
|
manager.stage(preference);
|
|
manager.publishStaged(preference);
|
|
}
|
|
}
|
|
}
|
|
|
|
void _applyRemote(
|
|
CommunityThemeSyncManager manager,
|
|
RemoteCommunityTheme remote,
|
|
) {
|
|
if (_manager != manager) return;
|
|
final pubkey = _pubkey;
|
|
final relayUrl = _relayUrl;
|
|
if (pubkey != null &&
|
|
relayUrl != null &&
|
|
_storage.readOutbox(pubkey, relayUrl) != null) {
|
|
final dirty = _storage.readOutbox(pubkey, relayUrl)!;
|
|
manager.publish(dirty);
|
|
return;
|
|
}
|
|
state = remote.preference;
|
|
if (pubkey != null && relayUrl != null) {
|
|
unawaited(_storage.write(pubkey, relayUrl, remote.preference));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Retains theme encryption material while the same community reconnects.
|
|
final communityThemeCryptoProvider = Provider<CommunityThemeCrypto?>((ref) {
|
|
final config = ref.watch(relayConfigProvider);
|
|
final pubkey = ref.watch(myPubkeyProvider);
|
|
if (pubkey == null || config.nsec == null) return null;
|
|
final privateHex = nostr.Nip19.decode(payload: config.nsec!).data;
|
|
final key = getConversationKey(privateHex, pubkey);
|
|
return CommunityThemeCrypto(
|
|
encrypt: (plaintext) => nip44Encrypt(key, plaintext),
|
|
decrypt: (ciphertext) => nip44Decrypt(key, ciphertext),
|
|
);
|
|
});
|
|
|
|
final communityThemeStorageProvider = Provider<CommunityThemeStorage>(
|
|
(ref) => CommunityThemeStorage(ref.watch(savedPrefsProvider)),
|
|
);
|
|
|
|
final communityThemeProvider =
|
|
NotifierProvider<CommunityThemeNotifier, CommunityThemePreference>(
|
|
CommunityThemeNotifier.new,
|
|
);
|