VaultStore.kt

4.5 KB · raw

1package com.peachpasswords.vault23import android.content.Context4import dagger.hilt.android.qualifiers.ApplicationContext5import kotlinx.serialization.Serializable6import kotlinx.serialization.decodeFromString7import kotlinx.serialization.encodeToString8import kotlinx.serialization.json.Json9import javax.inject.Inject10import javax.inject.Singleton1112/**13 * One encrypted slot at rest. [blob] is base64 of VaultCrypto's14 * `nonce || ciphertext || tag` output; [salt] (base64) drives Argon2id.15 * Mirrors the browser's StoredVault, minus the recovery-escrow fields which16 * the Android port wires in a later phase.17 */18@Serializable19data class StoredVault(20    val version: Int = 1,21    val salt: String,22    val blob: String,23)2425/** A soft-deleted entry, exposed to the sync layer for last-write-wins merges. */26@Serializable27data class Tombstone(val id: String, val deletedAt: Long)2829/** The decrypted slot-A envelope: the entry list plus the duress flag. */30@Serializable31data class VaultPayload(32    val entries: List<VaultEntry> = emptyList(),33    val duressConfigured: Boolean = false,34)3536/** Pure JSON codec for the slot-A plaintext envelope — usable without Android. */37object VaultCodec {38    private val json = Json { ignoreUnknownKeys = true; encodeDefaults = true }39    fun encode(payload: VaultPayload): ByteArray = json.encodeToString(payload).toByteArray(Charsets.UTF_8)40    fun decode(bytes: ByteArray): VaultPayload = json.decodeFromString(String(bytes, Charsets.UTF_8))41    fun encodeSettings(s: VaultSettings): String = json.encodeToString(s)42    fun decodeSettings(s: String): VaultSettings? = runCatching { json.decodeFromString<VaultSettings>(s) }.getOrNull()43    fun encodeSlot(s: StoredVault): String = json.encodeToString(s)44    fun decodeSlot(s: String): StoredVault? = runCatching { json.decodeFromString<StoredVault>(s) }.getOrNull()45}4647/**48 * Persistence for the two vault slots, the monotonic version counter, and the49 * settings blob. Abstracted behind an interface so the repository can be50 * exercised against an in-memory fake in plain JUnit.51 */52interface VaultStorage {53    fun loadSlotA(): StoredVault?54    fun hasVault(): Boolean55    fun writeSlotA(slot: StoredVault): Boolean56    fun writeSlotB(blobBase64: String): Boolean57    fun version(): Long58    fun stampLocalMutation(): Long59    fun loadSettings(): VaultSettings60    fun writeSettings(settings: VaultSettings): Boolean61    fun clearVault()62}6364/**65 * SharedPreferences-backed [VaultStorage]. The slot blobs are already AES-GCM66 * sealed under the Argon2id key, so plaintext prefs are fine — only the salt is67 * in the clear, exactly as in storage.local on the extension. The Pro license68 * lives in a SEPARATE prefs file (LicenseRepository), so wiping this store can69 * never touch the license — destroyVault()'s core invariant.70 */71@Singleton72class VaultStore @Inject constructor(73    @ApplicationContext context: Context,74) : VaultStorage {75    private val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)7677    override fun loadSlotA(): StoredVault? = prefs.getString(KEY_SLOT_A, null)?.let(VaultCodec::decodeSlot)7879    override fun hasVault(): Boolean = prefs.contains(KEY_SLOT_A)8081    override fun writeSlotA(slot: StoredVault): Boolean =82        prefs.edit().putString(KEY_SLOT_A, VaultCodec.encodeSlot(slot)).commit()8384    override fun writeSlotB(blobBase64: String): Boolean =85        prefs.edit().putString(KEY_SLOT_B, blobBase64).commit()8687    override fun version(): Long = prefs.getString(KEY_VERSION, null)?.toLongOrNull() ?: 0L8889    override fun stampLocalMutation(): Long {90        val next = maxOf(System.currentTimeMillis(), version() + 1L)91        prefs.edit().putString(KEY_VERSION, next.toString()).commit()92        return next93    }9495    override fun loadSettings(): VaultSettings =96        prefs.getString(KEY_SETTINGS, null)?.let(VaultCodec::decodeSettings) ?: VaultSettings()9798    override fun writeSettings(settings: VaultSettings): Boolean =99        prefs.edit().putString(KEY_SETTINGS, VaultCodec.encodeSettings(settings)).commit()100101    override fun clearVault() {102        prefs.edit()103            .remove(KEY_SLOT_A).remove(KEY_SLOT_B).remove(KEY_VERSION).remove(KEY_SETTINGS)104            .commit()105    }106107    companion object {108        private const val PREFS = "peach_vault_store"109        private const val KEY_SLOT_A = "peach_vault_a"110        private const val KEY_SLOT_B = "peach_vault_b"111        private const val KEY_VERSION = "peach_last_modified_at"112        private const val KEY_SETTINGS = "peach_settings"113    }114}