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:
Gianluca Boiano committed 2026-09-13 00:23:46 +02:00
1 parent 92e1d15d1c
commit d0583f2039
28 files changed
+1231 -16

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/models/contact.dart';
import 'package:meshcore_open/utils/contact_qr.dart';
import 'package:meshcore_open/utils/contact_qr_frame.dart';
void main() {
final publicKey = Uint8List.fromList(List<int>.filled(32, 0xAB));
Contact contactWith({required String name, required int flags}) {
return Contact(
publicKey: publicKey,
name: name,
type: advTypeChat,
flags: flags,
pathLength: 2,
path: Uint8List.fromList([1, 2]),
lastSeen: DateTime.fromMillisecondsSinceEpoch(0),
);
}
group('buildContactQrFrame advert packet', () {
test('replays the advert with CMD_IMPORT_CONTACT', () {
final advert = Uint8List.fromList(List<int>.filled(98, 7));
final frame = buildContactQrFrame(
ScannedContact(advertPacket: advert),
existing: contactWith(name: 'Old', flags: 0x03),
);
expect(frame[0], cmdImportContact);
expect(frame.sublist(1), advert);
});
});
group('buildContactQrFrame contact card', () {
test('writes an add/update frame with the unknown-path sentinel', () {
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey, name: 'Alice', type: 2),
);
expect(frame[0], cmdAddUpdateContact);
expect(frame.sublist(1, 33), publicKey);
expect(frame[33], 2);
expect(frame[34], 0);
expect(frame[35], 0xFF, reason: 'path is unknown, not zero-hop');
final nameStart = 36 + maxPathSize;
final name = String.fromCharCodes(
frame
.sublist(nameStart, nameStart + maxNameSize)
.takeWhile((b) => b != 0),
);
expect(name, 'Alice');
});
test('preserves the flags of an existing contact', () {
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey, name: 'Alice'),
existing: contactWith(name: 'Alice', flags: 0x05),
);
expect(frame[34], 0x05);
});
test('falls back to the existing name when the card has none', () {
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey),
existing: contactWith(name: 'Existing', flags: 0),
);
final nameStart = 36 + maxPathSize;
final name = String.fromCharCodes(
frame
.sublist(nameStart, nameStart + maxNameSize)
.takeWhile((b) => b != 0),
);
expect(name, 'Existing');
});
test('drops whole characters when the name exceeds 31 bytes', () {
// 16 three-byte characters = 48 encoded bytes; only 10 fit in 31.
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey, name: '☂' * 16),
);
final nameStart = 36 + maxPathSize;
final bytes = frame
.sublist(nameStart, nameStart + maxNameSize)
.takeWhile((b) => b != 0)
.toList();
expect(bytes.length, 30);
expect(utf8.decode(bytes), '☂' * 10);
});
});
group('buildContactQrFrame route preservation', () {
Contact contactWithPath(int pathLength, List<int> path) {
return Contact(
publicKey: publicKey,
name: 'Alice',
type: advTypeChat,
pathLength: pathLength,
path: Uint8List.fromList(path),
lastSeen: DateTime.fromMillisecondsSinceEpoch(0),
);
}
test('keeps the route of a contact the device already knows', () {
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey, name: 'Alice'),
existing: contactWithPath(2, [0x11, 0x22]),
);
expect(frame[35], 0x02, reason: 'two hops, 1-byte hash mode');
expect(frame.sublist(36, 38), [0x11, 0x22]);
});
test('encodes the hash mode in bits 6-7 for wider path hashes', () {
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey, name: 'Alice'),
existing: contactWithPath(2, [0x11, 0x22, 0x33, 0x44]),
pathHashByteWidth: 2,
);
expect(frame[35], 0x42);
expect(frame.sublist(36, 40), [0x11, 0x22, 0x33, 0x44]);
});
test('uses the unknown sentinel for a flood-only contact', () {
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey, name: 'Alice'),
existing: contactWithPath(-1, const []),
);
expect(frame[35], 0xFF);
expect(frame.sublist(36, 36 + maxPathSize).every((b) => b == 0), isTrue);
});
test('uses the unknown sentinel when the path does not fit the mode', () {
final frame = buildContactQrFrame(
ScannedContact(publicKey: publicKey, name: 'Alice'),
existing: contactWithPath(2, [0x11, 0x22]),
pathHashByteWidth: 2,
);
expect(frame[35], 0xFF);
});
});
}
+156
View File
@@ -0,0 +1,156 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/utils/contact_qr.dart';
void main() {
group('parseContactQr official contact card', () {
test('parses name with + and percent-encoding, key, explicit type', () {
final key = List.generate(
32,
(i) => i.toRadixString(16).padLeft(2, '0'),
).join();
final result = parseContactQr(
'meshcore://contact/add?name=Jane+D%C3%B6e&public_key=$key&type=2',
);
expect(result, isNotNull);
expect(result!.isAdvertPacket, isFalse);
expect(result.name, 'Jane Döe');
expect(result.publicKey!.length, 32);
expect(result.publicKey![0], 0x00);
expect(result.publicKey![31], 0x1f);
expect(result.type, 2);
});
test('missing type defaults to 1', () {
final key = 'ab' * 32;
final result = parseContactQr(
'meshcore://contact/add?name=Bob&public_key=$key',
);
expect(result, isNotNull);
expect(result!.type, 1);
});
test('garbage type falls back to 1', () {
final key = 'ab' * 32;
final result = parseContactQr(
'meshcore://contact/add?name=Bob&public_key=$key&type=nope',
);
expect(result, isNotNull);
expect(result!.type, 1);
});
test('uppercase hex public key is accepted', () {
final key = 'AB' * 32;
final result = parseContactQr(
'meshcore://contact/add?name=Bob&public_key=$key',
);
expect(result, isNotNull);
expect(result!.publicKey!.length, 32);
expect(result.publicKey![0], 0xAB);
});
test('63-char public key is rejected', () {
final key = '${'ab' * 31}a';
final result = parseContactQr(
'meshcore://contact/add?name=Bob&public_key=$key',
);
expect(result, isNull);
});
test('non-hex public key is rejected', () {
final key = ('zz' * 32);
final result = parseContactQr(
'meshcore://contact/add?name=Bob&public_key=$key',
);
expect(result, isNull);
});
test('signed public key digits are rejected', () {
final result = parseContactQr(
'meshcore://contact/add?name=Bob&public_key=${'+1' * 32}',
);
expect(result, isNull);
});
test('missing name defaults to empty string', () {
final key = 'ab' * 32;
final result = parseContactQr('meshcore://contact/add?public_key=$key');
expect(result, isNotNull);
expect(result!.name, '');
});
});
group('parseContactQr legacy advert packet', () {
test('legacy hex form yields advert packet bytes', () {
final hex = '0a' * 98;
final result = parseContactQr('meshcore://$hex');
expect(result, isNotNull);
expect(result!.isAdvertPacket, isTrue);
expect(result.advertPacket!.length, 98);
expect(result.advertPacket![0], 0x0a);
});
test('odd-length hex is rejected', () {
final result = parseContactQr('meshcore://abc');
expect(result, isNull);
});
test('non-hex payload is rejected', () {
final result = parseContactQr('meshcore://zz${'00' * 97}');
expect(result, isNull);
});
test('payload shorter than the 98 byte advert minimum is rejected', () {
final result = parseContactQr('meshcore://${'ab' * 97}');
expect(result, isNull);
});
test('payload longer than the 171 byte frame limit is rejected', () {
final result = parseContactQr('meshcore://${'ab' * 172}');
expect(result, isNull);
});
test('payload at the 171 byte frame limit is accepted', () {
final result = parseContactQr('meshcore://${'ab' * 171}');
expect(result, isNotNull);
expect(result!.advertPacket!.length, 171);
});
});
group('parseContactQr rejects unsupported input', () {
test('channel add link is not a contact', () {
final result = parseContactQr(
'meshcore://channel/add?name=Public&secret=abcd',
);
expect(result, isNull);
});
test('non-meshcore scheme is rejected', () {
final result = parseContactQr('https://example.com/contact/add');
expect(result, isNull);
});
test('empty string is rejected', () {
expect(parseContactQr(''), isNull);
});
test('whitespace-only string is rejected', () {
expect(parseContactQr(' \n\t '), isNull);
});
});
}