Skip to content

Commit a93f328

Browse files
authored
perf(mobile): retain preference crypto across reconnects (#8442)
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](#8442 (comment)) 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 <[email protected]>
1 parent 8bbfe44 commit a93f328

11 files changed

Lines changed: 452 additions & 171 deletions

File tree

‎mobile/lib/features/channels/channel_mutes/channel_mutes_provider.dart‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/widgets.dart';
22
import 'package:hooks_riverpod/hooks_riverpod.dart';
3-
import 'package:nostr/nostr.dart' as nostr;
43

54
import '../../../shared/relay/relay.dart';
65
import '../../../shared/theme/theme_provider.dart';
@@ -42,15 +41,9 @@ class ChannelMutesNotifier extends Notifier<ChannelMutesState> {
4241
return const ChannelMutesState();
4342
}
4443

45-
final pubkey = _safePubkeyFromNsec(nsec);
46-
if (pubkey == null || pubkey.isEmpty) {
47-
return const ChannelMutesState();
48-
}
49-
50-
final ChannelMutesCrypto crypto;
51-
try {
52-
crypto = ChannelMutesCrypto(nsec, pubkey);
53-
} catch (_) {
44+
final pubkey = ref.watch(myPubkeyProvider);
45+
final crypto = ref.watch(channelMutesCryptoProvider);
46+
if (pubkey == null || crypto == null) {
5447
return const ChannelMutesState();
5548
}
5649

@@ -114,12 +107,16 @@ final channelMutesProvider =
114107
ChannelMutesNotifier.new,
115108
);
116109

117-
String? _safePubkeyFromNsec(String nsec) {
110+
/// Identity-scoped crypto survives connection status changes. A community or
111+
/// signing-key change replaces it through [relayConfigProvider].
112+
final channelMutesCryptoProvider = Provider<ChannelMutesCrypto?>((ref) {
113+
final config = ref.watch(relayConfigProvider);
114+
final pubkey = ref.watch(myPubkeyProvider);
115+
final nsec = config.nsec?.trim();
116+
if (nsec == null || nsec.isEmpty || pubkey == null) return null;
118117
try {
119-
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
120-
if (privkeyHex.isEmpty) return null;
121-
return nostr.Keys(privkeyHex).public;
118+
return ChannelMutesCrypto(nsec, pubkey);
122119
} catch (_) {
123120
return null;
124121
}
125-
}
122+
});

‎mobile/lib/features/channels/channel_sections/channel_sections_provider.dart‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/widgets.dart';
22
import 'package:hooks_riverpod/hooks_riverpod.dart';
3-
import 'package:nostr/nostr.dart' as nostr;
43

54
import '../../../shared/relay/relay.dart';
65
import '../../../shared/theme/theme_provider.dart';
@@ -42,15 +41,9 @@ class ChannelSectionsNotifier extends Notifier<ChannelSectionsState> {
4241
return const ChannelSectionsState();
4342
}
4443

45-
final pubkey = _safePubkeyFromNsec(nsec);
46-
if (pubkey == null || pubkey.isEmpty) {
47-
return const ChannelSectionsState();
48-
}
49-
50-
final ChannelSectionsCrypto crypto;
51-
try {
52-
crypto = ChannelSectionsCrypto(nsec, pubkey);
53-
} catch (_) {
44+
final pubkey = ref.watch(myPubkeyProvider);
45+
final crypto = ref.watch(channelSectionsCryptoProvider);
46+
if (pubkey == null || crypto == null) {
5447
return const ChannelSectionsState();
5548
}
5649

@@ -132,12 +125,16 @@ final channelSectionsProvider =
132125
ChannelSectionsNotifier.new,
133126
);
134127

135-
String? _safePubkeyFromNsec(String nsec) {
128+
/// Identity-scoped crypto survives connection status changes. A community or
129+
/// signing-key change replaces it through [relayConfigProvider].
130+
final channelSectionsCryptoProvider = Provider<ChannelSectionsCrypto?>((ref) {
131+
final config = ref.watch(relayConfigProvider);
132+
final pubkey = ref.watch(myPubkeyProvider);
133+
final nsec = config.nsec?.trim();
134+
if (nsec == null || nsec.isEmpty || pubkey == null) return null;
136135
try {
137-
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
138-
if (privkeyHex.isEmpty) return null;
139-
return nostr.Keys(privkeyHex).public;
136+
return ChannelSectionsCrypto(nsec, pubkey);
140137
} catch (_) {
141138
return null;
142139
}
143-
}
140+
});

‎mobile/lib/features/channels/channel_sort/channel_sort_provider.dart‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/widgets.dart';
22
import 'package:hooks_riverpod/hooks_riverpod.dart';
3-
import 'package:nostr/nostr.dart' as nostr;
43

54
import '../../../shared/relay/relay.dart';
65
import '../../../shared/theme/theme_provider.dart';
@@ -44,15 +43,9 @@ class ChannelSortNotifier extends Notifier<ChannelSortState> {
4443
return const ChannelSortState();
4544
}
4645

47-
final pubkey = _safePubkeyFromNsec(nsec);
48-
if (pubkey == null || pubkey.isEmpty) {
49-
return const ChannelSortState();
50-
}
51-
52-
final ChannelSortCrypto crypto;
53-
try {
54-
crypto = ChannelSortCrypto(nsec, pubkey);
55-
} catch (_) {
46+
final pubkey = ref.watch(myPubkeyProvider);
47+
final crypto = ref.watch(channelSortCryptoProvider);
48+
if (pubkey == null || crypto == null) {
5649
return const ChannelSortState();
5750
}
5851

@@ -124,12 +117,16 @@ final channelSortProvider =
124117
ChannelSortNotifier.new,
125118
);
126119

127-
String? _safePubkeyFromNsec(String nsec) {
120+
/// Identity-scoped crypto survives connection status changes. A community or
121+
/// signing-key change replaces it through [relayConfigProvider].
122+
final channelSortCryptoProvider = Provider<ChannelSortCrypto?>((ref) {
123+
final config = ref.watch(relayConfigProvider);
124+
final pubkey = ref.watch(myPubkeyProvider);
125+
final nsec = config.nsec?.trim();
126+
if (nsec == null || nsec.isEmpty || pubkey == null) return null;
128127
try {
129-
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
130-
if (privkeyHex.isEmpty) return null;
131-
return nostr.Keys(privkeyHex).public;
128+
return ChannelSortCrypto(nsec, pubkey);
132129
} catch (_) {
133130
return null;
134131
}
135-
}
132+
});

‎mobile/lib/features/channels/channel_stars/channel_stars_provider.dart‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/widgets.dart';
22
import 'package:hooks_riverpod/hooks_riverpod.dart';
3-
import 'package:nostr/nostr.dart' as nostr;
43

54
import '../../../shared/relay/relay.dart';
65
import '../../../shared/theme/theme_provider.dart';
@@ -42,15 +41,9 @@ class ChannelStarsNotifier extends Notifier<ChannelStarsState> {
4241
return const ChannelStarsState();
4342
}
4443

45-
final pubkey = _safePubkeyFromNsec(nsec);
46-
if (pubkey == null || pubkey.isEmpty) {
47-
return const ChannelStarsState();
48-
}
49-
50-
final ChannelStarsCrypto crypto;
51-
try {
52-
crypto = ChannelStarsCrypto(nsec, pubkey);
53-
} catch (_) {
44+
final pubkey = ref.watch(myPubkeyProvider);
45+
final crypto = ref.watch(channelStarsCryptoProvider);
46+
if (pubkey == null || crypto == null) {
5447
return const ChannelStarsState();
5548
}
5649

@@ -114,12 +107,16 @@ final channelStarsProvider =
114107
ChannelStarsNotifier.new,
115108
);
116109

117-
String? _safePubkeyFromNsec(String nsec) {
110+
/// Identity-scoped crypto survives connection status changes. A community or
111+
/// signing-key change replaces it through [relayConfigProvider].
112+
final channelStarsCryptoProvider = Provider<ChannelStarsCrypto?>((ref) {
113+
final config = ref.watch(relayConfigProvider);
114+
final pubkey = ref.watch(myPubkeyProvider);
115+
final nsec = config.nsec?.trim();
116+
if (nsec == null || nsec.isEmpty || pubkey == null) return null;
118117
try {
119-
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
120-
if (privkeyHex.isEmpty) return null;
121-
return nostr.Keys(privkeyHex).public;
118+
return ChannelStarsCrypto(nsec, pubkey);
122119
} catch (_) {
123120
return null;
124121
}
125-
}
122+
});

‎mobile/lib/features/pairing/pairing_provider.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ class PairingNotifier extends Notifier<PairingState> {
121121
return false;
122122
}
123123

124-
_identityExportCommunity = community;
124+
// Keep the authorized snapshot and exported payload in the same key
125+
// representation used by RelayConfig for authentication.
126+
_identityExportCommunity = community.copyWith(nsec: community.nsec?.trim());
125127
_identityExportBiometricOnly = biometricOnly;
126128
_identityExportAuthorizedAt = ref.read(identityExportClockProvider)();
127129
state = state.copyWith(authorizationInProgress: false);

‎mobile/lib/shared/read_state/read_state_provider.dart‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ReadStateNotifier extends Notifier<ReadStateState> {
7676

7777
final relayConfig = ref.watch(relayConfigProvider);
7878
ref.watch(relaySessionProvider);
79-
final activeCommunity = ref.watch(activeCommunityProvider).value;
79+
ref.watch(activeCommunityProvider);
8080

8181
final nsec = relayConfig.nsec?.trim();
8282
if (nsec == null || nsec.isEmpty) {
@@ -87,14 +87,9 @@ class ReadStateNotifier extends Notifier<ReadStateState> {
8787
session: ref.read(relaySessionProvider.notifier),
8888
nsec: nsec,
8989
);
90-
final pubkey =
91-
_normalizePubkey(activeCommunity?.pubkey) ??
92-
_safeDerivedPubkey(signedRelay);
93-
if (pubkey == null) {
94-
return const ReadStateState.inert();
95-
}
96-
97-
final crypto = ReadStateCrypto.tryCreate(nsec: nsec, pubkey: pubkey);
90+
final pubkey = ref.watch(_readStatePubkeyProvider);
91+
if (pubkey == null) return const ReadStateState.inert();
92+
final crypto = ref.watch(readStateCryptoProvider);
9893
if (crypto == null) {
9994
return const ReadStateState.inert();
10095
}
@@ -247,11 +242,18 @@ String? _normalizePubkey(String? value) {
247242
return normalized;
248243
}
249244

250-
String? _safeDerivedPubkey(SignedEventRelay relay) {
251-
try {
252-
return _normalizePubkey(relay.pubkey);
253-
} catch (e) {
254-
debugPrint('[ReadStateManager] pubkey derivation failed: $e');
255-
return null;
256-
}
257-
}
245+
final _readStatePubkeyProvider = Provider<String?>((ref) {
246+
final active = ref.watch(activeCommunityProvider).value;
247+
final config = ref.watch(relayConfigProvider);
248+
return _normalizePubkey(active?.pubkey) ??
249+
_normalizePubkey(pubkeyFromNsec(config.nsec?.trim()));
250+
});
251+
252+
/// Retains read-state encryption material across connection status changes.
253+
final readStateCryptoProvider = Provider<ReadStateCrypto?>((ref) {
254+
final config = ref.watch(relayConfigProvider);
255+
final pubkey = ref.watch(_readStatePubkeyProvider);
256+
final nsec = config.nsec?.trim();
257+
if (nsec == null || nsec.isEmpty || pubkey == null) return null;
258+
return ReadStateCrypto.tryCreate(nsec: nsec, pubkey: pubkey);
259+
});

‎mobile/lib/shared/relay/relay_provider.dart‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ import 'relay_client.dart';
1111
/// - `baseUrl` — where the relay lives (used for WS + media upload)
1212
/// - `nsec` — the user's signing key (drives NIP-42 AUTH and event sigs)
1313
class RelayConfig {
14-
const RelayConfig({required String baseUrl, this.nsec}) : _baseUrl = baseUrl;
14+
const RelayConfig({required String baseUrl, String? nsec})
15+
: _baseUrl = baseUrl,
16+
_nsec = nsec;
1517

1618
/// Relay origin exactly as the active community stored it.
1719
final String _baseUrl;
1820

19-
/// Nostr secret key (bech32 nsec) for signing events and NIP-42 AUTH.
20-
final String? nsec;
21+
/// Normalized Nostr secret key shared by identity, signing, and NIP-42 AUTH.
22+
String? get nsec => _nsec?.trim();
23+
24+
final String? _nsec;
2125

2226
/// The origin as persisted, before scheme canonicalization.
2327
///

‎mobile/lib/shared/theme/community_theme_provider.dart‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CommunityThemeNotifier extends Notifier<CommunityThemePreference> {
3030
_storage = ref.watch(communityThemeStorageProvider);
3131
final config = ref.watch(relayConfigProvider);
3232
final session = ref.watch(relaySessionProvider);
33-
final pubkey = pubkeyFromNsec(config.nsec);
33+
final pubkey = ref.watch(myPubkeyProvider);
3434
_pubkey = pubkey;
3535
_relayUrl = config.baseUrl;
3636

@@ -65,7 +65,7 @@ class CommunityThemeNotifier extends Notifier<CommunityThemePreference> {
6565
session: ref.read(relaySessionProvider.notifier),
6666
nsec: config.nsec!,
6767
),
68-
crypto: _crypto(config.nsec!, pubkey),
68+
crypto: ref.watch(communityThemeCryptoProvider)!,
6969
onRemote: (remote) => _applyRemote(manager, remote),
7070
onPublished: (preference) {
7171
if (_scopedLocalPubkey == pubkey &&
@@ -216,14 +216,18 @@ class CommunityThemeNotifier extends Notifier<CommunityThemePreference> {
216216
}
217217
}
218218

219-
CommunityThemeCrypto _crypto(String nsec, String pubkey) {
220-
final privateHex = nostr.Nip19.decode(payload: nsec).data;
219+
/// Retains theme encryption material while the same community reconnects.
220+
final communityThemeCryptoProvider = Provider<CommunityThemeCrypto?>((ref) {
221+
final config = ref.watch(relayConfigProvider);
222+
final pubkey = ref.watch(myPubkeyProvider);
223+
if (pubkey == null || config.nsec == null) return null;
224+
final privateHex = nostr.Nip19.decode(payload: config.nsec!).data;
221225
final key = getConversationKey(privateHex, pubkey);
222226
return CommunityThemeCrypto(
223227
encrypt: (plaintext) => nip44Encrypt(key, plaintext),
224228
decrypt: (ciphertext) => nip44Decrypt(key, ciphertext),
225229
);
226-
}
230+
});
227231

228232
final communityThemeStorageProvider = Provider<CommunityThemeStorage>(
229233
(ref) => CommunityThemeStorage(ref.watch(savedPrefsProvider)),

0 commit comments

Comments
 (0)