Merge pull request #1331 from OffbandMesh/feat/1288-land-on-dev
feat(#1311): land Epic D on dev (#1330 merged into its stacked base)pull/1338/head
commit
67d7541c88
@ -0,0 +1,62 @@
|
||||
/// The two forms of a channel or contact share link (#1280).
|
||||
///
|
||||
/// - `meshcore://channel/add?<fields>` and `meshcore://contact/add?<fields>`:
|
||||
/// what the stock app reads, so QR codes keep using it.
|
||||
/// - `https://offband.app/add/channel#<fields>` and `/add/contact#<fields>`:
|
||||
/// what Share and Copy Link emit. The OS opens it in Offband, which the
|
||||
/// stock app's claim on `meshcore://` gets in the way of.
|
||||
///
|
||||
/// The fields are the same in both. In the https form they sit after `#`,
|
||||
/// which browsers never send to the server, so a channel secret stays out
|
||||
/// of the server's logs.
|
||||
library;
|
||||
|
||||
/// The hosts whose `/add/...` links are Offband share links. The app is
|
||||
/// verified for both (#1309), so these open it instead of the browser.
|
||||
const shareLinkHosts = {'offband.app', 'dev.offband.app'};
|
||||
|
||||
/// The host Share and Copy Link emit (#1311).
|
||||
const shareLinkHost = 'offband.app';
|
||||
|
||||
final _meshcoreForm = RegExp(r'^meshcore://(channel|contact)/add\?(.+)$');
|
||||
|
||||
/// The `meshcore://` twin of an `https://offband.app/add/channel#...` or
|
||||
/// `/add/contact#...` link, or null if [raw] is not one (#1309).
|
||||
String? meshcoreFormOf(String raw) {
|
||||
final uri = Uri.tryParse(raw.trim());
|
||||
if (uri == null || !isShareAddress(uri)) return null;
|
||||
final path = _pathOf(uri);
|
||||
if (path.length != 2) return null;
|
||||
if (path[1] != 'channel' && path[1] != 'contact') return null;
|
||||
if (uri.query.isNotEmpty || uri.fragment.isEmpty) return null;
|
||||
return 'meshcore://${path[1]}/add?${uri.fragment}';
|
||||
}
|
||||
|
||||
/// The path's segments, lowercased: a link typed by hand as `/Add/Channel`
|
||||
/// is the same link.
|
||||
List<String> _pathOf(Uri uri) => [
|
||||
for (final s in uri.pathSegments)
|
||||
if (s.isNotEmpty) s.toLowerCase(),
|
||||
];
|
||||
|
||||
/// The https twin of a `meshcore://channel/add?...` or `contact/add?...`
|
||||
/// link, on [shareLinkHost] (#1311). The fields are carried as they are,
|
||||
/// already encoded.
|
||||
String httpsFormOf(String meshcoreLink) {
|
||||
final match = _meshcoreForm.firstMatch(meshcoreLink);
|
||||
if (match == null) {
|
||||
throw ArgumentError.value(meshcoreLink, 'meshcoreLink', 'not a share link');
|
||||
}
|
||||
return 'https://$shareLinkHost/add/${match[1]}#${match[2]}';
|
||||
}
|
||||
|
||||
/// [raw] in the `meshcore://` form, whichever form it arrived in, so one
|
||||
/// parser reads both.
|
||||
String toMeshcoreForm(String raw) => meshcoreFormOf(raw) ?? raw.trim();
|
||||
|
||||
/// True for any `https://offband.app/add/...` address, well formed or not.
|
||||
bool isShareAddress(Uri uri) =>
|
||||
uri.scheme.toLowerCase() == 'https' &&
|
||||
!uri.hasPort &&
|
||||
shareLinkHosts.contains(uri.host.toLowerCase()) &&
|
||||
_pathOf(uri).firstOrNull == 'add';
|
||||
@ -0,0 +1,146 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/helpers/share_link_forms.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/models/channel.dart';
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
import 'package:meshcore_open/widgets/qr_code_display.dart';
|
||||
|
||||
// #1311: Share and Copy Link emit https://offband.app/add/..., which opens
|
||||
// Offband; QR codes stay meshcore://, which the stock app scans. Every parser
|
||||
// reads both.
|
||||
|
||||
const _key = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
|
||||
const _secret = '5d25cf40b1f5b4a7eb0cf9703634a948';
|
||||
|
||||
void main() {
|
||||
group('the two forms', () {
|
||||
test('a channel link round-trips through both forms', () {
|
||||
for (final name in ['Ops', '#echo', 'a&b', '50% off', 'one+two', 'a b']) {
|
||||
final channel = Channel(
|
||||
index: 0,
|
||||
name: name,
|
||||
psk: Channel.parsePskHex(_secret),
|
||||
);
|
||||
final link = channel.toShareLink();
|
||||
expect(link, startsWith('https://offband.app/add/channel#'));
|
||||
expect(meshcoreFormOf(link), channel.toShareUri());
|
||||
final back = Channel.fromShareUri(link)!;
|
||||
expect(back.name, name, reason: link);
|
||||
expect(back.pskHex, _secret, reason: link);
|
||||
}
|
||||
});
|
||||
|
||||
test('a contact link round-trips through both forms', () {
|
||||
for (final name in ['Bob', 'Strycher-RK4', 'a&b', 'a b']) {
|
||||
final link = Contact.buildShareLink(
|
||||
publicKeyHex: _key,
|
||||
name: name,
|
||||
type: advTypeRepeater,
|
||||
);
|
||||
expect(link, startsWith('https://offband.app/add/contact#'));
|
||||
expect(
|
||||
meshcoreFormOf(link),
|
||||
Contact.buildShareUri(
|
||||
publicKeyHex: _key,
|
||||
name: name,
|
||||
type: advTypeRepeater,
|
||||
),
|
||||
);
|
||||
final back = Contact.fromShareUri(link)!;
|
||||
expect(back.publicKeyHex, _key);
|
||||
expect(back.name, name);
|
||||
expect(back.type, advTypeRepeater);
|
||||
}
|
||||
});
|
||||
|
||||
test('the parsers still read the meshcore:// form', () {
|
||||
final channel = Channel(
|
||||
index: 0,
|
||||
name: 'Ops',
|
||||
psk: Channel.parsePskHex(_secret),
|
||||
);
|
||||
expect(Channel.fromShareUri(channel.toShareUri())?.name, 'Ops');
|
||||
expect(Channel.isValidShareUri(channel.toShareUri()), isTrue);
|
||||
expect(Channel.isValidShareUri(channel.toShareLink()), isTrue);
|
||||
final uri = Contact.buildShareUri(publicKeyHex: _key, name: 'Bob');
|
||||
expect(Contact.isValidShareUri(uri), isTrue);
|
||||
expect(
|
||||
Contact.isValidShareUri(
|
||||
Contact.buildShareLink(publicKeyHex: _key, name: 'Bob'),
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('httpsFormOf refuses what is not a share link', () {
|
||||
for (final bad in [
|
||||
'meshcore://${'ab' * 98}',
|
||||
'meshcore://channel/add',
|
||||
'https://offband.app/add/channel#name=x',
|
||||
]) {
|
||||
expect(() => httpsFormOf(bad), throwsArgumentError, reason: bad);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('the channel QR stays meshcore://, Copy link copies https', (
|
||||
tester,
|
||||
) async {
|
||||
final channel = Channel(
|
||||
index: 0,
|
||||
name: '#echo',
|
||||
psk: Channel.parsePskHex(_secret),
|
||||
);
|
||||
final copied = <String>[];
|
||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
(call) async {
|
||||
if (call.method == 'Clipboard.setData') {
|
||||
copied.add((call.arguments as Map)['text'] as String);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
);
|
||||
addTearDown(
|
||||
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
null,
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Scaffold(
|
||||
body: QrCodeShareDialog(
|
||||
data: channel.toShareUri(),
|
||||
link: channel.toShareLink(),
|
||||
linkCopiedMessage: 'Channel link copied',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(
|
||||
tester.widget<QrCodeDisplay>(find.byType(QrCodeDisplay)).data,
|
||||
channel.toShareUri(),
|
||||
);
|
||||
await tester.tap(find.text('Copy link'));
|
||||
await tester.pump();
|
||||
expect(copied, [channel.toShareLink()]);
|
||||
expect(find.text('Channel link copied'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a QR with no link offers no Copy link', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: const Scaffold(body: QrCodeShareDialog(data: '{"x":1}')),
|
||||
),
|
||||
);
|
||||
expect(find.text('Copy link'), findsNothing);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue