findfamily: only persist a tracker after provisioning succeeds
- Commit
3f5a2a03b555c99234afc76321ca7c9de957d090- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-21 22:47 UTC (3 months ago)
- Parents
d993a49
Diff
findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerBinder.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerBinder.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerBinder.ktindex 2f2cb2f52d..e717ae479a 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerBinder.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerBinder.kt@@ -27,11 +27,18 @@ object TrackerBinder { /** * Bind [device] as a new tracker named [name]. *- * Steps: generate an ML-KEM/ML-DSA keypair (public bundle stored on the `User`- * row, private bundle kept owner-only in [TrackerStore]); generate a 32-byte- * beacon secret; create the `User(kind = TRACKER)` row; register the tracker on- * the server so finders can resolve it; write the provisioning blob to the- * device. Returns true iff the GATT provisioning write succeeds.+ * Steps: generate an ML-KEM/ML-DSA keypair and a 32-byte beacon secret, provision the+ * device over GATT, and only then persist anything — the private bundle and secret in+ * [TrackerStore], the `User(kind = TRACKER)` row, and the server-side registration.+ * Returns true iff the GATT provisioning write succeeds.+ *+ * The provisioning write comes first deliberately. Persisting up front meant every+ * failed bind left an orphan tracker in the family list and orphan secrets in+ * DataStore, which is how a handful of retries turned into a list full of trackers+ * that were never really bound. The cost of this order is a narrow window where a+ * crash between the write and the persist leaves a provisioned device the app doesn't+ * know about; that recovers by long-pressing the button and binding again, which is a+ * far better failure mode. */ suspend fun bind(context: Context, name: String, device: BluetoothDevice): Boolean = withContext(Dispatchers.IO) {@@ -45,6 +52,11 @@ object TrackerBinder { val secret = ByteArray(TrackerProtocol.SECRET_LEN).also { SecureRandom().nextBytes(it) } val trackerId = Random.nextLong(from = 1, until = Long.MAX_VALUE) + if (!TrackerProvisioner(context).provision(device, trackerId, secret)) {+ Log.w(TAG, "provisioning write failed; not persisting tracker $trackerId")+ return@runCatching false+ }+ store.save(trackerId, secret, privateBundle, device.address) repository.upsertUser( User(@@ -63,8 +75,7 @@ object TrackerBinder { // Best-effort: registration self-heals on the next heartbeat if the // socket is momentarily down (see pollTrackerReports). runCatching { Networking.registerTracker(trackerId, secret, publicBundle) }-- TrackerProvisioner(context).provision(device, trackerId, secret)+ true }.onFailure { Log.w(TAG, "bind failed", it) }.getOrDefault(false) }