package com.peachpasswords.vault import android.content.Context import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import javax.inject.Inject import javax.inject.Singleton /** * One encrypted slot at rest. [blob] is base64 of VaultCrypto's * `nonce || ciphertext || tag` output; [salt] (base64) drives Argon2id. * Mirrors the browser's StoredVault, minus the recovery-escrow fields which * the Android port wires in a later phase. */ @Serializable data class StoredVault( val version: Int = 1, val salt: String, val blob: String, ) /** A soft-deleted entry, exposed to the sync layer for last-write-wins merges. */ @Serializable data class Tombstone(val id: String, val deletedAt: Long) /** * The decrypted slot-A envelope: the entry list, the duress flag, and the * stable sync root key. * * [rootKeyB64] is the vault's stable 32-byte sync master key (base64, no * padding). Unlike the Argon2id password key it never rotates on a password * change, so paired devices keep deriving the same vault_id, Noise static key, * and per-entry envelope keys across independent password rotations (browser * SEV-2-B). Empty on legacy payloads written before sync landed — the * repository lazily mints and persists one on first unlock. */ @Serializable data class VaultPayload( val entries: List = emptyList(), val duressConfigured: Boolean = false, val rootKeyB64: String = "", ) /** Pure JSON codec for the slot-A plaintext envelope — usable without Android. */ object VaultCodec { private val json = Json { ignoreUnknownKeys = true; encodeDefaults = true } fun encode(payload: VaultPayload): ByteArray = json.encodeToString(payload).toByteArray(Charsets.UTF_8) fun decode(bytes: ByteArray): VaultPayload = json.decodeFromString(String(bytes, Charsets.UTF_8)) fun encodeSettings(s: VaultSettings): String = json.encodeToString(s) fun decodeSettings(s: String): VaultSettings? = runCatching { json.decodeFromString(s) }.getOrNull() fun encodeSlot(s: StoredVault): String = json.encodeToString(s) fun decodeSlot(s: String): StoredVault? = runCatching { json.decodeFromString(s) }.getOrNull() } /** * Persistence for the two vault slots, the monotonic version counter, and the * settings blob. Abstracted behind an interface so the repository can be * exercised against an in-memory fake in plain JUnit. */ interface VaultStorage { fun loadSlotA(): StoredVault? fun hasVault(): Boolean fun writeSlotA(slot: StoredVault): Boolean fun writeSlotB(blobBase64: String): Boolean fun version(): Long fun stampLocalMutation(): Long fun loadSettings(): VaultSettings fun writeSettings(settings: VaultSettings): Boolean fun clearVault() } /** * SharedPreferences-backed [VaultStorage]. The slot blobs are already AES-GCM * sealed under the Argon2id key, so plaintext prefs are fine — only the salt is * in the clear, exactly as in storage.local on the extension. The Pro license * lives in a SEPARATE prefs file (LicenseRepository), so wiping this store can * never touch the license — destroyVault()'s core invariant. */ @Singleton class VaultStore @Inject constructor( @ApplicationContext context: Context, ) : VaultStorage { private val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) override fun loadSlotA(): StoredVault? = prefs.getString(KEY_SLOT_A, null)?.let(VaultCodec::decodeSlot) override fun hasVault(): Boolean = prefs.contains(KEY_SLOT_A) override fun writeSlotA(slot: StoredVault): Boolean = prefs.edit().putString(KEY_SLOT_A, VaultCodec.encodeSlot(slot)).commit() override fun writeSlotB(blobBase64: String): Boolean = prefs.edit().putString(KEY_SLOT_B, blobBase64).commit() override fun version(): Long = prefs.getString(KEY_VERSION, null)?.toLongOrNull() ?: 0L override fun stampLocalMutation(): Long { val next = maxOf(System.currentTimeMillis(), version() + 1L) prefs.edit().putString(KEY_VERSION, next.toString()).commit() return next } override fun loadSettings(): VaultSettings = prefs.getString(KEY_SETTINGS, null)?.let(VaultCodec::decodeSettings) ?: VaultSettings() override fun writeSettings(settings: VaultSettings): Boolean = prefs.edit().putString(KEY_SETTINGS, VaultCodec.encodeSettings(settings)).commit() override fun clearVault() { prefs.edit() .remove(KEY_SLOT_A).remove(KEY_SLOT_B).remove(KEY_VERSION).remove(KEY_SETTINGS) .commit() } companion object { private const val PREFS = "peach_vault_store" private const val KEY_SLOT_A = "peach_vault_a" private const val KEY_SLOT_B = "peach_vault_b" private const val KEY_VERSION = "peach_last_modified_at" private const val KEY_SETTINGS = "peach_settings" } }