mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
`@jos` listed José María, `@jose` listed nobody and `@josé` listed them again: mobile only lowercased names. Name matching now follows section 3 of the shared mention rules, as buzz-app does (recommendation 6). Each code point is NFKD-normalized, its combining accents (the Combining Diacritical Marks blocks) are removed, it is lowercased and a final `ς` becomes `σ`, so `jose` matches `José`, `fiona` matches `fio` and `jose` matches `jose`, while `ø`, `ł`, `æ`, `ß`, `ı` and the marks of other scripts stay distinct. Words are runs of letters, marks and digits, so a hyphen or apostrophe separates them: `jane` finds `Mary-Jane` and `brien` finds `O'Brien`. The folding lives in `name_match.dart` and drives the chooser, Space selection, multi-word admission, the verified-name matches and the recipient pickers, so `@jose maria` lists José María with no accents typed. The vendored fixture moves from version 1 (95 cases) to version 4 of buzz-app's `mention-rules.fixtures.json` (125 cases), copied unchanged, and the conformance test runs every section, with a new `matching` group. No case is skipped. The rules the newer versions added are implemented too: an exact public key (64 hex in either case, or a complete lowercase or uppercase npub with an optional `nostr:` prefix) matches only its owner, even one with no name, and Space selects that owner; the viewer's own agent whose name continues the query with a new word leads (`Sol 6` for `sol`), and an exact person leads over it; agents from the viewer's other communities follow everyone here; an outside choice's longer name no longer blocks a member's exact name on Space, and a choice's own longer alias is not its own namesake. The search cache's exact-key exemption uses the same `typedPublicKey`. Refutation itself stays lowercase-only on purpose: it reasons about the relay's prefix search, which is out of scope here, so an accented query is a new search rather than a continuation. A refuted query still matches members on the device by folding: after `z` finds nobody, `@zoe` lists the member Zoë without a search. Tests: the compose bar search journey types `@jose maria` and sees José María, and `@zoe` after the `z` refutation sees Zoë; both and seven fixture cases fail when accent stripping is removed. `dart format`, `flutter analyze` and the full mobile `flutter test` suite pass. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Matt Toohey <contact@matttoohey.com>
115 lines
4.3 KiB
Dart
115 lines
4.3 KiB
Dart
import 'package:flutter/foundation.dart' show visibleForTesting;
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
/// Neutral label rendered when an identity string cannot be read as a Nostr
|
|
/// public key — malformed identities never leak truncated raw hex into the UI.
|
|
const String unknownIdentityLabel = 'Unknown identity';
|
|
|
|
/// Whether [value] is a 64-character hex public key (any case).
|
|
///
|
|
/// A character loop rather than a regular expression: AOT builds on iOS run
|
|
/// regular expressions in an interpreter, and identity views check every key
|
|
/// of a large roster on each rebuild.
|
|
bool isHexPubkey(String value) {
|
|
if (value.length != 64) return false;
|
|
for (var i = 0; i < 64; i++) {
|
|
final c = value.codeUnitAt(i);
|
|
if (!((c >= 0x30 && c <= 0x39) || // 0-9
|
|
(c >= 0x61 && c <= 0x66) || // a-f
|
|
(c >= 0x41 && c <= 0x46))) {
|
|
// A-F
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// The canonical full NIP-19 npub for [identity], or null when [identity] is
|
|
/// not a valid public key.
|
|
///
|
|
/// Accepts both canonical public-key forms — a 64-character hex key (any
|
|
/// case) or an `npub1…` string — and returns the full canonical npub.
|
|
/// Already-npub input round-trips through the codec so the result is always
|
|
/// the canonical lowercase encoding. Internal storage stays hex; the npub is
|
|
/// the user-facing display/copy form. Invalid input returns null (never a
|
|
/// passthrough of the raw string) so callers can suppress copy actions.
|
|
String? fullNpub(String identity) {
|
|
final trimmed = identity.trim();
|
|
try {
|
|
if (isHexPubkey(trimmed)) {
|
|
return _encodeNpub(trimmed.toLowerCase());
|
|
}
|
|
final decoded = nostr.Nip19.decode(payload: trimmed);
|
|
if (decoded.prefix != nostr.Nip19Prefix.npub ||
|
|
!isHexPubkey(decoded.data)) {
|
|
return null;
|
|
}
|
|
return _encodeNpub(decoded.data.toLowerCase());
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// The hex public key that typed text names exactly (portable mention rules,
|
|
/// Terms): 64 hex characters in either case, or a complete all-lowercase or
|
|
/// all-uppercase npub, with or without a `nostr:` prefix in any case. Null
|
|
/// for anything else, including a key still being typed, a mixed-case npub,
|
|
/// or one with a bad checksum or a payload of another length.
|
|
String? typedPublicKey(String text) {
|
|
final value = text.trim();
|
|
if (isHexPubkey(value)) return value.toLowerCase();
|
|
const scheme = 'nostr:';
|
|
final npub =
|
|
value.length > scheme.length &&
|
|
value.substring(0, scheme.length).toLowerCase() == scheme
|
|
? value.substring(scheme.length)
|
|
: value;
|
|
if (!npub.toLowerCase().startsWith('npub1')) return null;
|
|
try {
|
|
// bech32 rejects mixed case itself.
|
|
final decoded = nostr.Nip19.decode(payload: npub);
|
|
if (decoded.prefix != nostr.Nip19Prefix.npub ||
|
|
!isHexPubkey(decoded.data)) {
|
|
return null;
|
|
}
|
|
return decoded.data.toLowerCase();
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Encoded npubs by lowercase hex key. Identity views label every roster
|
|
/// member without a profile name by npub on each rebuild, and bech32 encoding
|
|
/// dominated those rebuilds on large channels. Bounded: once full it keeps
|
|
/// what it has, so a roster larger than the limit still reuses most keys.
|
|
final _npubs = <String, String>{};
|
|
@visibleForTesting
|
|
const npubCacheLimit = 8192;
|
|
|
|
String _encodeNpub(String hexPubkey) {
|
|
final cached = _npubs[hexPubkey];
|
|
if (cached != null) return cached;
|
|
final npub = nostr.Nip19.encode(
|
|
prefix: nostr.Nip19Prefix.npub,
|
|
data: hexPubkey,
|
|
);
|
|
if (_npubs.length < npubCacheLimit) _npubs[hexPubkey] = npub;
|
|
return npub;
|
|
}
|
|
|
|
/// The canonical compact public-key label: the first 8 and last 4 characters
|
|
/// of the full npub joined by an ellipsis — the same truncation desktop uses
|
|
/// for identity surfaces.
|
|
///
|
|
/// A compact label is a recognition aid, never a verification path; copy
|
|
/// actions expose the full npub via [fullNpub]. Accepts hex or npub input
|
|
/// (already-npub input yields the same label as its underlying key).
|
|
/// Returns [unknownIdentityLabel] when [identity] is not a valid public key.
|
|
String shortPubkey(String identity) {
|
|
final npub = fullNpub(identity);
|
|
if (npub == null) return unknownIdentityLabel;
|
|
return npub.length <= 12
|
|
? npub
|
|
: '${npub.substring(0, 8)}…${npub.substring(npub.length - 4)}';
|
|
}
|