Files
buzz/mobile/lib/shared/verified_profiles/verified_profile.dart
Logan Johnson c39ee507b2 fix(mobile): find verified people who have no profile (#9086)
## What changes

Mobile now finds a person who has a relay-verified profile (NIP-FI, kind
30382) but never published a kind:0 profile. This happens, for example,
when someone skips profile setup. The relay's `POST /profiles/search`
already finds such people by their verified name. Each one comes back as
a kind:30382 head with no kind:0. Mobile kept only kind:0 results, so
these people did not appear.

- `RelaySessionNotifier.searchProfiles` keeps kind:30382 heads from the
endpoint.
- `VerifiedProfilesNotifier.searched` turns a search page into people:
- If the relay authority is not known yet, it first waits for NIP-11
discovery. Otherwise, a search that finished first would drop these
people for that query.
- It checks each head's signature with the directory's off-isolate proof
checker.
- Proven heads go into the verified directory, so the rows show verified
names right away.
- `searchedPeople` (in `verified_profile.dart`) builds one row per
person, in the relay's order:
  - If the person has a kind:0, the newest kind:0 is used, as before.
- If not, it uses the proven head that the authority signed, provided it
parses as an active verified profile.
- These cases add no row:
  - a head signed by another key;
  - a head that names the relay key but has a bad signature;
  - a retraction;
  - a malformed subject.
- **Behavior change for all mobile mentions:** every verified person now
matches in mention ranking by their verified full name and username, as
well as by display name, including people who have a kind:0. The relay's
search matches these same three names. Without this, a person whose
display name is their username (`glilienfeld`, full name "Gabi
Lilienfeld") came back from the relay for "gabi", and then the local
filter dropped them.
- Callers:
  - Mention search, which also serves Add members and New DM.
  - The user rows in global search, through `directoryUsersFromSearch`.

The desktop app has the same fix for Manage members:
https://github.com/block/buzz-app/pull/2554

## Screenshots

These come from the real Add members sheet on an Android emulator. Only
the relay session, verified-profile discovery and the member actions are
faked. The names are made up. Jordan Park has a kind:0. jrivera has only
a verified profile, with the full name "Jordan Rivera". The search is
"jordan". The row shows the verified display name, which here is the
same as the username.

| Search finds jrivera | After tapping + on jrivera |
| --- | --- |
| <img width="300" alt="jrivera is found next to Jordan Park"
src="https://github.com/user-attachments/assets/d93b77b1-1bc7-4d09-afc1-f359616a192a"
/> | <img width="300" alt="jrivera is selected to add"
src="https://github.com/user-attachments/assets/ce6c7a71-b098-434e-8b0e-ed0de8d1a5a2"
/> |

## Tests

- `add_members_sheet_names_test.dart`: a journey in which Gabi has
`display_name` = `glilienfeld` and `full_name` = "Gabi Lilienfeld", and
NIP-11 discovery is slow.
  - While discovery is pending, typing "gabi" shows nothing yet.
  - When discovery finishes, Gabi appears with no further keystroke.
- A head from another key does not appear, and neither does a head that
names the relay key but has a bad signature.
  - **Add member** adds Gabi's key.
- I removed each fix in turn: the full-name alias, the signature proof,
and the wait for discovery. The journey fails each time, for the
expected reason.
- `verified_profile_test.dart`: a table of `searchedPeople` cases.
  - A kind:0 wins over a head, and the newest kind:0 counts.
- These find nobody: another signer, a retraction, an uppercase subject,
a non-profile kind, and any head when the authority is unknown.
- `relay_session_test.dart` and `channel_management_provider_test.dart`:
rows for the kept heads and the directory row.
- `flutter analyze` is clean, and the full `flutter test` passes on
6efef79cc: 4,462 tests.

**Note:** mobile on `main` does not compile at e558f15fa.
`account_identity_store.dart` calls the removed `_keys`. That fix is
https://github.com/block/buzz/pull/9085. The local runs above had that
one-line fix applied but not committed. Until it lands, mobile CI on
this PR will fail.

## Not covered

- **Search during a reconnect:** discovery starts only when the relay
session is connected. If an HTTP search succeeds while the session is
reconnecting, that search leaves these people out.
- **Discovery failure:** if NIP-11 discovery fails, the search also
leaves these people out.
- In both cases the mention cache keeps that partial page for the query,
and the next search tries discovery again. People who have a kind:0 show
as before. Showing the left-out people when the authority arrives later
would need the cached results to be rebuilt; that is a possible
follow-up.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Logan Johnson <loganj@squareup.com>
Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 18:53:40 -05:00

348 lines
11 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:unorm_dart/unorm_dart.dart' as unorm;
import '../relay/nostr_models.dart';
/// NIP-FI verified profile projection: a NIP-85 user assertion (kind 30382)
/// signed by the community relay's NIP-11 `self` key.
const verifiedProfileKind = 30382;
/// Subjects per subscription. Keeps REQs inside relay filter and message limits.
const verifiedProfileBatchSize = 128;
final RegExp _lowerHex64 = RegExp(r'^[0-9a-f]{64}$');
final RegExp _lowerHex128 = RegExp(r'^[0-9a-f]{128}$');
final RegExp _username = RegExp(r'^[a-z0-9._-]{1,64}$');
final RegExp _decimal = RegExp(r'^[0-9]+$');
final RegExp _prohibited = RegExp(
r'[\p{Cc}\p{Cf}\p{Cs}\p{Co}\p{Cn}\p{Zl}\p{Zp}]',
unicode: true,
);
final RegExp _edgeWhitespace = RegExp(
r'^\p{White_Space}|\p{White_Space}$',
unicode: true,
);
/// The community key against which projections are verified, as advertised by
/// a successful NIP-11 discovery that enables `public_profile`.
@immutable
class VerifiedProfileAuthority {
const VerifiedProfileAuthority({
required this.pubkey,
required this.multipleIssuers,
});
final String pubkey;
final bool multipleIssuers;
@override
bool operator ==(Object other) =>
other is VerifiedProfileAuthority &&
other.pubkey == pubkey &&
other.multipleIssuers == multipleIssuers;
@override
int get hashCode => Object.hash(pubkey, multipleIssuers);
}
/// The outcome of a *successful* discovery response. A failed or malformed
/// response is not an outcome: it throws and must not replace prior state.
sealed class VerifiedProfileDiscovery {
const VerifiedProfileDiscovery();
}
/// `federated_identity.public_profile` is present, valid and bound to `self`.
class VerifiedProfilesEnabled extends VerifiedProfileDiscovery {
const VerifiedProfilesEnabled(this.authority);
final VerifiedProfileAuthority authority;
}
/// A valid response omits `federated_identity` or `public_profile`: an
/// explicit operator disable that hides every mark, including cached ones.
class VerifiedProfilesDisabled extends VerifiedProfileDiscovery {
const VerifiedProfilesDisabled();
}
/// Parses a decoded NIP-11 document.
///
/// Throws [FormatException] for a malformed document, which callers treat as
/// a failed discovery (the previous state stands).
VerifiedProfileDiscovery parseVerifiedProfileDiscovery(Object? info) {
if (info is! Map) throw const FormatException('NIP-11 is not an object');
final federated = info['federated_identity'];
if (federated == null && !info.containsKey('federated_identity')) {
return const VerifiedProfilesDisabled();
}
if (federated is! Map) {
throw const FormatException('Malformed federated_identity');
}
if (!federated.containsKey('public_profile')) {
return const VerifiedProfilesDisabled();
}
final profile = federated['public_profile'];
final self = info['self'];
if (profile is! Map ||
self is! String ||
!_lowerHex64.hasMatch(self) ||
profile['kind'] != verifiedProfileKind ||
profile['multiple_issuers'] is! bool) {
throw const FormatException('Malformed public_profile discovery');
}
return VerifiedProfilesEnabled(
VerifiedProfileAuthority(
pubkey: self,
multipleIssuers: profile['multiple_issuers'] as bool,
),
);
}
/// A name, username-free label, or issuer label: 1 to 64 scalar values in NFC,
/// no `Cc`, `Cf`, `Cs`, `Co`, `Cn`, `Zl` or `Zp` characters, and no
/// `White_Space` at either end.
bool isValidVerifiedName(String value) {
final length = value.runes.length;
return length >= 1 &&
length <= 64 &&
unorm.nfc(value) == value &&
!_prohibited.hasMatch(value) &&
!_edgeWhitespace.hasMatch(value);
}
/// A verified name as the relay last published it. Historical: it records the
/// issuer's name-to-key assertion at [verifiedAt], never current access.
@immutable
class VerifiedProfile {
const VerifiedProfile({
required this.displayName,
required this.username,
required this.issuerLabel,
required this.verifiedAt,
required this.checked,
required this.multipleIssuers,
this.fullName,
});
final String displayName;
final String username;
final String? fullName;
final String issuerLabel;
/// Unix seconds of the issuer assertion the relay verified.
final int verifiedAt;
/// True only when reconciled with the relay on the current connection and a
/// fresh discovery; otherwise this is a cached record.
final bool checked;
final bool multipleIssuers;
VerifiedProfile withChecked(bool value) => value == checked
? this
: VerifiedProfile(
displayName: displayName,
username: username,
fullName: fullName,
issuerLabel: issuerLabel,
verifiedAt: verifiedAt,
checked: value,
multipleIssuers: multipleIssuers,
);
@override
bool operator ==(Object other) =>
other is VerifiedProfile &&
other.displayName == displayName &&
other.username == username &&
other.fullName == fullName &&
other.issuerLabel == issuerLabel &&
other.verifiedAt == verifiedAt &&
other.checked == checked &&
other.multipleIssuers == multipleIssuers;
@override
int get hashCode => Object.hash(
displayName,
username,
fullName,
issuerLabel,
verifiedAt,
checked,
multipleIssuers,
);
}
/// The total size, in UTF-16 code units of content and every tag element
/// (plus one per tag and per element), above which an event is dropped before any buffering or proof work. A valid
/// projection is at most about 600 (eight two-element tags of at most 128), so
/// a small malformed event still takes its address, while a full pre-EOSE
/// buffer stays near 2 MB.
const _maxEventSize = 4096;
int? _boundedSize(NostrEvent event) {
var size = event.content.length;
if (size > _maxEventSize) return null;
for (final tag in event.tags) {
// Each tag and element counts at least one, so empty ones are bounded too.
size += 1;
for (final value in tag) {
size += value.length + 1;
if (size > _maxEventSize) return null;
}
if (size > _maxEventSize) return null;
}
return size;
}
/// The subject a kind 30382 event addresses: its first `d` tag, when the
/// event is by [authority], within [_maxEventSize], and the tag is a
/// lowercase hex pubkey.
///
/// Projection shape is not checked here: a newer signed event with a
/// malformed profile still replaces the older one at this address, so it
/// clears the earlier verified name instead of preserving it (only the latest
/// projection counts). Callers must have verified id and signature.
String? verifiedProfileSubject(
NostrEvent event,
VerifiedProfileAuthority authority,
) {
if (event.kind != verifiedProfileKind ||
event.pubkey != authority.pubkey ||
// Metadata is bounded too: an id or signature is fixed-size hex.
!_lowerHex64.hasMatch(event.id) ||
!_lowerHex128.hasMatch(event.sig) ||
_boundedSize(event) == null) {
return null;
}
for (final tag in event.tags) {
if (tag.isNotEmpty && tag.first == 'd') {
return tag.length > 1 && _lowerHex64.hasMatch(tag[1]) ? tag[1] : null;
}
}
return null;
}
/// NIP-01 replaceable ordering: the later `created_at` wins, and a tie goes to
/// the lower event id.
bool supersedes(NostrEvent candidate, NostrEvent? current) =>
current == null ||
candidate.createdAt > current.createdAt ||
(candidate.createdAt == current.createdAt &&
candidate.id.compareTo(current.id) < 0);
/// Strictly validates the projection carried by [event] (whose id and
/// signature the caller has already verified).
///
/// Returns null unless the event is the shape NIP-FI defines: empty content;
/// exactly seven tags (eight with `full_name`), each of two elements with no
/// repeated name; `d` and `p` equal; `active` equal to `true`; well-formed
/// names, username and issuer label; and a `verified_at` no later than
/// `created_at`. A retraction (`active=false`) is therefore null.
VerifiedProfile? parseVerifiedProfile(
NostrEvent event,
VerifiedProfileAuthority authority, {
required bool checked,
}) {
final subject = verifiedProfileSubject(event, authority);
if (subject == null ||
event.content != '' ||
(event.tags.length != 7 && event.tags.length != 8)) {
return null;
}
final tags = <String, String>{};
for (final tag in event.tags) {
if (tag.length != 2 || tags.containsKey(tag[0])) return null;
tags[tag[0]] = tag[1];
}
const required = {
'd',
'p',
'active',
'display_name',
'username',
'issuer_label',
'verified_at',
};
if (!required.every(tags.containsKey) ||
tags.length !=
required.length + (tags.containsKey('full_name') ? 1 : 0)) {
return null;
}
final fullName = tags['full_name'];
final displayName = tags['display_name']!;
final username = tags['username']!;
final issuerLabel = tags['issuer_label']!;
final rawVerifiedAt = tags['verified_at']!;
if (tags['p'] != subject ||
tags['active'] != 'true' ||
(fullName != null && !isValidVerifiedName(fullName)) ||
!isValidVerifiedName(displayName) ||
!_username.hasMatch(username) ||
!isValidVerifiedName(issuerLabel) ||
!_decimal.hasMatch(rawVerifiedAt)) {
return null;
}
final verifiedAt = int.tryParse(rawVerifiedAt);
if (verifiedAt == null || verifiedAt > event.createdAt) return null;
return VerifiedProfile(
displayName: displayName,
username: username,
fullName: fullName,
issuerLabel: issuerLabel,
verifiedAt: verifiedAt,
checked: checked,
multipleIssuers: authority.multipleIssuers,
);
}
/// The verified names besides the display name that the relay's profile
/// search matches: the full name, when there is one, and the username.
List<String> verifiedAliases(VerifiedProfile profile) => [
?profile.fullName,
profile.username,
];
/// One person a profile search found: their newest kind:0, or, for someone
/// with none, their verified profile.
typedef SearchedPerson = ({
String pubkey,
NostrEvent? profile,
VerifiedProfile? verified,
});
/// The people a profile search page found, in the relay's order: each
/// person's newest kind:0, and for a person with none, the head [authority]
/// signed, when it is an active verified profile. Any other event finds
/// nobody. Callers must have verified each head's id and signature
/// ([VerifiedProfilesNotifier.searched] does).
List<SearchedPerson> searchedPeople(
List<NostrEvent> events,
VerifiedProfileAuthority? authority,
) {
final order = <String>{};
final profiles = <String, NostrEvent>{};
final heads = <String, NostrEvent>{};
for (final event in events) {
if (event.kind == 0) {
final pubkey = event.pubkey.toLowerCase();
order.add(pubkey);
final current = profiles[pubkey];
if (current == null || event.createdAt > current.createdAt) {
profiles[pubkey] = event;
}
} else if (authority != null) {
final subject = verifiedProfileSubject(event, authority);
if (subject == null) continue;
order.add(subject);
if (supersedes(event, heads[subject])) heads[subject] = event;
}
}
return [
for (final pubkey in order)
if (profiles[pubkey] case final profile?)
(pubkey: pubkey, profile: profile, verified: null)
else if (parseVerifiedProfile(heads[pubkey]!, authority!, checked: false)
case final verified?)
(pubkey: pubkey, profile: null, verified: verified),
];
}