diet-guard/app/lib/services/sync_device_id.dart
Krzysztof kuhy Rudnicki 617e1ec879
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
E2E-encrypt diet_guard sync and export keys
Every sync payload (food log, banks, budget, catering credential,
Body, export keyring) now lives on diet-guard-sync/devices-e2e, sealed
with the Argon2id key from the shared crdt-sync passphrase; record
ids, tombstones and clocks stay clear. Old plaintext nodes are copied
once and never written or deleted. Locked = no pull, no push.

- PC: _sync_e2e.open_e2e over crdt-sync 0.11.0, key and copy memo in
  ~/.local/share/diet_guard/e2e; undecryptable peers are skipped.
- Phone/desktop: sync_e2e.dart, crdt_sync_flutter 0.8.1, lock icon
  in Settings; pullExportKeys goes through the same open.
- wake-alarm weigh-ins read from wake-alarm-sync/devices-e2e (own
  header), on both sides.

Todo: fda71ed1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:31:44 +01:00

65 lines
2.7 KiB
Dart

/// This install's persisted sync device id.
library;
import 'package:diet_guard_app/services/github_client_factory.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
/// Where every device's synced files live on the remote: the end-to-end
/// encrypted twin of the old plaintext `diet-guard-sync/devices` (see
/// `sync_e2e.dart`; `E2eLayout.of` derives the same names).
const syncDevicesDir = 'diet-guard-sync/devices-e2e';
/// Where peers publish their (keyed) revisions, apart from the old
/// plaintext `diet-guard-sync/revs` the E2E copy fingerprints.
const syncRevsDir = 'diet-guard-sync/revs-e2e';
/// SharedPreferences key holding this install's device id.
///
/// The same key the sibling apps (`todo`, `home_inventory`, `wake_alarm`)
/// use for the same purpose.
const kSyncDeviceIdKey = 'crdt.nodeId';
String? _cached;
/// This install's sync device id, for HLC stamping and the pushed path.
///
/// Synchronous because [Hlc] stamping is synchronous and threading a future
/// through every merge adapter would be a large change for no benefit. Call
/// [initSyncDeviceId] once during startup, before any sync or merge runs;
/// until then this falls back to the compile-time role constant
/// ([syncDeviceId]) so a caller that forgets still writes a *valid* id rather
/// than crashing -- it just writes the pre-migration one.
String get currentSyncDeviceId => _cached ?? syncDeviceId;
/// The id this install pushed under before migrating to a persisted uuid.
///
/// Passed to `syncLog` as its legacy id so the log already sitting at
/// `devices/<legacy>/` is treated as this device's own rather than a peer's;
/// without it every tick re-downloads and re-merges this device's own
/// pre-migration history. Set to null once that path has been reclaimed.
const String legacySyncDeviceId = syncDeviceId;
/// Loads (or mints and persists) this install's device id.
///
/// A per-install uuid rather than the fixed `phone`/`desktop` role constant:
/// two installs sharing an id overwrite each other's pushed file on every
/// tick, and a reinstall would inherit the previous install's CRDT identity.
///
/// Idempotent -- safe to call more than once.
Future<String> initSyncDeviceId({SharedPreferences? prefs}) async {
final store = prefs ?? await SharedPreferences.getInstance();
final existing = store.getString(kSyncDeviceIdKey);
if (existing != null && existing.isNotEmpty) {
_cached = existing;
return existing;
}
final minted = const Uuid().v4();
await store.setString(kSyncDeviceIdKey, minted);
_cached = minted;
return minted;
}
/// Resets the cached id. Test-only.
void resetSyncDeviceIdForTest() => _cached = null;