Files
buzz/mobile/lib/shared/hosted/nip_fi_discovery.dart
Tom Brow b58ee08404 fix(mobile): request hosted relay assertions only when NIP-FI is required (#9225)
Port the desktop NIP-FI requirement rule to mobile: hosted relay connections request an account assertion only when the relay's NIP-11 document requires NIP-FI, so communities that have not enabled it keep connecting with BUZZ_HOSTED_RELAY_AUTH on.

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:57:05 -08:00

48 lines
1.7 KiB
Dart

import 'package:http/http.dart' as http;
import '../relay/nip11_document.dart';
/// Whether the relay at [origin] requires NIP-FI admission evidence, read from
/// its NIP-11 document. Throws on any failure or malformed document.
typedef NipFiRequirementFetch = Future<bool> Function(Uri origin);
/// Reads [origin]'s NIP-11 document from its root, without following
/// redirects; see [fetchNip11Document].
Future<bool> fetchNipFiRequirement(
http.Client client,
Uri origin, {
Duration deadline = nip11Deadline,
}) async => parseNipFiRequirement(
await fetchNip11Document(
client,
Uri.parse(origin.origin).replace(path: '/'),
deadline: deadline,
followRedirects: false,
),
);
/// NIP-FI Discovery: `limitation.federated_identity` advertises support, as a
/// shadow-mode host does too; the host-specific `requires_nip_fi` says this
/// community requires evidence. A relay without the flag requires what it
/// supports. Same requirement rule as buzz-app's desktop discovery.
bool parseNipFiRequirement(Object? document) {
if (document is! Map<String, dynamic>) {
throw const FormatException('NIP-11 document is not an object');
}
final supports = switch (document['limitation']) {
null => false,
final Map<String, dynamic> limitation =>
switch (limitation['federated_identity']) {
null || false => false,
true => true,
_ => throw const FormatException('Malformed federated_identity'),
},
_ => throw const FormatException('Malformed NIP-11 limitation'),
};
return switch (document['requires_nip_fi']) {
null => supports,
final bool requires => requires,
_ => throw const FormatException('Malformed requires_nip_fi'),
};
}