mirror of
https://github.com/block/buzz.git
synced 2026-10-27 04:30:52 +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>
141 lines
4.1 KiB
Dart
141 lines
4.1 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import '../../../shared/relay/relay.dart';
|
|
import '../../../shared/theme/theme_provider.dart';
|
|
import '../../../shared/community/community_provider.dart';
|
|
import 'channel_sections_manager.dart';
|
|
import 'channel_sections_storage.dart';
|
|
|
|
class ChannelSectionsState {
|
|
final bool isReady;
|
|
final ChannelSectionStore store;
|
|
|
|
/// Bumped on every change to force downstream rebuilds.
|
|
final int version;
|
|
|
|
const ChannelSectionsState({
|
|
this.isReady = false,
|
|
this.store = const ChannelSectionStore(),
|
|
this.version = 0,
|
|
});
|
|
}
|
|
|
|
class ChannelSectionsNotifier extends Notifier<ChannelSectionsState> {
|
|
ChannelSectionsManager? _manager;
|
|
bool _isInitialized = false;
|
|
|
|
@override
|
|
ChannelSectionsState build() {
|
|
_manager?.dispose(flushPending: false);
|
|
_manager = null;
|
|
_isInitialized = false;
|
|
|
|
final relayConfig = ref.watch(relayConfigProvider);
|
|
final sessionState = ref.watch(relaySessionProvider);
|
|
// Rebuild when the active community changes (pubkey may differ).
|
|
ref.watch(activeCommunityProvider);
|
|
|
|
final nsec = relayConfig.nsec?.trim();
|
|
if (nsec == null || nsec.isEmpty) {
|
|
return const ChannelSectionsState();
|
|
}
|
|
|
|
final pubkey = ref.watch(myPubkeyProvider);
|
|
final crypto = ref.watch(channelSectionsCryptoProvider);
|
|
if (pubkey == null || crypto == null) {
|
|
return const ChannelSectionsState();
|
|
}
|
|
|
|
final prefs = ref.read(savedPrefsProvider);
|
|
final signedRelay = SignedEventRelay(
|
|
session: ref.read(relaySessionProvider.notifier),
|
|
nsec: nsec,
|
|
);
|
|
|
|
late final ChannelSectionsManager manager;
|
|
manager = ChannelSectionsManager(
|
|
pubkey: pubkey,
|
|
relayUrl: relayConfig.baseUrl,
|
|
prefs: prefs,
|
|
crypto: crypto,
|
|
relaySession: ref.read(relaySessionProvider.notifier),
|
|
signedEventRelay: signedRelay,
|
|
remoteEnabled: sessionState.status == SessionStatus.connected,
|
|
onChanged: () => _emitManagerState(manager),
|
|
);
|
|
_manager = manager;
|
|
|
|
// Resume re-read catches an EVENT a healthy socket never delivered.
|
|
ref.listen(appLifecycleProvider, (_, next) {
|
|
if (next == AppLifecycleState.resumed) manager.refreshFromRelay();
|
|
});
|
|
|
|
ref.onDispose(() {
|
|
manager.dispose();
|
|
if (_manager == manager) {
|
|
_manager = null;
|
|
}
|
|
});
|
|
|
|
Future.microtask(() async {
|
|
await manager.initialize();
|
|
if (_manager != manager) return;
|
|
_isInitialized = true;
|
|
_emitManagerState(manager);
|
|
});
|
|
|
|
return ChannelSectionsState(
|
|
isReady: false,
|
|
store: manager.store,
|
|
version: 1,
|
|
);
|
|
}
|
|
|
|
void createSection(String name) => _manager?.createSection(name);
|
|
|
|
void renameSection(String sectionId, String newName) =>
|
|
_manager?.renameSection(sectionId, newName);
|
|
|
|
void deleteSection(String sectionId) => _manager?.deleteSection(sectionId);
|
|
|
|
void moveSectionUp(String sectionId) => _manager?.moveSectionUp(sectionId);
|
|
|
|
void moveSectionDown(String sectionId) =>
|
|
_manager?.moveSectionDown(sectionId);
|
|
|
|
void assignChannel(String channelId, String sectionId) =>
|
|
_manager?.assignChannel(channelId, sectionId);
|
|
|
|
void unassignChannel(String channelId) =>
|
|
_manager?.unassignChannel(channelId);
|
|
|
|
void _emitManagerState(ChannelSectionsManager manager) {
|
|
if (_manager != manager) return;
|
|
state = ChannelSectionsState(
|
|
isReady: _isInitialized,
|
|
store: manager.store,
|
|
version: state.version + 1,
|
|
);
|
|
}
|
|
}
|
|
|
|
final channelSectionsProvider =
|
|
NotifierProvider<ChannelSectionsNotifier, ChannelSectionsState>(
|
|
ChannelSectionsNotifier.new,
|
|
);
|
|
|
|
/// Identity-scoped crypto survives connection status changes. A community or
|
|
/// signing-key change replaces it through [relayConfigProvider].
|
|
final channelSectionsCryptoProvider = Provider<ChannelSectionsCrypto?>((ref) {
|
|
final config = ref.watch(relayConfigProvider);
|
|
final pubkey = ref.watch(myPubkeyProvider);
|
|
final nsec = config.nsec?.trim();
|
|
if (nsec == null || nsec.isEmpty || pubkey == null) return null;
|
|
try {
|
|
return ChannelSectionsCrypto(nsec, pubkey);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
});
|