Add contact import from QR code
Contacts could only be imported by pasting `meshcore://<hex advert>` from the clipboard, so contact QR codes and links shared by the official app (`meshcore://contact/add?name=...&public_key=...&type=...`) could not be used at all. Add a contact QR scanner screen, reachable from the contacts menu, that scans with the camera or decodes a QR code from a photo in the gallery. Both the official contact-card link and the existing hex advert form are accepted; cards are applied with CMD_ADD_UPDATE_CONTACT and advert blobs keep using CMD_IMPORT_CONTACT. Fixes #529
This commit is contained in:
28 files changed
+1231
-16
No files matched your search
@@ -0,0 +1,101 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
/// Parsed result of a MeshCore contact QR / link.
|
||||
class ScannedContact {
|
||||
/// Raw advert packet bytes (legacy `meshcore://<hex>` form), else null.
|
||||
final Uint8List? advertPacket;
|
||||
|
||||
/// 32-byte public key (contact-card form), else null.
|
||||
final Uint8List? publicKey;
|
||||
|
||||
/// Contact name from the card form; empty string when unknown.
|
||||
final String name;
|
||||
|
||||
/// Advert type: 1 Companion, 2 Repeater, 3 Room Server, 4 Sensor.
|
||||
final int type;
|
||||
|
||||
const ScannedContact({
|
||||
this.advertPacket,
|
||||
this.publicKey,
|
||||
this.name = '',
|
||||
this.type = 1,
|
||||
});
|
||||
|
||||
bool get isAdvertPacket => advertPacket != null;
|
||||
}
|
||||
|
||||
/// Matches the advert packet minimum enforced by `buildImportContactFrame`.
|
||||
const int _minAdvertPacketBytes = 98;
|
||||
|
||||
/// `buildImportContactFrame` prepends one command byte to a `maxFrameSize`
|
||||
/// (172 byte) frame, so anything longer cannot be sent.
|
||||
const int _maxAdvertPacketBytes = 171;
|
||||
const int _contactCardKeyBytes = 32;
|
||||
const String _legacyPrefix = 'meshcore://';
|
||||
|
||||
/// Returns null when [data] is not a recognizable MeshCore contact QR/link.
|
||||
ScannedContact? parseContactQr(String data) {
|
||||
final trimmed = data.trim();
|
||||
if (trimmed.isEmpty) return null;
|
||||
|
||||
final uri = Uri.tryParse(trimmed);
|
||||
if (uri == null || uri.scheme != 'meshcore') return null;
|
||||
|
||||
final normalizedPath = '${uri.host}${uri.path}'.toLowerCase().replaceAll(
|
||||
RegExp(r'^/+|/+$'),
|
||||
'',
|
||||
);
|
||||
if (normalizedPath == 'contact/add') {
|
||||
return _parseContactCard(uri);
|
||||
}
|
||||
|
||||
if (!trimmed.toLowerCase().startsWith(_legacyPrefix)) return null;
|
||||
final hexString = trimmed.substring(_legacyPrefix.length);
|
||||
final bytes = _tryHexDecode(hexString);
|
||||
if (bytes == null ||
|
||||
bytes.length < _minAdvertPacketBytes ||
|
||||
bytes.length > _maxAdvertPacketBytes) {
|
||||
return null;
|
||||
}
|
||||
return ScannedContact(advertPacket: bytes);
|
||||
}
|
||||
|
||||
ScannedContact? _parseContactCard(Uri uri) {
|
||||
final params = uri.queryParameters;
|
||||
final publicKeyHex = params['public_key'];
|
||||
if (publicKeyHex == null) return null;
|
||||
|
||||
final publicKey = _tryHexDecode(publicKeyHex);
|
||||
if (publicKey == null || publicKey.length != _contactCardKeyBytes) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final name = (params['name'] ?? '').trim();
|
||||
int type = int.tryParse(params['type'] ?? '') ?? 1;
|
||||
if (type < 1 || type > 4) type = 1;
|
||||
|
||||
return ScannedContact(publicKey: publicKey, name: name, type: type);
|
||||
}
|
||||
|
||||
/// Local hex decoder: keeps this util pure-Dart. `hex2Uint8List` in
|
||||
/// `connector/meshcore_protocol.dart` throws on invalid input and that file
|
||||
/// pulls in `package:flutter/widgets.dart` and `package:crypto`, which this
|
||||
/// util must not depend on.
|
||||
Uint8List? _tryHexDecode(String hex) {
|
||||
if (hex.isEmpty || hex.length.isOdd) return null;
|
||||
final bytes = Uint8List(hex.length ~/ 2);
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
final high = _hexDigit(hex.codeUnitAt(i * 2));
|
||||
final low = _hexDigit(hex.codeUnitAt(i * 2 + 1));
|
||||
if (high == null || low == null) return null;
|
||||
bytes[i] = (high << 4) | low;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
int? _hexDigit(int codeUnit) {
|
||||
if (codeUnit >= 0x30 && codeUnit <= 0x39) return codeUnit - 0x30;
|
||||
if (codeUnit >= 0x61 && codeUnit <= 0x66) return codeUnit - 0x61 + 10;
|
||||
if (codeUnit >= 0x41 && codeUnit <= 0x46) return codeUnit - 0x41 + 10;
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import '../connector/meshcore_protocol.dart';
|
||||
import '../models/contact.dart';
|
||||
import 'contact_qr.dart';
|
||||
|
||||
/// Builds the device frame that applies [scanned].
|
||||
///
|
||||
/// Advert blobs are replayed with `CMD_IMPORT_CONTACT`; contact cards are
|
||||
/// written with `CMD_ADD_UPDATE_CONTACT`, preserving the flags, name and route
|
||||
/// of [existing] when the QR omits them. A card carries no route, so a contact
|
||||
/// the device does not know yet is written with the `0xFF` unknown-path
|
||||
/// sentinel (`Contact.fromFrame` maps it to `hopCount == -1`) and its messages
|
||||
/// flood until an advert supplies a path.
|
||||
///
|
||||
/// [pathHashByteWidth] is the connector's current path-hash mode, needed to
|
||||
/// re-encode a preserved path the same way the rest of the app does.
|
||||
Uint8List buildContactQrFrame(
|
||||
ScannedContact scanned, {
|
||||
Contact? existing,
|
||||
int pathHashByteWidth = 1,
|
||||
}) {
|
||||
if (scanned.isAdvertPacket) {
|
||||
return buildImportContactFrame(scanned.advertPacket!);
|
||||
}
|
||||
final route = _routeOf(existing, pathHashByteWidth);
|
||||
return buildUpdateContactPathFrame(
|
||||
scanned.publicKey!,
|
||||
route.path,
|
||||
route.pathLen,
|
||||
type: scanned.type,
|
||||
flags: existing?.flags ?? 0,
|
||||
name: _fitName(
|
||||
scanned.name.isEmpty ? (existing?.name ?? '') : scanned.name,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Rescanning a contact must not throw away a route the device already has:
|
||||
/// writing an empty path with the unknown sentinel would force flooding until
|
||||
/// another advert restores it. Reuse [existing]'s path when it is valid for the
|
||||
/// current mode, encoded as `MeshCoreConnector._encodePathLenForCurrentMode`
|
||||
/// does (hop count in bits 0-5, mode in bits 6-7).
|
||||
({Uint8List path, int pathLen}) _routeOf(Contact? existing, int width) {
|
||||
final unknown = (path: Uint8List(0), pathLen: 0xFF);
|
||||
if (existing == null) return unknown;
|
||||
final hops = existing.pathLength;
|
||||
if (hops < 0 || hops > 0x3F) return unknown;
|
||||
final hashWidth = width.clamp(1, 4).toInt();
|
||||
final path = existing.path;
|
||||
if (path.length > maxPathSize || path.length != hops * hashWidth) {
|
||||
return unknown;
|
||||
}
|
||||
return (path: path, pathLen: hops | ((hashWidth - 1) << 6));
|
||||
}
|
||||
|
||||
/// `writeCString` clips the encoded name to `maxNameSize - 1` bytes without
|
||||
/// looking at character boundaries, which would leave a half-written rune in
|
||||
/// the contact record. Drop whole characters instead.
|
||||
String _fitName(String name) {
|
||||
const limit = maxNameSize - 1;
|
||||
if (utf8.encode(name).length <= limit) return name;
|
||||
final buffer = StringBuffer();
|
||||
var used = 0;
|
||||
for (final rune in name.runes) {
|
||||
final char = String.fromCharCode(rune);
|
||||
final charBytes = utf8.encode(char).length;
|
||||
if (used + charBytes > limit) break;
|
||||
buffer.write(char);
|
||||
used += charBytes;
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
Reference in new issue
Block a user