c98070593695

feat: Pro license transfer during chirp pairing — PAIR_IDENTITY gains optional 'license'; a Pro device advertises its signed blob and the peer activates it (LicenseRepository signature-verify) after SAS confirm, either direction; browser-parity wire field; bump 2.3.1→2.3.2

time
2026-06-19 17:32 UTC · 5 months ago
machine
koh2
agent
claude-opus-4-8
session
pairing pro license transfer
parent
7e04b97b04e0
5 files app/build.gradle+2 −2app/src/main/java/com/peachpasswords/sync/ChirpIdentity.kt+6 −0app/src/main/java/com/peachpasswords/sync/ChirpPairing.kt+46 −3app/src/main/java/com/peachpasswords/ui/pairing/ChirpPairViewModel.kt+17 −2app/src/test/java/com/peachpasswords/sync/ChirpPairingTest.kt+30 −0
app/build.gradle+2 −2
@@ -16,8 +16,8 @@1616         applicationId "com.peachpasswords"1717         minSdk 261818         targetSdk 3519-        versionCode 23010020-        versionName "2.3.1"19+        versionCode 23020020+        versionName "2.3.2"2121 2222         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"2323         vectorDrawables {

view file

app/src/main/java/com/peachpasswords/sync/ChirpIdentity.kt+6 −0
@@ -14,6 +14,12 @@1414     val longTermPublicKeyHex: String,1515     val signingPublicKeyHex: String,1616     val wsPort: Int? = null,17+    /**18+     * This device's signed Pro license blob (`PPK-…`), set only when Pro. Sent19+     * in the outbound PAIR_IDENTITY so the peer can activate it after SAS — one20+     * Pro device grants Pro to the other when pairing. Either direction.21+     */22+    val license: String? = null,1723 )1824 1925 fun getOrCreateDeviceId(context: Context): String {

view file

app/src/main/java/com/peachpasswords/sync/ChirpPairing.kt+46 −3
@@ -31,6 +31,11 @@3131 data class PairingHandshakeResult(3232     val device: PairedDevice,3333     val sas: String,34+    /**35+     * The peer's signed Pro license blob, if it advertised one. Activated36+     * (signature-verified) ONLY after the SAS is confirmed. Never persisted.37+     */38+    val peerLicense: String? = null,3439 )3540 3641 enum class PairingRole {@@ -49,6 +54,13 @@4954     suspend fun openAudio(): AudioIO5055     val chirpTimeoutMs: Long5156     fun onProgress(progress: PairingProgress)57+58+    /**59+     * Activate a Pro license the (now SAS-confirmed) peer advertised. The60+     * implementation verifies the signature and ignores it if already Pro.61+     * Default no-op so existing callers/tests need no change.62+     */63+    suspend fun activatePeerLicense(license: String) {}5264 }5365 5466 class ChirpPairingFailure(@@ -331,6 +343,7 @@331343     transport.close()332344 333345     val paired = gateOnSAS(deps, result)346+    activatePeerLicenseIfAny(deps, result)334347     deps.completePairing(paired)335348     deps.cacheLastKnownIp(paired.longTermPublicKeyHex.orEmpty(), peerEndpoint.ip)336349     deps.onProgress(PairingProgress(phase = PairingPhase.PAIRED, peerDeviceName = paired.deviceName))@@ -357,6 +370,23 @@357370     return result.device358371 }359372 373+/**374+ * If the (now SAS-confirmed) peer advertised a Pro license, hand it to the375+ * deps to verify + activate. Best-effort: a bad/duplicate license never fails376+ * the pairing — the device link still succeeds.377+ */378+private suspend fun activatePeerLicenseIfAny(379+    deps: ChirpPairingDeps,380+    result: PairingHandshakeResult,381+) {382+    val license = result.peerLicense ?: return383+    try {384+        deps.activatePeerLicense(license)385+    } catch (_: Throwable) {386+        // Invalid signature, already-Pro, or storage error — pairing stands.387+    }388+}389+360390 private suspend fun runInitiatorHandshake(361391     deps: ChirpPairingDeps,362392     remoteChirpPublicKey: ByteArray,@@ -383,6 +413,7 @@383413     server.transport.close()384414 385415     val paired = gateOnSAS(deps, result)416+    activatePeerLicenseIfAny(deps, result)386417     deps.completePairing(paired)387418     deps.cacheLastKnownIp(paired.longTermPublicKeyHex.orEmpty(), server.peerIp)388419     deps.onProgress(PairingProgress(phase = PairingPhase.PAIRED, peerDeviceName = paired.deviceName))@@ -446,6 +477,7 @@446477                     longTermPublicKeyHex = ourIdentity.longTermPublicKeyHex,447478                     signingPublicKeyHex = ourIdentity.signingPublicKeyHex,448479                     wsPort = ourIdentity.wsPort,480+                    license = ourIdentity.license,449481                 ),450482             ),451483         ),@@ -457,6 +489,7 @@457489     return PairingHandshakeResult(458490         device = frameToPairedDevice(theirFrame),459491         sas = SASDerivation.derive(session.handshakeHash),492+        peerLicense = theirFrame.license,460493     )461494 }462495 @@ -488,6 +521,7 @@488521                     longTermPublicKeyHex = ourIdentity.longTermPublicKeyHex,489522                     signingPublicKeyHex = ourIdentity.signingPublicKeyHex,490523                     wsPort = ourIdentity.wsPort,524+                    license = ourIdentity.license,491525                 ),492526             ),493527         ),@@ -496,10 +530,11 @@496530     return PairingHandshakeResult(497531         device = frameToPairedDevice(theirFrame),498532         sas = SASDerivation.derive(session.handshakeHash),533+        peerLicense = theirFrame.license,499534     )500535 }501536 502-private data class PairingFrame(537+internal data class PairingFrame(503538     val type: String,504539     val deviceId: String,505540     val deviceName: String,@@ -507,9 +542,12 @@507542     val longTermPublicKeyHex: String,508543     val signingPublicKeyHex: String? = null,509544     val wsPort: Int? = null,545+    // Optional signed Pro license blob — additive; peers that don't understand546+    // it ignore it. Activated by the receiver only after SAS confirmation.547+    val license: String? = null,510548 )511549 512-private fun encodePairingFrame(frame: PairingFrame): ByteArray {550+internal fun encodePairingFrame(frame: PairingFrame): ByteArray {513551     val builder = StringBuilder()514552     builder.append('{')515553     builder.append("\"type\":\"").append(frame.type).append('\"')@@ -523,11 +561,14 @@523561     if (frame.wsPort != null) {524562         builder.append(",\"ws_port\":").append(frame.wsPort)525563     }564+    if (!frame.license.isNullOrBlank()) {565+        builder.append(",\"license\":\"").append(escapeJson(frame.license)).append('\"')566+    }526567     builder.append('}')527568     return builder.toString().encodeToByteArray()528569 }529570 530-private fun decodePairingFrame(bytes: ByteArray): PairingFrame {571+internal fun decodePairingFrame(bytes: ByteArray): PairingFrame {531572     val text = bytes.decodeToString()532573     val parsed = simpleJsonObject(text)533574     require(parsed["type"] == "PAIR_IDENTITY") { "expected PAIR_IDENTITY frame" }@@ -551,6 +592,7 @@551592         }552593     }553594     val wsPort = parsed["ws_port"]?.toIntOrNull()595+    val license = parsed["license"]?.takeIf { it.isNotBlank() }554596     return PairingFrame(555597         type = "PAIR_IDENTITY",556598         deviceId = deviceId,@@ -559,6 +601,7 @@559601         longTermPublicKeyHex = longTermPublicKeyHex,560602         signingPublicKeyHex = signingPublicKeyHex,561603         wsPort = wsPort,604+        license = license,562605     )563606 }564607 

view file

app/src/main/java/com/peachpasswords/ui/pairing/ChirpPairViewModel.kt+17 −2
@@ -4,6 +4,7 @@44 import androidx.lifecycle.ViewModel55 import androidx.lifecycle.viewModelScope66 import com.peachpasswords.BuildConfig7+import com.peachpasswords.license.LicenseRepository78 import com.peachpasswords.sync.AudioIO89 import com.peachpasswords.sync.ChirpPairingDeps910 import com.peachpasswords.sync.LocalChirpIdentity@@ -51,6 +52,7 @@5152     @ApplicationContext private val context: Context,5253     private val vault: VaultRepository,5354     private val deviceStore: PairedDeviceStore,55+    private val license: LicenseRepository,5456 ) : ViewModel() {5557 5658     private val _progress = MutableStateFlow(PairingProgress(PairingPhase.IDLE))@@ -119,12 +121,25 @@119121     private fun deps(masterKey: ByteArray): ChirpPairingDeps = object : ChirpPairingDeps {120122         override val chirpTimeoutMs: Long = 15_000121123 122-        override suspend fun fetchLocalIdentity(): LocalChirpIdentity =123-            buildLocalIdentity(context, masterKey)124+        override suspend fun fetchLocalIdentity(): LocalChirpIdentity {125+            val base = buildLocalIdentity(context, masterKey)126+            // Advertise our signed license iff this device is Pro, so the peer127+            // can activate it after SAS — pairing carries Pro across.128+            return if (license.isProActive()) base.copy(license = license.getLicenseKey()) else base129+        }124130 125131         override suspend fun completePairing(paired: PairedDevice) {126132             // Persist the newly-trusted peer so Sync can dial it later (C7 wiring).127133             deviceStore.upsert(paired)134+        }135+136+        override suspend fun activatePeerLicense(license: String) {137+            // Receive Pro from an already-Pro peer. Verify the signature; keep138+            // our own license if we already have one.139+            if (this@ChirpPairViewModel.license.isProActive()) return140+            if (this@ChirpPairViewModel.license.validateLicense(license)) {141+                this@ChirpPairViewModel.license.storeLicense(license)142+            }128143         }129144 130145         override suspend fun confirmSAS(sas: String): Boolean {

view file

app/src/test/java/com/peachpasswords/sync/ChirpPairingTest.kt+30 −0
@@ -174,6 +174,36 @@174174     }175175 176176     @Test177+    fun `pair frame round-trips an optional Pro license`() {178+        val frame = PairingFrame(179+            type = "PAIR_IDENTITY",180+            deviceId = "a",181+            deviceName = "Pixel",182+            deviceType = DeviceType.ANDROID,183+            longTermPublicKeyHex = "0".repeat(64),184+            signingPublicKeyHex = "1".repeat(64),185+            license = "PPK-AAA-BBB",186+        )187+        val json = encodePairingFrame(frame).decodeToString()188+        assertTrue(json.contains("\"license\":\"PPK-AAA-BBB\""))189+        assertEquals("PPK-AAA-BBB", decodePairingFrame(json.encodeToByteArray()).license)190+191+        // Omitted entirely when null (forward-compatible with peers that ignore it).192+        val noLicense = encodePairingFrame(frame.copy(license = null)).decodeToString()193+        assertFalse(noLicense.contains("license"))194+    }195+196+    @Test197+    fun `decodes a browser PAIR_IDENTITY frame carrying a license`() {198+        val browserJson = "{\"type\":\"PAIR_IDENTITY\"," +199+            "\"device_id\":\"ext-1\",\"device_name\":\"Firefox\"," +200+            "\"device_type\":\"extension\"," +201+            "\"long_term_public_key_hex\":\"${"0".repeat(64)}\"," +202+            "\"license\":\"PPK-XYZ-123\"}"203+        assertEquals("PPK-XYZ-123", decodePairingFrame(browserJson.encodeToByteArray()).license)204+    }205+206+    @Test177207     fun `runChirpPairing emits listening before timeout`() = runBlocking {178208         val trail = mutableListOf<PairingProgress>()179209         try {

view file