DeviceIdentity.kt

3.6 KB · raw

1package com.peachpasswords.sync23import android.content.Context4import java.util.UUID56// The persisted keys keep their original names on purpose. Renaming them7// would orphan this device's stored identity, which means a new device id and8// a new sync keypair — every existing pairing would silently stop working on9// upgrade. The name is stale; the cost of fixing it is not worth paying.10private const val DEVICE_ID_KEY = "peach_chirp_device_id"11private const val DEVICE_NAME_KEY = "peach_chirp_device_name"12private const val PREFS_FILE = "peach_chirp_identity"1314data class LocalDeviceIdentity(15    val deviceId: String,16    val deviceName: String,17    val deviceType: DeviceType,18    val longTermPublicKeyHex: String,19    val signingPublicKeyHex: String,20    val wsPort: Int? = null,21    val relayUrl: String = Rendezvous.DEFAULT_RELAY_URL,22    /**23     * This device's signed Pro license blob (`PPK-…`), set only when Pro. Sent24     * in the outbound PAIR_IDENTITY so the peer can activate it after SAS — one25     * Pro device grants Pro to the other when pairing. Either direction.26     */27    val license: String? = null,28)2930fun getOrCreateDeviceId(context: Context): String {31    val prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)32    val current = prefs.getString(DEVICE_ID_KEY, null)33    if (!current.isNullOrEmpty()) return current34    val fresh = UUID.randomUUID().toString()35    prefs.edit().putString(DEVICE_ID_KEY, fresh).apply()36    return fresh37}3839/** Replace the local device id after an authenticated peer detects a collision. */40fun rotateDeviceId(context: Context): String {41    val fresh = UUID.randomUUID().toString()42    context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)43        .edit()44        .putString(DEVICE_ID_KEY, fresh)45        .apply()46    return fresh47}4849fun getDeviceName(context: Context): String {50    val prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)51    return prefs.getString(DEVICE_NAME_KEY, null).orEmpty().ifBlank { defaultDeviceName() }52}5354fun setDeviceName(context: Context, name: String) {55    val trimmed = name.trim()56    require(trimmed.isNotEmpty()) { "device name must not be empty" }57    require(trimmed.length <= 64) { "device name must be 64 chars or fewer" }58    context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)59        .edit()60        .putString(DEVICE_NAME_KEY, trimmed)61        .apply()62}6364fun clearDeviceIdentity(context: Context) {65    context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)66        .edit()67        .clear()68        .apply()69}7071fun buildLocalIdentity(context: Context, masterKey: ByteArray): LocalDeviceIdentity {72    val deviceId = getOrCreateDeviceId(context)73    val deviceName = getDeviceName(context)74    val kp = deriveSyncIdentityKeypair(masterKey, deviceId)75    val signing = deriveSyncSigningKeypair(masterKey, deviceId)76    return LocalDeviceIdentity(77        deviceId = deviceId,78        deviceName = deviceName,79        deviceType = extensionDeviceType(),80        longTermPublicKeyHex = pubkeyToHex(kp.publicKey),81        signingPublicKeyHex = pubkeyToHex(signing.publicKeyRaw),82        // No LAN port is advertised: peers reconnect through the relay83        // (docs/sync-lan-retirement.md), never by dialling this device.84        wsPort = null,85        relayUrl = RelaySettingsStore.readRelayUrl(context),86    )87}8889fun deriveLocalSyncKeypair(masterKey: ByteArray, deviceId: String): SyncIdentityKeypair {90    return deriveSyncIdentityKeypair(masterKey, deviceId)91}9293private fun defaultDeviceName(): String {94    return "Android"95}9697private fun extensionDeviceType(): DeviceType {98    return DeviceType.ANDROID99}