Files
buzz/mobile/lib/shared/profile/user_profile.dart
Stephen Edwards 36e6e4f8e7 fix(mobile): apply message edits only from authorized authors (#9887)
## Why

Buzz mobile now checks who signed each edit before deciding what to show
in a channel or thread. That includes edits to the first message of a
thread loaded from local storage. Mobile also preserves the information
needed to verify edits after an offline restart and retries missing
profile lookups when the app reconnects.

The companion relay change is #9886.

## Mechanism

Mobile uses the first valid message reference on an edit and shows the
newest edit that passes its checks. It accepts edits from the original
message’s signer. If the relay signed the original, mobile also accepts
edits from the author named by the relay. A verified agent owner can
edit the agent’s chat messages (kinds 9 and 40002) and diff messages
(kind 40008) in the same channel.

**Agent owners.** Mobile verifies the owner named in the agent’s signed
profile. The app reads permission when a user opens the channel or
thread message menu or the image actions. The native photo viewer also
reads permission when it opens and checks again before deleting. These
paths and the timeline use the same signed profile to verify the owner.

When an agent’s signed profile names an owner, mobile saves the signed
event once and reads fields such as the name and avatar from it on
restart. If that entry exceeds the 16 KiB limit, mobile saves the
display fields alone if they fit. Mobile needs the signed profile to
verify the owner. When the app connects, it asks for profiles needed to
check edits in the current timeline and retries unresolved lookups on
reconnect. The cache skips profiles it has already checked with the
relay and public keys the relay recently reported as having no profile.
Once mobile loads a newer profile from the relay that no longer names
the owner, it stops applying that person’s edits. The next time that
person opens the message menu, Edit is absent. In the device test, both
changes took effect after restarting the app.

**Relay signing key.** While connected, mobile looks up the key if the
timeline contains an edit whose signer differs from the original
message’s signer and the original has a public key in an `actor` or `p`
tag. A mention in a `p` tag can also trigger the lookup. Mobile uses
that tagged author only after confirming that the relay signed the
original message.

Mobile keeps the last key during reconnects and can read it from the
saved-message store after an offline restart. If the timeline still
needs the key when mobile reconnects, mobile refreshes it so a rotated
key can take effect during the same session. A failed request or a
response without a key leaves the previous key available. Thread
summaries read the key available when they refresh and start no key
lookup.

## What changed

- Start with `edit_authority.dart` for the profile and key lookups and
the permission check that runs when actions open. See
`timeline_message.dart` for edit selection and
`thread_detail_helpers.dart` for the first message of a thread loaded
from local storage.
- See `verified_profile_owner.dart` for ownership verification,
`profile_snapshot.dart` for storage and restore,
`user_cache_provider.dart` for profile refreshes, and
`SavedMessageStore.relayAuthor` for the saved relay key.
- Tests cover edit selection, owner edits to chat and diff messages, and
edits to a saved thread’s first message after replies load. They also
cover opening message and image actions, restored profiles without a
signed copy and profiles signed by the wrong key, reconnect retries,
offline reopening with a large signed profile, and a newly fetched relay
key replacing a saved key. Tests also verify that the native photo
viewer blocks deletion if permission is withdrawn while it is open, that
opening message actions requests a fresh profile when the saved profile
names the viewer as owner but lacks a signed copy, and that a tampered
saved profile cannot verify ownership.

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

---------

Signed-off-by: Stephen Edwards <sedwards@squareup.com>
2026-11-24 10:34:52 -05:00

73 lines
2.5 KiB
Dart

import 'package:flutter/foundation.dart';
import '../relay/nostr_models.dart';
import '../utils/string_utils.dart';
@immutable
class UserProfile {
final String pubkey;
final String? displayName;
final String? avatarUrl;
final String? about;
final String? nip05Handle;
/// NIP-OA verified owner pubkey from the profile's `auth` tag; non-null
/// means this identity is an agent (mirrors desktop's `ownerPubkey`).
final String? ownerPubkey;
/// The signed kind:0 this profile was read from, kept only when it carries
/// an owner attestation. [ownerPubkey] can come from copies whose signature
/// was never checked, so permission to act for this identity re-verifies
/// this event instead (`verifiedProfileOwnerProvider`). Null means no
/// attestation, or one not yet fetched.
final NostrEvent? ownerEvidence;
bool get isAgent => ownerPubkey != null;
const UserProfile({
required this.pubkey,
this.displayName,
this.avatarUrl,
this.about,
this.nip05Handle,
this.ownerPubkey,
this.ownerEvidence,
});
factory UserProfile.fromJson(Map<String, dynamic> json) => UserProfile(
pubkey: json['pubkey'] as String,
displayName: json['display_name'] as String?,
avatarUrl: json['avatar_url'] as String?,
about: json['about'] as String?,
nip05Handle: json['nip05_handle'] as String?,
);
/// Short label: display name, or the compact npub form of the public key.
///
/// Blank display names (empty or whitespace-only) fall back to the compact
/// npub too — relay profiles can carry them — so a valid identity never
/// renders an empty label, like [initial]. Nonblank names render as
/// authored: trim only tests blankness, so authored padding survives
/// (unlike [initial], which reads the trimmed padding).
String get label {
final name = displayName;
return name != null && name.trim().isNotEmpty ? name : shortPubkey(pubkey);
}
/// First letter for fallback avatar.
String get initial {
final name = displayName?.trim();
if (name != null && name.isNotEmpty) return name[0].toUpperCase();
// Hex-derived (not npub-derived) so unnamed identities keep distinct
// initials instead of every npub rendering `N`.
return pubkey.isNotEmpty ? pubkey[0].toUpperCase() : '?';
}
}
/// Optional profile handle shown beside a message author's display name.
String? messageUsernameLabel(UserProfile? profile) {
final handle = profile?.nip05Handle?.trim();
if (handle != null && handle.isNotEmpty) return handle;
return null;
}