Skip to content

Commit d291f12

Browse files
browclaude
andcommitted
fix(mobile): show an out-of-range identity as unavailable in Settings
Native derivation throws for a well-formed nsec whose scalar is zero or not below the curve order. Catch it so the identity row falls back to "Identity unavailable" instead of crashing Settings. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 4f36965 commit d291f12

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎mobile/lib/features/settings/settings_page/connection_section.dart‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,25 @@ Future<bool> _waitForResumedFrame() async {
108108
return true;
109109
}
110110

111+
/// The full npub for [privHex], or null when it holds no usable key. A
112+
/// well-formed nsec can still hold a scalar outside the curve order.
113+
String? _npubFor(String privHex) {
114+
if (privHex.isEmpty) return null;
115+
try {
116+
return fullNpub(derivePublicKeyHex(privHex));
117+
} on ArgumentError {
118+
return null;
119+
}
120+
}
121+
111122
class _IdentityRow extends StatelessWidget {
112123
const _IdentityRow({required this.nsec});
113124

114125
final String nsec;
115126

116127
@override
117128
Widget build(BuildContext context) {
118-
final privHex = nostr.Nip19.decode(payload: nsec).data;
119-
final npub = privHex.isNotEmpty
120-
? fullNpub(derivePublicKeyHex(privHex))
121-
: null;
129+
final npub = _npubFor(nostr.Nip19.decode(payload: nsec).data);
122130

123131
// The full npub is the canonical copy/share form (never raw hex); an
124132
// invalid identity is surfaced as unavailable and never copied.

‎mobile/test/features/settings/connection_section_test.dart‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,44 @@ void main() {
129129
expect(find.text('Public key (npub) copied'), findsOneWidget);
130130
}, variant: TargetPlatformVariant.only(TargetPlatform.iOS));
131131

132+
testWidgets('an out-of-range identity shows as unavailable', (tester) async {
133+
SharedPreferences.setMockInitialValues({});
134+
final prefs = await SharedPreferences.getInstance();
135+
136+
await tester.pumpWidget(
137+
WidgetHelpers.testable(
138+
overrides: [
139+
relayConfigProvider.overrideWith(_OutOfRangeRelayConfigNotifier.new),
140+
authProvider.overrideWith(_AuthNotifier.new),
141+
pairingProvider.overrideWith(
142+
() => _PairingNotifier(Future<bool>.value(true)),
143+
),
144+
savedPrefsProvider.overrideWithValue(prefs),
145+
],
146+
child: SettingsPage(
147+
profileHeader: const SizedBox.shrink(),
148+
identityRecoveryPageBuilder: (_) => const SizedBox.shrink(),
149+
),
150+
),
151+
);
152+
await tester.pump();
153+
await tester.ensureVisible(find.text('Copy public key (npub)'));
154+
await tester.pumpAndSettle();
155+
156+
expect(tester.takeException(), isNull);
157+
expect(
158+
find.ancestor(
159+
of: find.text('Copy public key (npub)'),
160+
matching: find.byWidgetPredicate(
161+
(widget) =>
162+
widget is Semantics &&
163+
widget.properties.value == 'Identity unavailable',
164+
),
165+
),
166+
findsOneWidget,
167+
);
168+
});
169+
132170
testWidgets('waits for a resumed frame before navigating after auth', (
133171
tester,
134172
) async {
@@ -346,6 +384,17 @@ class _RelayConfigNotifier extends RelayConfigNotifier {
346384
RelayConfig build() => RelayConfig(baseUrl: 'https://relay.test', nsec: nsec);
347385
}
348386

387+
class _OutOfRangeRelayConfigNotifier extends RelayConfigNotifier {
388+
// Valid bech32 and 64 hex chars, but not below the secp256k1 order.
389+
static final nsec = nostr.Nip19.encode(
390+
prefix: nostr.Nip19Prefix.nsec,
391+
data: 'f' * 64,
392+
);
393+
394+
@override
395+
RelayConfig build() => RelayConfig(baseUrl: 'https://relay.test', nsec: nsec);
396+
}
397+
349398
class _PairingNotifier extends PairingNotifier {
350399
_PairingNotifier(this.authorization);
351400

0 commit comments

Comments
 (0)