Files
meshcore-open/lib/helpers/contact_share_message.dart
T
zjs81 c939f7a29c Add Czech, in-chat search, typed-hash path trace and contact sharing
- Czech UI locale (app_cs.arb), selectable in the language picker and as a
  message translation language; language names added to every locale.
- Fix wrong language names: Slovak shown as Slovenian in it/ja/pt/sl,
  garbled Slovak/Bulgarian in pl, Ukrainian in ru, Hungarian in ko/zh.
- Search inside DM and channel chats across the full stored history, with
  highlighted matches; filtered channel messages are excluded.
- Path trace by typed hashes (e.g. A1,B2,C3) from the Contacts and map
  menus, without needing node locations; the map trace panel can hand its
  hops to it.
- Share contacts in chats using the official app's <pubkey:type:name>
  format; received cards have a one-tap Add contact button that reuses the
  QR card import path.
- Translations for all new strings in every locale; docs updated.
2026-11-07 22:31:14 -07:00

64 lines
1.8 KiB
Dart

import 'dart:typed_data';
import '../utils/contact_qr.dart';
/// A contact shared as a chat message: `<PUBKEYHEX:TYPE:NAME>`.
class ContactShare {
final Uint8List publicKey;
final String publicKeyHex;
final int type;
final String name;
const ContactShare({
required this.publicKey,
required this.publicKeyHex,
required this.type,
required this.name,
});
/// First 6 + "..." + last 6 hex chars, as the official app shows it.
String get shortKey => shortContactKey(publicKeyHex);
ScannedContact toScannedContact() =>
ScannedContact(publicKey: publicKey, name: name, type: type);
}
String shortContactKey(String hex) => hex.length <= 12
? hex
: '${hex.substring(0, 6)}...${hex.substring(hex.length - 6)}';
const int _keyBytes = 32;
final RegExp _tokenPattern = RegExp(r'^<([0-9a-fA-F]{64}):([1-4]):([^>]*)>$');
/// Builds the share token for a contact. [publicKey] must be 32 bytes.
String encodeContactShare({
required Uint8List publicKey,
required int type,
required String name,
}) {
assert(publicKey.length == _keyBytes);
final hex = StringBuffer();
for (final b in publicKey) {
hex.write(b.toRadixString(16).padLeft(2, '0'));
}
final cleanName = name.replaceAll(RegExp(r'[<>\r\n]'), '').trim();
return '<$hex:$type:$cleanName>';
}
/// Returns null unless the whole trimmed [text] is a valid share token.
ContactShare? parseContactShare(String text) {
final match = _tokenPattern.firstMatch(text.trim());
if (match == null) return null;
final hex = match.group(1)!.toLowerCase();
final bytes = Uint8List(_keyBytes);
for (var i = 0; i < _keyBytes; i++) {
bytes[i] = int.parse(hex.substring(i * 2, i * 2 + 2), radix: 16);
}
return ContactShare(
publicKey: bytes,
publicKeyHex: hex,
type: int.parse(match.group(2)!),
name: match.group(3)!.trim(),
);
}