Skip to content

Commit a3c2c9b

Browse files
committed
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]> (cherry picked from commit a93f328) Signed-off-by: Tom Brow <[email protected]>
1 parent 87b1dca commit a3c2c9b

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

@@ -113,12 +106,16 @@ final channelMutesProvider =
113106
ChannelMutesNotifier.new,
114107
);
115108

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

‎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

@@ -131,12 +124,16 @@ final channelSectionsProvider =
131124
ChannelSectionsNotifier.new,
132125
);
133126

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

‎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

@@ -113,12 +106,16 @@ final channelStarsProvider =
113106
ChannelStarsNotifier.new,
114107
);
115108

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

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

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

122-
_identityExportCommunity = community;
122+
// Keep the authorized snapshot and exported payload in the same key
123+
// representation used by RelayConfig for authentication.
124+
_identityExportCommunity = community.copyWith(nsec: community.nsec?.trim());
123125
_identityExportBiometricOnly = biometricOnly;
124126
_identityExportAuthorizedAt = ref.read(identityExportClockProvider)();
125127
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
}
@@ -246,11 +241,18 @@ String? _normalizePubkey(String? value) {
246241
return normalized;
247242
}
248243

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

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

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

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

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

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

‎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)