fix: improve transfer recognition and local data safety

This commit is contained in:
2026-07-26 21:36:46 +08:00
parent 7df25edd96
commit 882293343e
21 changed files with 505 additions and 128 deletions
@@ -0,0 +1,40 @@
import 'dart:convert';
import 'package:crypto/crypto.dart';
class BackendIdentity {
BackendIdentity._();
static const internalBuild = bool.fromEnvironment('INTERNAL_BUILD');
static const configuredBaseUrl = String.fromEnvironment('API_BASE_URL');
static const String baseUrl = configuredBaseUrl != ''
? configuredBaseUrl
: 'https://api.invalid';
static final String scope = scopeForBaseUrl(baseUrl);
static String accountNamespace(int userId) => 'account_${scope}_$userId';
static String scopeForBaseUrl(String value) => sha256
.convert(utf8.encode(_normalizedBaseUrl(value)))
.toString()
.substring(0, 16);
static String accountNamespaceFor(String baseUrl, int userId) =>
'account_${scopeForBaseUrl(baseUrl)}_$userId';
static String _normalizedBaseUrl(String value) {
final uri = Uri.tryParse(value.trim());
if (uri == null || !uri.hasScheme || uri.host.isEmpty) {
return value.trim().toLowerCase();
}
final path = uri.path == '/'
? ''
: uri.path.replaceFirst(RegExp(r'/+$'), '');
return uri
.replace(path: path, query: null, fragment: null)
.toString()
.toLowerCase();
}
}