Files
buzz/mobile/test/shared/verified_profiles/verified_profile_test.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

255 lines
8.5 KiB
Dart
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:buzz/shared/relay/relay.dart';
import 'package:buzz/shared/verified_profiles/verified_profile.dart';
import 'package:buzz/shared/verified_profiles/verified_profile_proof.dart';
import 'package:flutter_test/flutter_test.dart';
import 'verified_profile_fixture.dart';
VerifiedProfile? parse(NostrEvent event) =>
parseVerifiedProfile(event, authority, checked: true);
NostrEvent withTags(List<List<String>> tags, {int createdAt = 2000}) =>
signedProjection(tags, createdAt: createdAt);
void main() {
group('discovery', () {
Map<String, Object?> info({Object? fi, bool withFi = true, Object? self}) =>
{
'self': self ?? relayKeys.public,
'federated_identity': ?(withFi ? fi : null),
};
Map<String, Object?> pp([Object? kind = 30382, Object? multi = false]) => {
'public_profile': {'kind': kind, 'multiple_issuers': multi},
};
test('enabled, disabled and malformed are distinct', () {
final enabled = parseVerifiedProfileDiscovery(info(fi: pp(30382, true)));
expect(enabled, isA<VerifiedProfilesEnabled>());
expect(
(enabled as VerifiedProfilesEnabled).authority,
VerifiedProfileAuthority(
pubkey: relayKeys.public,
multipleIssuers: true,
),
);
expect(
parseVerifiedProfileDiscovery(info(withFi: false)),
isA<VerifiedProfilesDisabled>(),
);
expect(
parseVerifiedProfileDiscovery(info(fi: <String, Object?>{})),
isA<VerifiedProfilesDisabled>(),
);
for (final bad in <Object?>[
'nope',
info(fi: 'x'),
info(fi: {'public_profile': 'x'}),
info(fi: pp(1)),
info(fi: pp(30382, 'yes')),
info(fi: pp(), self: 'A' * 64),
info(fi: pp(), self: 'abc'),
]) {
expect(
() => parseVerifiedProfileDiscovery(bad),
throwsFormatException,
reason: '$bad',
);
}
});
});
group('names', () {
test('accepts and rejects per NIP-FI', () {
for (final ok in ['Ada', 'Zoë Smith', '日本語', 'a' * 64]) {
expect(isValidVerifiedName(ok), isTrue, reason: ok);
}
for (final bad in [
'',
'a' * 65,
' Ada',
'Ada ',
'Ada ',
'A\u0000da',
'A​da',
'Zoë',
'A
da',
'Ada',
]) {
expect(
isValidVerifiedName(bad),
isFalse,
reason: bad.codeUnits.join(','),
);
}
});
});
group('projection', () {
test('a well-formed projection parses, with and without full_name', () {
final profile = parse(withTags(profileTags(subjectA)))!;
expect(profile.displayName, 'Ada Lovelace');
expect(profile.username, 'ada');
expect(profile.issuerLabel, 'Example Corp');
expect(profile.verifiedAt, 1000);
expect(profile.fullName, isNull);
final full = parse(withTags(profileTags(subjectA, fullName: 'Ada K')))!;
expect(full.fullName, 'Ada K');
});
test('rejects every deviation from the event shape', () {
final good = profileTags(subjectA);
List<List<String>> without(String name) => [
for (final t in good)
if (t.first != name) t,
];
List<List<String>> replace(String name, String value) => [
for (final t in good) t.first == name ? [name, value] : t,
];
final cases = <String, NostrEvent>{
'wrong author': signedProjection(good, author: relayKeysOther()),
'retraction': withTags(retractionTags(subjectA)),
'p differs from d': withTags(replace('p', subjectB)),
'duplicate tag': withTags([
...good,
['username', 'ada2'],
]),
'extra tag': withTags([
...good,
['x', 'y'],
]),
'two full_name': withTags([
...good,
['full_name', 'A'],
['full_name', 'B'],
]),
'three-element tag': withTags([
for (final t in good) t.first == 'p' ? [...t, 'x'] : t,
]),
'missing display_name': withTags([
...without('display_name'),
['x', 'y'],
]),
'bad username': withTags(replace('username', 'Ada!')),
'long username': withTags(replace('username', 'a' * 65)),
'non-NFC name': withTags(replace('display_name', 'Zoë')),
'padded name': withTags(replace('display_name', ' Ada')),
'bad issuer': withTags(replace('issuer_label', '')),
'bad full_name': withTags([
...good,
['full_name', ' x'],
]),
'inactive text': withTags(replace('active', 'TRUE')),
'verified_at after created_at': withTags(
replace('verified_at', '2001'),
),
'verified_at not decimal': withTags(replace('verified_at', '1e3')),
'verified_at negative': withTags(replace('verified_at', '-5')),
'uppercase subject': withTags([
['d', 'A' * 64],
...good.skip(1),
]),
'content not empty': signedProjection(good, content: 'hi'),
};
for (final MapEntry(:key, :value) in cases.entries) {
expect(parse(value), isNull, reason: key);
}
});
test('verified_at equal to created_at is accepted', () {
expect(
parse(withTags(profileTags(subjectA, verifiedAt: '2000'))),
isNotNull,
);
});
});
group('ordering and proof', () {
test('newer created_at wins; a tie goes to the lower id', () {
final older = withTags(profileTags(subjectA), createdAt: 10);
final newer = withTags(profileTags(subjectA, name: 'New'), createdAt: 11);
expect(supersedes(newer, older), isTrue);
expect(supersedes(older, newer), isFalse);
final x = withTags(profileTags(subjectA, name: 'X'), createdAt: 20);
final y = withTags(profileTags(subjectA, name: 'Y'), createdAt: 20);
final lower = x.id.compareTo(y.id) < 0 ? x : y;
final higher = identical(lower, x) ? y : x;
expect(supersedes(lower, higher), isTrue);
expect(supersedes(higher, lower), isFalse);
expect(supersedes(older, null), isTrue);
});
test('signature and id are verified', () {
final event = withTags(profileTags(subjectA));
expect(hasVerifiedEventProof(event), isTrue);
final forgedSig = NostrEvent(
id: event.id,
pubkey: event.pubkey,
createdAt: event.createdAt,
kind: event.kind,
tags: event.tags,
content: event.content,
sig: '0' * 128,
);
expect(hasVerifiedEventProof(forgedSig), isFalse);
final tampered = NostrEvent(
id: event.id,
pubkey: event.pubkey,
createdAt: event.createdAt,
kind: event.kind,
tags: profileTags(subjectA, name: 'Mallory'),
content: event.content,
sig: event.sig,
);
expect(hasVerifiedEventProof(tampered), isFalse);
});
});
test('a profile search finds people by kind:0, or by the relay\'s head', () {
NostrEvent profile(String pubkey, String name, int createdAt) => NostrEvent(
id: '$pubkey$createdAt',
pubkey: pubkey,
createdAt: createdAt,
kind: 0,
tags: const [],
content: '{"name":"$name"}',
sig: 'sig',
);
final c = 'c' * 64, d = 'd' * 64, e = 'e' * 64, f = 'f' * 64;
final events = [
// A head-only person keeps the relay's rank.
signedProjection(profileTags(c, name: 'Gabi Lilienfeld')),
// A kind:0 wins over the head; the newest kind:0 counts.
profile(subjectA, 'old', 1),
signedProjection(profileTags(subjectA)),
profile(subjectA, 'Ada', 2),
// These find nobody: another key's head, a retraction, a bad subject.
signedProjection(profileTags(d), author: relayKeysOther()),
signedProjection(retractionTags(e)),
signedProjection(profileTags('F' * 64)),
profile(subjectB, 'Bea', 1),
NostrEvent(
id: f,
pubkey: f,
createdAt: 1,
kind: 1,
tags: const [],
content: 'note',
sig: 'sig',
),
];
String row(SearchedPerson person) =>
'${person.pubkey[0]}:'
'${person.verified?.displayName ?? 'kind0 ${person.profile!.content}'}';
expect(searchedPeople(events, authority).map(row), [
'c:Gabi Lilienfeld',
'a:kind0 {"name":"Ada"}',
'b:kind0 {"name":"Bea"}',
]);
// Without a known authority, no head finds anyone.
expect(searchedPeople(events, null).map(row), [
'a:kind0 {"name":"Ada"}',
'b:kind0 {"name":"Bea"}',
]);
});
}