Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Use Rust SQLite implementation for Android and iOS (#2159)
Leendert de Borst31eed02db819
diff --git a/apps/mobile-app/android/app/build.gradle b/apps/mobile-app/android/app/build.gradleindex 6fa0aff6d..2b4ca2f44 100644--- a/apps/mobile-app/android/app/build.gradle+++ b/apps/mobile-app/android/app/build.gradle@@ -210,7 +210,6 @@ dependencies {implementation("androidx.exifinterface:exifinterface:1.4.2")// Add modern SQLite library with VACUUM INTO and backup API support- implementation("com.github.requery:sqlite-android:3.49.0")// Add ZXing library for QR code scanning (F-Droid compatible)implementation("com.journeyapps:zxing-android-embedded:4.3.0")diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyAuthenticationActivity.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyAuthenticationActivity.ktindex c398ed099..5df36438c 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyAuthenticationActivity.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyAuthenticationActivity.kt@@ -161,16 +161,14 @@ class PasskeyAuthenticationActivity : FragmentActivity() {val passkeyId = UUID.fromString(passkeyIdString)- // Get database connection from vault- val db = vaultStore.database.dbConnection- if (db == null) {+ if (!vaultStore.database.isOpen()) {Log.e(TAG, "Database not available - vault may not be unlocked")setResult(RESULT_CANCELED)finish()return@launch}- val passkey = vaultStore.getPasskeyById(passkeyId, db)+ val passkey = vaultStore.getPasskeyById(passkeyId)if (passkey == null) {Log.e(TAG, "Passkey not found: $passkeyId")setResult(RESULT_CANCELED)diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyRegistrationActivity.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyRegistrationActivity.ktindex 08b7a3839..78c1ebc2b 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyRegistrationActivity.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/credentialprovider/PasskeyRegistrationActivity.kt@@ -237,9 +237,7 @@ class PasskeyRegistrationActivity : FragmentActivity() {private fun proceedWithPasskeyRegistration(savedInstanceState: Bundle?) {try {// Check for existing passkeys- val db = vaultStore.database-- if (db != null) {+ if (vaultStore.database.isOpen()) {// Get existing passkeys for the rpId (can be replaced)viewModel.existingPasskeys = vaultStore.getPasskeysWithCredentialInfo(rpId = viewModel.rpId,diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.ktindex 5e1dd5226..116d7fcdc 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt@@ -25,7 +25,6 @@ import kotlinx.coroutines.withContextimport net.aliasvault.app.qrscanner.QRScannerActivityimport net.aliasvault.app.vaultstore.AppErrorimport net.aliasvault.app.vaultstore.VaultStore-import net.aliasvault.app.vaultstore.VaultSyncRunLogimport net.aliasvault.app.vaultstore.interfaces.CryptoOperationCallbackimport net.aliasvault.app.vaultstore.keystoreprovider.AndroidKeystoreProviderimport net.aliasvault.app.vaultstore.storageprovider.AndroidStorageProviderdiff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/SqlScript.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/SqlScript.ktdeleted file mode 100644index c705b9e6c..000000000--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/SqlScript.kt+++ /dev/null@@ -1,30 +0,0 @@-package net.aliasvault.app.vaultstore--/**- * Splits a multi-statement SQL script into the statements Android's SQLite API can run one at a time.- */-object SqlScript {- /**- * Split a script on semicolons, keeping a trigger body (`CREATE TRIGGER ... END`) whole. Strips out all comments which could be mistaken for statements.- */- fun splitStatements(script: String): List<String> {- val withoutComments = script.trimStart('').lineSequence().filterNot { it.trimStart().startsWith("--") }.joinToString("\n")- val statements = mutableListOf<String>()- val trigger = StringBuilder()- val chunks = withoutComments.split(";").map { it.trim().trimStart('') }- for (chunk in chunks.filter { it.isNotEmpty() }) {- if (trigger.isNotEmpty()) {- trigger.append(chunk).append(";")- if (chunk.uppercase() == "END") {- statements.add(trigger.toString())- trigger.clear()- }- } else if (chunk.uppercase().contains("CREATE TRIGGER")) {- trigger.append(chunk).append(";")- } else {- statements.add(chunk)- }- }- return statements- }-}diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.ktindex a7d648e5b..2532d16e3 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultCrypto.kt@@ -329,9 +329,16 @@ class VaultCrypto(}/**- * Encrypt data.+ * Encrypt text.*/fun encryptData(data: String): String {+ return encryptBytes(data.toByteArray(Charsets.UTF_8))+ }++ /**+ * Encrypt raw bytes.+ */+ fun encryptBytes(data: ByteArray): String {try {val iv = ByteArray(12)SecureRandom().nextBytes(iv)@@ -342,7 +349,7 @@ class VaultCrypto(cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmSpec)- val encrypted = cipher.doFinal(data.toByteArray(Charsets.UTF_8))+ val encrypted = cipher.doFinal(data)val result = ByteArray(iv.size + encrypted.size)System.arraycopy(iv, 0, result, 0, iv.size)diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.ktindex e67febe99..1f5acd5e3 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultDatabase.kt@@ -1,13 +1,16 @@package net.aliasvault.app.vaultstore+import android.database.Cursor+import android.database.MatrixCursorimport android.util.Base64import android.util.Log-import io.requery.android.database.sqlite.SQLiteDatabase+import net.aliasvault.app.rustcore.JnaInitializerimport net.aliasvault.app.vaultstore.storageprovider.StorageProvider-import java.io.File+import uniffi.aliasvault_core.SqlValue+import uniffi.aliasvault_core.SqliteMemoryDatabase/**- * Handles database storage, encryption, and decryption operations.+ * The live vault database: the decrypted bytes are opened in memory using the Rust core's SQLite client.*/class VaultDatabase(private val storageProvider: StorageProvider,@@ -17,12 +20,7 @@ class VaultDatabase(private const val TAG = "VaultDatabase"}- /**- * The database connection.- */- internal var dbConnection: SQLiteDatabase? = null-- // region Database Storage+ private var db: SqliteMemoryDatabase? = null/*** Store the encrypted database in the storage provider.@@ -45,8 +43,6 @@ class VaultDatabase(return storageProvider.getEncryptedDatabaseFile().exists()}- // endregion-/*** The id of the user's personal manifest as the last sync recorded it (engine state, stored as `{"v": id}`),* or null before the first pull.@@ -56,17 +52,13 @@ class VaultDatabase(return org.json.JSONObject(json).optString("v").takeIf { it.isNotEmpty() }}- // region Vault Unlock-/*** Unlock the vault. This can trigger biometric authentication.*/fun unlockVault(authMethods: String) {- val encryptedDbBase64 = getEncryptedDatabase()- val decrypted = crypto.decryptDataBytes(encryptedDbBase64, authMethods)-+ val decrypted = crypto.decryptDataBytes(getEncryptedDatabase(), authMethods)try {- setupDatabaseWithDecryptedData(decrypted)+ open(decrypted)} catch (e: Exception) {Log.e(TAG, "Error unlocking vault", e)throw e@@ -80,278 +72,119 @@ class VaultDatabase(return crypto.encryptionKey != null}- // endregion-- // region Database Setup-/**- * Setup the database with decrypted data.- * Uses SQLite VACUUM INTO to properly copy all schema objects including- * foreign keys, indexes, triggers, and views from file to memory.- * This is equivalent to the Swift implementation using the backup API.+ * Whether a database is open.*/+ fun isOpen(): Boolean = db != null+/**- * Setup the database with the decrypted data. The plaintext is either the raw SQLite bytes (what the app- * writes now, same as the browser extension) or base64 text of them (what older builds wrote).+ * Open the decrypted vault in memory. The plaintext is either the raw SQLite bytes+ * or base64 text of them (legacy pre-0.31.0).*/- private fun setupDatabaseWithDecryptedData(decrypted: ByteArray) {- var tempDbFile: File? = null- var sourceDb: io.requery.android.database.sqlite.SQLiteDatabase? = null- try {- // Step 1: Take the SQLite bytes as-is, or decode the legacy base64 text.- val decryptedDbData = if (isSqliteDatabase(decrypted)) {- decrypted- } else {- try {- Base64.decode(String(decrypted, Charsets.UTF_8), Base64.NO_WRAP)- } catch (e: Exception) {- Log.e(TAG, "Failed to decode base64 data after decryption", e)- throw AppError.Base64DecodeFailed(cause = e)- }- }-- // Step 2: Write decrypted data to temp file- tempDbFile = File.createTempFile("temp_db", ".sqlite")- tempDbFile.deleteOnExit()- try {- tempDbFile.writeBytes(decryptedDbData)- } catch (e: Exception) {- Log.e(TAG, "Failed to write decrypted data to temp file", e)- throw AppError.DatabaseTempWriteFailed(cause = e)- }-- // Step 3: Close any existing connection- dbConnection?.close()- dbConnection = null-- // Step 4: Open the source database from file using requery's SQLite (read-only)- sourceDb = try {- io.requery.android.database.sqlite.SQLiteDatabase.openDatabase(- tempDbFile.path,- null,- io.requery.android.database.sqlite.SQLiteDatabase.OPEN_READONLY,- )- } catch (e: Exception) {- Log.e(TAG, "Failed to open source database (file may be corrupt)", e)- throw AppError.DatabaseOpenFailed(cause = e)- }-- // Close source database before we attach it to memory db- sourceDb.close()- sourceDb = null-- // Step 5: Create in-memory database using requery's SQLite- dbConnection = try {- SQLiteDatabase.create(null)- } catch (e: Exception) {- Log.e(TAG, "Failed to create in-memory database connection", e)- throw AppError.DatabaseMemoryFailed(cause = e)- }-- // Step 6: Attach and copy database+ private fun open(decrypted: ByteArray) {+ val bytes = if (isSqliteDatabase(decrypted)) {+ decrypted+ } else {try {- // Attach the temp file as 'source' to copy from- val attachSql = "ATTACH DATABASE '${tempDbFile.path}' AS source"- dbConnection?.compileStatement(attachSql)?.execute()-- // Copy entire database using sqlite_master (preserves all schema)- dbConnection?.beginTransaction()- try {- copyCompleteDatabase()- dbConnection?.setTransactionSuccessful()- } finally {- dbConnection?.endTransaction()- }-- // Detach source- dbConnection?.compileStatement("DETACH DATABASE source")?.execute()- } catch (e: AppError) {- throw e+ Base64.decode(String(decrypted, Charsets.UTF_8), Base64.NO_WRAP)} catch (e: Exception) {- Log.e(TAG, "Failed to backup database to memory", e)- throw AppError.DatabaseBackupFailed(cause = e)+ Log.e(TAG, "Failed to decode base64 data after decryption", e)+ throw AppError.Base64DecodeFailed(cause = e)}+ }- // Step 7: Set pragmas for optimal performance and safety- try {- // PRAGMA statements must use rawQuery, not compileStatement- dbConnection?.rawQuery("PRAGMA journal_mode = WAL", null)- dbConnection?.rawQuery("PRAGMA synchronous = NORMAL", null)- dbConnection?.rawQuery("PRAGMA foreign_keys = ON", null)- } catch (e: Exception) {- Log.e(TAG, "Failed to set database pragmas", e)- throw AppError.DatabasePragmaFailed(cause = e)+ close()+ JnaInitializer.ensureInitialized()+ val opened = try {+ SqliteMemoryDatabase.fromBytes(bytes).also {+ it.queryValues("SELECT count(*) FROM sqlite_master", emptyList())}- } catch (e: AppError) {- throw e} catch (e: Exception) {- Log.e(TAG, "Error setting up database with decrypted data", e)- throw e- } finally {- // Clean up source database connection- sourceDb?.close()-- // Clean up temp file- tempDbFile?.let {- if (it.exists()) {- it.setWritable(true, true)- it.delete()- }- }+ Log.e(TAG, "Failed to open the vault database (data may be corrupt)", e)+ throw AppError.DatabaseOpenFailed(cause = e)+ }+ try {+ opened.executeBatch("PRAGMA foreign_keys = ON")+ } catch (e: Exception) {+ opened.destroy()+ Log.e(TAG, "Failed to set database pragmas", e)+ throw AppError.DatabasePragmaFailed(cause = e)}+ db = opened}/**- * Copy complete database from attached 'source' to main database.- * This copies all schema objects (tables, indexes, triggers, views) and data.+ * The open database.*/- private fun copyCompleteDatabase() {- // First, get and execute all schema statements- val schemaCursor = dbConnection?.rawQuery(- """- SELECT sql FROM source.sqlite_master- WHERE sql NOT NULL- AND type IN ('table', 'index', 'trigger', 'view')- AND name NOT LIKE 'sqlite_%'- ORDER BY- CASE type- WHEN 'table' THEN 1- WHEN 'index' THEN 2- WHEN 'trigger' THEN 3- WHEN 'view' THEN 4- END- """.trimIndent(),- null,- ) ?: error(IllegalStateException("Failed to read source schema"))+ internal fun connection(): SqliteMemoryDatabase = db ?: error("Database not initialized")- val schemaStatements = mutableListOf<String>()- schemaCursor.use {- while (it.moveToNext()) {- val sql = it.getString(0)- if (!sql.isNullOrBlank()) {- schemaStatements.add(sql)- }- }- }-- // Execute schema creation statements- for (sql in schemaStatements) {- try {- dbConnection?.compileStatement(sql)?.execute()- } catch (e: Exception) {- // Skip if already exists or is an auto-created index- if (!e.message?.contains("already exists", ignoreCase = true)!!) {- Log.w(TAG, "Schema statement may be auto-index: $sql", e)- }- }- }-- // Then copy all table data- val tablesCursor = dbConnection?.rawQuery(- "SELECT name FROM source.sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'",- null,- ) ?: error(IllegalStateException("Failed to get table list"))+ /**+ * Run a SELECT and return its rows keyed by column name. Values are String, Long, Double, ByteArray or null.+ */+ fun query(sql: String, params: List<Any?> = emptyList()): List<Map<String, Any?>> {+ val result = connection().queryValues(sql, params.map(::toSqlValue))+ return result.rows.map { row -> row.indices.associate { result.columns[it] to fromSqlValue(row[it]) } }+ }- tablesCursor.use {- while (it.moveToNext()) {- val tableName = it.getString(0)- val insertStmt = dbConnection?.compileStatement("INSERT INTO $tableName SELECT * FROM source.$tableName")- try {- insertStmt?.execute()- } finally {- insertStmt?.close()- }+ /**+ * Run a SELECT and return its rows as a cursor, for positional column access.+ */+ fun queryCursor(sql: String, params: List<Any?> = emptyList()): Cursor {+ val result = connection().queryValues(sql, params.map(::toSqlValue))+ return MatrixCursor(result.columns.toTypedArray(), result.rows.size).also { cursor ->+ for (row in result.rows) {+ cursor.addRow(row.map(::fromSqlValue).toTypedArray())}}}- // endregion-- // region Transaction Management-/**- * Begin a SQL transaction on the vault.+ * Run one INSERT, UPDATE or DELETE and return the number of rows it changed.*/- fun beginTransaction() {- val db = dbConnection ?: error(IllegalStateException("Database not initialized"))- db.compileStatement("BEGIN TRANSACTION").execute()+ fun execute(sql: String, params: List<Any?> = emptyList()): Int {+ return connection().execute(sql, params.map(::toSqlValue)).toInt()}/**- * Persist the in-memory database to encrypted local storage.- * This method can be called independently to persist the database without committing a transaction.+ * Run a multi-statement script without parameters. Migration scripts manage their own transactions and+ * pragmas, so nothing is wrapped around them.*/- fun persistDatabaseToEncryptedStorage() {- val db = dbConnection ?: error(IllegalStateException("Database not initialized"))-- // Slight delay tolerance for busy databases- try { db.rawQuery("PRAGMA busy_timeout=5000", null)?.close() } catch (_: Exception) {}-- val tempDbFile = File(storageProvider.getRandomTempFilePath())-- // Ensure the temp file does not exist yet- if (tempDbFile.exists()) {- tempDbFile.delete()- }-- try {- // Properly quote the path for SQL- val quotedPath = tempDbFile.absolutePath.replace("'", "''")- val vacuumIntoSql = "VACUUM INTO '$quotedPath'"-- // Retry up to 5 times if we hit transient locking errors- for (attempt in 1..5) {- try {- // VACUUM cannot run inside a transaction.- // End any lingering transaction (no-op if none).- try { db.compileStatement("END").execute() } catch (_: Exception) {}-- db.compileStatement(vacuumIntoSql).use { it.execute() }- break // Success, exit the loop- } catch (e: Exception) {- val msg = e.message?.lowercase().orEmpty()- val transient = msg.contains("locked") || msg.contains("busy") || msg.contains("statements in progress")-- Log.w(TAG, "VACUUM INTO attempt $attempt/5 failed: ${e.message}")+ fun executeScript(sql: String) {+ connection().executeBatch(sql)+ }- if (transient && attempt < 5) {- Thread.sleep((150L * attempt))- } else {- Log.e(TAG, "VACUUM INTO failed after retries", e)- throw e- }- }- }+ private fun toSqlValue(value: Any?): SqlValue = when (value) {+ null -> SqlValue.Null+ is SqlValue -> value+ is ByteArray -> SqlValue.Blob(value)+ is Boolean -> SqlValue.Integer(if (value) 1L else 0L)+ is Int -> SqlValue.Integer(value.toLong())+ is Long -> SqlValue.Integer(value)+ is Float -> SqlValue.Real(value.toDouble())+ is Double -> SqlValue.Real(value)+ else -> SqlValue.Text(value.toString())+ }- // Validate output file exists and has content- if (!tempDbFile.exists() || tempDbFile.length() == 0L) {- Log.e(TAG, "VACUUM INTO produced no file or empty file at ${tempDbFile.absolutePath}")- error(IllegalStateException("VACUUM INTO produced no output"))- }+ private fun fromSqlValue(value: SqlValue): Any? = when (value) {+ is SqlValue.Null -> null+ is SqlValue.Integer -> value.v1+ is SqlValue.Real -> value.v1+ is SqlValue.Text -> value.v1+ is SqlValue.Blob -> value.v1+ }- val rawData = tempDbFile.readBytes()- val base64String = android.util.Base64.encodeToString(rawData, android.util.Base64.NO_WRAP)- val encryptedBase64Data = crypto.encryptData(base64String)- storeEncryptedDatabase(encryptedBase64Data)- } catch (e: Exception) {- Log.e(TAG, "Error exporting and encrypting database", e)- throw e- } finally {- // Always clean up the temp file- try {- tempDbFile.delete()- } catch (e: Exception) {- Log.e(TAG, "Error deleting temp file", e)- }- }+ /**+ * Begin a SQL transaction on the vault.+ */+ fun beginTransaction() {+ connection().executeBatch("BEGIN TRANSACTION")}/*** Commit a SQL transaction and persist the encrypted vault.*/fun commitTransaction() {- val db = dbConnection ?: error(IllegalStateException("Database not initialized"))- db.compileStatement("COMMIT").execute()+ connection().executeBatch("COMMIT")persistDatabaseToEncryptedStorage()}@@ -359,24 +192,42 @@ class VaultDatabase(* Rollback a SQL transaction on the vault.*/fun rollbackTransaction() {- val db = dbConnection ?: error(IllegalStateException("Database not initialized"))- db.compileStatement("ROLLBACK").execute()+ connection().executeBatch("ROLLBACK")}- // endregion+ /**+ * Persist the in-memory database to encrypted local storage.+ * This method can be called independently to persist the database without committing a transaction.+ */+ fun persistDatabaseToEncryptedStorage() {+ val connection = connection()+ // End any open transactions.+ try { connection.executeBatch("END") } catch (_: Exception) {}+ try {+ storeEncryptedDatabase(crypto.encryptBytes(export()))+ } catch (e: Exception) {+ Log.e(TAG, "Error exporting and encrypting database", e)+ throw e+ }+ }- // region Cleanup+ /**+ * The database as SQLite file bytes, compacted first when no transaction is open.+ */+ fun export(): ByteArray {+ val connection = connection()+ try { connection.executeBatch("VACUUM") } catch (_: Exception) {}+ return connection.export()+ }/*** Close the database connection.*/fun close() {- dbConnection?.close()- dbConnection = null+ db?.destroy()+ db = null}- // endregion-/*** Whether these plaintext bytes are a SQLite database rather than base64 text of one.*/diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.ktindex 7d514f70b..bab42933f 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt@@ -2,7 +2,6 @@ package net.aliasvault.app.vaultstoreimport android.os.SystemClockimport android.util.Log-import io.requery.android.database.sqlite.SQLiteDatabaseimport kotlinx.coroutines.suspendCancellableCoroutineimport net.aliasvault.app.vaultstore.interfaces.CryptoOperationCallbackimport net.aliasvault.app.vaultstore.interfaces.ItemOperationCallback@@ -348,8 +347,6 @@ class VaultStore(* Execute a read-only SQL query (SELECT) on the vault.*/fun executeQuery(queryString: String, params: Array<Any?>): List<Map<String, Any?>> {- val db = database.dbConnection ?: error("Database not initialized")-// Process params - convert base64-prefixed strings to ByteArray for blob bindingval convertedParams = params.map { param ->when {@@ -361,39 +358,15 @@ class VaultStore(param is ByteArray -> paramelse -> param.toString()}- }.toTypedArray()-- val cursor = db.query(queryString, convertedParams)- val results = mutableListOf<Map<String, Any?>>()-- cursor.use {- val columnNames = it.columnNames- while (it.moveToNext()) {- val row = mutableMapOf<String, Any?>()- for (columnName in columnNames) {- val colIndex = it.getColumnIndexOrThrow(columnName)- val colType = it.getType(colIndex)- when (colType) {- android.database.Cursor.FIELD_TYPE_NULL -> row[columnName] = null- android.database.Cursor.FIELD_TYPE_INTEGER -> row[columnName] = it.getLong(colIndex)- android.database.Cursor.FIELD_TYPE_FLOAT -> row[columnName] = it.getDouble(colIndex)- android.database.Cursor.FIELD_TYPE_STRING -> row[columnName] = it.getString(colIndex)- android.database.Cursor.FIELD_TYPE_BLOB -> row[columnName] = it.getBlob(colIndex)- }- }- results.add(row)- }}- return results+ return database.query(queryString, convertedParams)}/*** Execute an SQL update on the vault that mutates it.*/fun executeUpdate(queryString: String, params: Array<Any?>): Int {- val db = database.dbConnection ?: error("Database not initialized")-// Process params - convert base64-prefixed strings to ByteArray for blob bindingval processedParams = params.map { param ->when {@@ -407,63 +380,15 @@ class VaultStore(}}- val stmt = db.compileStatement(queryString)- try {- processedParams.forEachIndexed { index, value ->- when (value) {- null -> stmt.bindNull(index + 1)- is ByteArray -> stmt.bindBlob(index + 1, value)- else -> stmt.bindString(index + 1, value.toString())- }- }- stmt.execute()- } finally {- stmt.close()- }-- // Get the number of affected rows- val affectedCursor = db.rawQuery("SELECT changes()", null)- affectedCursor.use {- if (it.moveToFirst()) {- return it.getInt(0)- }- }- return 0+ return database.execute(queryString, processedParams)}/**- * Execute a raw SQL command on the vault without parameters.- * Splits the query by semicolons to handle multiple statements (see SqlScript).- *- * Note: Migration SQL scripts handle their own transactions and PRAGMA statements.- * PRAGMA foreign_keys statements MUST be executed outside of transactions to take effect,- * which is why we execute them using rawQuery instead of compileStatement.+ * Execute a raw SQL script (one or more statements) on the vault without parameters.+ * Migration scripts handle their own transactions and PRAGMA statements.*/fun executeRaw(queryString: String) {- val db = database.dbConnection ?: error("Database not initialized")-- SqlScript.splitStatements(queryString)- .forEach { trimmed ->- val upperTrimmed = trimmed.uppercase()- when {- // Handle PRAGMA statements using rawQuery (required for PRAGMA to work properly)- upperTrimmed.startsWith("PRAGMA") -> db.rawQuery(trimmed, null)?.close()- // Handle transaction control statements using execSQL- isTransactionControlStatement(upperTrimmed) -> db.execSQL(trimmed)- // Use compileStatement for all other SQL (DDL and DML)- else -> db.compileStatement(trimmed).execute()- }- }- }-- /**- * Check if a SQL statement is a transaction control statement.- */- private fun isTransactionControlStatement(upperStatement: String): Boolean {- return upperStatement.startsWith("BEGIN TRANSACTION") ||- upperStatement.startsWith("BEGIN") ||- upperStatement.startsWith("COMMIT") ||- upperStatement.startsWith("ROLLBACK")+ database.executeScript(queryString)}/**@@ -857,11 +782,7 @@ class VaultStore(/*** Get all passkeys for an item.*/- @Suppress("UnusedParameter")- fun getPasskeysForItem(- itemId: java.util.UUID,- db: io.requery.android.database.sqlite.SQLiteDatabase,- ): List<net.aliasvault.app.vaultstore.models.Passkey> {+ fun getPasskeysForItem(itemId: java.util.UUID): List<net.aliasvault.app.vaultstore.models.Passkey> {return passkey.getPasskeysForItem(itemId)}@@ -895,19 +816,14 @@ class VaultStore(/*** Get a passkey by its ID.*/- @Suppress("UnusedParameter")- fun getPasskeyById(- passkeyId: java.util.UUID,- db: io.requery.android.database.sqlite.SQLiteDatabase,- ): net.aliasvault.app.vaultstore.models.Passkey? {+ fun getPasskeyById(passkeyId: java.util.UUID): net.aliasvault.app.vaultstore.models.Passkey? {return passkey.getPasskeyById(passkeyId)}/*** Insert a new passkey into the database.*/- @Suppress("UnusedParameter")- fun insertPasskey(passkeyObj: net.aliasvault.app.vaultstore.models.Passkey, db: io.requery.android.database.sqlite.SQLiteDatabase) {+ fun insertPasskey(passkeyObj: net.aliasvault.app.vaultstore.models.Passkey) {passkey.insertPasskey(passkeyObj)}diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.ktindex 047110faa..cdcef11e1 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt@@ -1,9 +1,7 @@package net.aliasvault.app.vaultstore-import android.database.Cursorimport android.util.Base64import android.util.Log-import io.requery.android.database.sqlite.SQLiteDatabaseimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.withContextimport net.aliasvault.app.rustcore.JnaInitializer@@ -13,8 +11,8 @@ import net.aliasvault.app.vaultstore.storageprovider.StorageProviderimport net.aliasvault.app.webapi.WebApiServiceimport org.json.JSONArrayimport org.json.JSONObject+import uniffi.aliasvault_core.SqliteMemoryDatabaseimport uniffi.aliasvault_core.VaultSyncSession-import java.io.Fileimport java.net.SocketTimeoutException/**@@ -40,8 +38,8 @@ class VaultSyncEngine(private const val DERIVATION_PARAMS_STATE_KEY = "encryptionKeyDerivationParams"}- private var staging: SQLiteDatabase? = null- private var stagingFile: File? = null+ /** The engine staging database used for internal sync and merge operations. */+ private var staging: SqliteMemoryDatabase? = nullprivate var runLog = VaultSyncRunLog("")/**@@ -123,14 +121,21 @@ class VaultSyncEngine(JSONObject()}"dbQuery" -> {- val rows = query(database(command.optString("db")), command.optString("sql"), command.optJSONArray("params") ?: JSONArray())- JSONObject().put("rows", rows)+ val params = (command.optJSONArray("params") ?: JSONArray()).toString()+ JSONObject().put("rows", JSONArray(database(command.optString("db")).query(command.optString("sql"), params)))}"dbExec" -> {- exec(database(command.optString("db")), command.optJSONArray("statements") ?: JSONArray())+ database(command.optString("db")).exec((command.optJSONArray("statements") ?: JSONArray()).toString())JSONObject()}- "dbExport" -> JSONObject().put("bytes", Base64.encodeToString(export(database(command.optString("db"))), Base64.NO_WRAP))+ "dbExport" -> {+ val bytes = when (val db = command.optString("db")) {+ "local" -> vaultStore.database.export()+ "staging" -> stagingDatabase().export()+ else -> error("Unknown database $db")+ }+ JSONObject().put("bytes", Base64.encodeToString(bytes, Base64.NO_WRAP))+ }"vaultStore" -> storeVault(command)"vaultLoad" -> JSONObject().put("encryptedBlob", if (vaultStore.hasEncryptedDatabase()) vaultStore.getEncryptedDatabase() else JSONObject.NULL)"markClean" -> {@@ -247,126 +252,32 @@ class VaultSyncEngine(// region SQLite- private fun database(name: String): SQLiteDatabase = when (name) {- "local" -> vaultStore.database.dbConnection ?: error("The vault is not unlocked")- "staging" -> staging ?: error("The staging database is not open")+ private fun database(name: String): SqliteMemoryDatabase = when (name) {+ "local" -> vaultStore.database.connection()+ "staging" -> stagingDatabase()else -> error("Unknown database $name")}+ private fun stagingDatabase(): SqliteMemoryDatabase = staging ?: error("The staging database is not open")+/**- * Open the staging database: from SQLite bytes (as a temporary file), or fresh in memory with the current schema.+ * Open the staging database in memory.*/private fun openStaging(bytesBase64: String?) {closeStaging()- if (bytesBase64 != null) {- val file = File(storageProvider.getRandomTempFilePath())- file.writeBytes(Base64.decode(bytesBase64, Base64.NO_WRAP))- stagingFile = file- staging = SQLiteDatabase.openDatabase(file.path, null, SQLiteDatabase.OPEN_READWRITE).also {- it.rawQuery("PRAGMA foreign_keys = OFF", null)?.close()- }+ staging = if (bytesBase64 != null) {+ SqliteMemoryDatabase.fromBytes(Base64.decode(bytesBase64, Base64.NO_WRAP))} else {- staging = SQLiteDatabase.create(null).also {- executeScript(it, VaultSql.completeSchema)- // The schema script ends by turning foreign keys on; the engine inserts rows in codec order, not FK order.- it.rawQuery("PRAGMA foreign_keys = OFF", null)?.close()- }+ SqliteMemoryDatabase.withSchema(VaultSql.completeSchema)+ }.also {+ // The schema script ends by turning foreign keys on; the engine inserts rows in codec order, not FK order.+ it.executeBatch("PRAGMA foreign_keys = OFF")}}private fun closeStaging() {- staging?.close()+ staging?.destroy()staging = null- stagingFile?.delete()- stagingFile = null- }-- /**- * Run a multi-statement SQL script. Statements split on semicolons, except inside a trigger body.- */- private fun executeScript(db: SQLiteDatabase, script: String) {- for (statement in SqlScript.splitStatements(script)) {- val upper = statement.uppercase()- when {- upper.startsWith("PRAGMA") -> db.rawQuery(statement, null)?.close()- upper.startsWith("BEGIN") || upper.startsWith("COMMIT") || upper.startsWith("ROLLBACK") -> db.execSQL(statement)- else -> db.compileStatement(statement).use { it.execute() }- }- }- }-- private fun export(db: SQLiteDatabase): ByteArray {- val file = File(storageProvider.getRandomTempFilePath())- file.delete()- try {- val quoted = file.absolutePath.replace("'", "''")- db.compileStatement("VACUUM INTO '$quoted'").use { it.execute() }- return file.readBytes()- } finally {- file.delete()- }- }-- private fun bindValue(value: Any?): Any? = when (value) {- null, JSONObject.NULL -> null- is Boolean -> if (value) 1L else 0L- is Int -> value.toLong()- is Long -> value- is Double -> value- is Float -> value.toDouble()- is String -> value- is JSONObject -> if (value.has("__b64")) Base64.decode(value.getString("__b64"), Base64.NO_WRAP) else value.toString()- else -> value.toString()- }-- private fun query(db: SQLiteDatabase, sql: String, params: JSONArray): JSONArray {- val args = Array<Any?>(params.length()) { bindValue(params.opt(it)) }- val rows = JSONArray()- db.rawQuery(sql, args).use { cursor ->- while (cursor.moveToNext()) {- val row = JSONObject()- for (index in 0 until cursor.columnCount) {- val name = cursor.getColumnName(index)- when (cursor.getType(index)) {- Cursor.FIELD_TYPE_INTEGER -> row.put(name, cursor.getLong(index))- Cursor.FIELD_TYPE_FLOAT -> row.put(name, cursor.getDouble(index))- Cursor.FIELD_TYPE_STRING -> row.put(name, cursor.getString(index))- Cursor.FIELD_TYPE_BLOB -> row.put(name, JSONObject().put("__b64", Base64.encodeToString(cursor.getBlob(index), Base64.NO_WRAP)))- else -> row.put(name, JSONObject.NULL)- }- }- rows.put(row)- }- }- return rows- }-- private fun exec(db: SQLiteDatabase, statements: JSONArray) {- db.beginTransaction()- try {- for (index in 0 until statements.length()) {- val entry = statements.getJSONObject(index)- db.compileStatement(entry.getString("sql")).use { statement ->- bindParams(statement, entry.optJSONArray("params") ?: JSONArray())- statement.execute()- }- }- db.setTransactionSuccessful()- } finally {- db.endTransaction()- }- }-- private fun bindParams(statement: io.requery.android.database.sqlite.SQLiteStatement, params: JSONArray) {- for (position in 0 until params.length()) {- when (val value = bindValue(params.opt(position))) {- null -> statement.bindNull(position + 1)- is Long -> statement.bindLong(position + 1, value)- is Double -> statement.bindDouble(position + 1, value)- is ByteArray -> statement.bindBlob(position + 1, value)- else -> statement.bindString(position + 1, value.toString())- }- }}// endregiondiff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncRunLog.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncRunLog.ktindex 60e1aa00c..9efa75926 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncRunLog.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncRunLog.kt@@ -11,6 +11,7 @@ import java.util.Locale*/class VaultSyncRunLog(private val operation: String) {companion object {+ /** Logcat tag for the sync log lines. */const val TAG = "VaultSync"private const val ENGINE_TAG = "VaultSyncEngine"private const val SLOW_COMMAND_MS = 50Ldiff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.ktindex 7bca4fe3f..fd4cceed9 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/BaseRepository.kt@@ -147,69 +147,13 @@ open class BaseRepository(* Execute a SELECT query on the database.*/protected fun executeQuery(query: String, params: Array<Any?>): List<Map<String, Any?>> {- val db = database.dbConnection ?: error("Database not initialized")- val cursor = db.query(query, params.map { it?.toString() }.toTypedArray())-- val results = mutableListOf<Map<String, Any?>>()- cursor.use {- val columnNames = it.columnNames- while (it.moveToNext()) {- val row = mutableMapOf<String, Any?>()- for (columnName in columnNames) {- when (it.getType(it.getColumnIndexOrThrow(columnName))) {- android.database.Cursor.FIELD_TYPE_NULL -> row[columnName] = null- android.database.Cursor.FIELD_TYPE_INTEGER -> row[columnName] = it.getLong(- it.getColumnIndexOrThrow(columnName),- )- android.database.Cursor.FIELD_TYPE_FLOAT -> row[columnName] = it.getDouble(- it.getColumnIndexOrThrow(columnName),- )- android.database.Cursor.FIELD_TYPE_STRING -> row[columnName] = it.getString(- it.getColumnIndexOrThrow(columnName),- )- android.database.Cursor.FIELD_TYPE_BLOB -> row[columnName] = it.getBlob(- it.getColumnIndexOrThrow(columnName),- )- }- }- results.add(row)- }- }-- return results+ return database.query(query, params.toList())}/*** Execute an UPDATE, INSERT, or DELETE query on the database.*/protected fun executeUpdate(query: String, params: Array<Any?>): Int {- val db = database.dbConnection ?: error("Database not initialized")-- val statement = db.compileStatement(query)- try {- // Bind parameters- params.forEachIndexed { index, param ->- when (param) {- null -> statement.bindNull(index + 1)- is ByteArray -> statement.bindBlob(index + 1, param)- is Long -> statement.bindLong(index + 1, param)- is Double -> statement.bindDouble(index + 1, param)- else -> statement.bindString(index + 1, param.toString())- }- }- statement.execute()- } finally {- statement.close()- }-- // Get the number of affected rows- val cursor = db.rawQuery("SELECT changes()", null)- cursor.use {- if (it.moveToFirst()) {- return it.getInt(0)- }- }-- return 0+ return database.execute(query, params.toList())}}diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.ktindex 33464c101..3e09f7753 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/ItemRepository.kt@@ -95,7 +95,7 @@ class ItemRepository(database: VaultDatabase) : BaseRepository(database) {// Build folder pathsval folderPaths = buildFolderPaths()- val itemResults = executeQueryWithBlobs(ItemQueries.GET_ALL_ACTIVE, emptyArray())+ val itemResults = executeQuery(ItemQueries.GET_ALL_ACTIVE, emptyArray())for (row in itemResults) {try {val idString = row["Id"] as? String ?: continue@@ -205,7 +205,7 @@ class ItemRepository(database: VaultDatabase) : BaseRepository(database) {* @return Item object or null if not found.*/fun getById(itemId: String): Item? {- val itemResults = executeQueryWithBlobs(ItemQueries.GET_BY_ID, arrayOf(itemId.lowercase()))+ val itemResults = executeQuery(ItemQueries.GET_BY_ID, arrayOf(itemId.lowercase()))val row = itemResults.firstOrNull() ?: return null// Build folder paths@@ -324,7 +324,7 @@ class ItemRepository(database: VaultDatabase) : BaseRepository(database) {@Suppress("LoopWithTooManyJumpStatements")private fun getItemsWithoutFields(query: String, label: String): List<Item> {val items = mutableListOf<Item>()- val results = executeQueryWithBlobs(query, emptyArray())+ val results = executeQuery(query, emptyArray())// Build folder pathsval folderPaths = buildFolderPaths()@@ -612,41 +612,4 @@ class ItemRepository(database: VaultDatabase) : BaseRepository(database) {else -> FieldMetadata(fieldKey, FieldType.TEXT, false, false)}}-- /**- * Execute a SELECT query that may return BLOB columns.- * Unlike executeQuery which converts all params to strings, this preserves ByteArray types.- */- private fun executeQueryWithBlobs(query: String, params: Array<Any?>): List<Map<String, Any?>> {- val db = database.dbConnection ?: error("Database not initialized")- val cursor = db.query(query, params.map { it?.toString() }.toTypedArray())-- val results = mutableListOf<Map<String, Any?>>()- cursor.use {- val columnNames = it.columnNames- while (it.moveToNext()) {- val row = mutableMapOf<String, Any?>()- for (columnName in columnNames) {- when (it.getType(it.getColumnIndexOrThrow(columnName))) {- android.database.Cursor.FIELD_TYPE_NULL -> row[columnName] = null- android.database.Cursor.FIELD_TYPE_INTEGER -> row[columnName] = it.getLong(- it.getColumnIndexOrThrow(columnName),- )- android.database.Cursor.FIELD_TYPE_FLOAT -> row[columnName] = it.getDouble(- it.getColumnIndexOrThrow(columnName),- )- android.database.Cursor.FIELD_TYPE_STRING -> row[columnName] = it.getString(- it.getColumnIndexOrThrow(columnName),- )- android.database.Cursor.FIELD_TYPE_BLOB -> row[columnName] = it.getBlob(- it.getColumnIndexOrThrow(columnName),- )- }- }- results.add(row)- }- }-- return results- }}diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.ktindex bf57a54e7..b3ae4e54f 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/repositories/PasskeyRepository.kt@@ -100,36 +100,27 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {* @param passkey The passkey to insert*/fun insert(passkey: Passkey) {- val db = database.dbConnection ?: error("Vault not unlocked")-val publicKeyString = String(passkey.publicKey, Charsets.UTF_8)val privateKeyString = String(passkey.privateKey, Charsets.UTF_8)- val statement = db.compileStatement(PasskeyQueries.INSERT)- statement.use {- it.bindString(1, passkey.id.toString().lowercase())- it.bindString(2, passkey.parentItemId.toString().lowercase()) // Note: still called parentItemId but references ItemId- it.bindString(3, passkey.rpId)- if (passkey.userHandle != null) {- it.bindBlob(4, passkey.userHandle)- } else {- it.bindNull(4)- }- it.bindString(5, publicKeyString)- it.bindString(6, privateKeyString)- if (passkey.prfKey != null) {- it.bindBlob(7, passkey.prfKey)- } else {- it.bindNull(7)- }- it.bindString(8, passkey.displayName)- it.bindString(9, DateHelpers.toStandardFormat(passkey.createdAt))- it.bindString(10, DateHelpers.toStandardFormat(passkey.updatedAt))- it.bindLong(11, if (passkey.isDeleted) 1 else 0)- it.bindString(12, passkey.parentItemId.toString().lowercase())- it.bindString(13, activeManifestId())- it.executeInsert()- }+ database.execute(+ PasskeyQueries.INSERT,+ listOf(+ passkey.id.toString().lowercase(),+ passkey.parentItemId.toString().lowercase(), // Note: still called parentItemId but references ItemId+ passkey.rpId,+ passkey.userHandle,+ publicKeyString,+ privateKeyString,+ passkey.prfKey,+ passkey.displayName,+ DateHelpers.toStandardFormat(passkey.createdAt),+ DateHelpers.toStandardFormat(passkey.updatedAt),+ if (passkey.isDeleted) 1L else 0L,+ passkey.parentItemId.toString().lowercase(),+ activeManifestId(),+ ),+ )}/**@@ -364,12 +355,12 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {userName: String? = null,userId: ByteArray? = null,): List<PasskeyWithCredentialInfo> {- val db = database.dbConnection ?: return emptyList()+ if (!database.isOpen()) return emptyList()val results = mutableListOf<PasskeyWithCredentialInfo>()- val cursor = db.query(+ val cursor = database.queryCursor(PasskeyQueries.GET_WITH_CREDENTIAL_INFO,- arrayOf(FieldKey.LOGIN_USERNAME, FieldKey.LOGIN_EMAIL, rpId),+ listOf(FieldKey.LOGIN_USERNAME, FieldKey.LOGIN_EMAIL, rpId),)cursor.use {@@ -411,12 +402,12 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {* @return List of ItemWithCredentialInfo objects with all URLs.*/fun getAllItemsWithoutPasskey(): List<ItemWithCredentialInfo> {- val db = database.dbConnection ?: return emptyList()+ if (!database.isOpen()) return emptyList()val results = mutableListOf<ItemWithCredentialInfo>()- val cursor = db.query(+ val cursor = database.queryCursor(PasskeyQueries.GET_ALL_ITEMS_WITHOUT_PASSKEY,- arrayOf(FieldKey.LOGIN_URL, FieldKey.LOGIN_USERNAME, FieldKey.LOGIN_EMAIL, FieldKey.LOGIN_PASSWORD),+ listOf(FieldKey.LOGIN_URL, FieldKey.LOGIN_USERNAME, FieldKey.LOGIN_EMAIL, FieldKey.LOGIN_PASSWORD),)cursor.use {@@ -492,10 +483,10 @@ class PasskeyRepository(database: VaultDatabase) : BaseRepository(database) {* @return List of PasskeyWithItem objects.*/fun getAllWithItems(): List<PasskeyWithItem> {- val db = database.dbConnection ?: return emptyList()+ if (!database.isOpen()) return emptyList()val results = mutableListOf<PasskeyWithItem>()- val cursor = db.query(PasskeyQueries.GET_ALL_WITH_ITEMS, arrayOf(FieldKey.LOGIN_USERNAME, FieldKey.LOGIN_EMAIL))+ val cursor = database.queryCursor(PasskeyQueries.GET_ALL_WITH_ITEMS, listOf(FieldKey.LOGIN_USERNAME, FieldKey.LOGIN_EMAIL))cursor.use {while (it.moveToNext()) {diff --git a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swiftindex 9562e4799..2d8a93904 100644--- a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift+++ b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift@@ -1,5 +1,4 @@import Foundation-import SQLiteimport LocalAuthenticationimport VaultStoreKitimport VaultModels@@ -166,7 +165,7 @@ public class VaultManager: NSObject {rejecter reject: @escaping RCTPromiseRejectBlock) {do {// Parse all params to the correct type- let bindingParams: [(any SQLite.Binding)?] = params.map { param in+ let bindingParams: [SqliteBindValue] = params.map { param inif param is NSNull {return nil} else if let value = param as? String {@@ -197,7 +196,7 @@ public class VaultManager: NSObject {rejecter reject: @escaping RCTPromiseRejectBlock) {do {// Parse all params to the correct type- let bindingParams: [(any SQLite.Binding)?] = params.map { param in+ let bindingParams: [SqliteBindValue] = params.map { param inif param is NSNull {return nil} else if let value = param as? String {diff --git a/apps/mobile-app/ios/Podfile b/apps/mobile-app/ios/Podfileindex bba4dbcfd..83a16cb75 100644--- a/apps/mobile-app/ios/Podfile+++ b/apps/mobile-app/ios/Podfile@@ -15,7 +15,6 @@ prepare_react_native_project!target 'AliasVault' douse_expo_modules!- pod 'SQLite.swift', '~> 0.14.0'pod 'SwiftLint', :configurations => ['Debug']pod 'Macaw', '~> 0.9.7'pod 'ExpoModulesJSI', :path => '../node_modules/expo-modules-jsi/apple'@@ -95,16 +94,13 @@ target 'AliasVault' doendtarget 'Autofill' do- pod 'SQLite.swift', '~> 0.14.0'pod 'Macaw', '~> 0.9.7'endtarget 'VaultStoreKit' do- pod 'SQLite.swift', '~> 0.14.0'endtarget 'VaultStoreKitTests' do- pod 'SQLite.swift', '~> 0.14.0'endtarget 'VaultUI' dodiff --git a/apps/mobile-app/ios/Podfile.lock b/apps/mobile-app/ios/Podfile.lockindex 40c2b1e65..eb62f2c0b 100644--- a/apps/mobile-app/ios/Podfile.lock+++ b/apps/mobile-app/ios/Podfile.lock@@ -2367,9 +2367,6 @@ PODS:- ReactCommon/turbomodule/core- ReactNativeDependencies- Yoga- - SQLite.swift (0.14.1):- - SQLite.swift/standard (= 0.14.1)- - SQLite.swift/standard (0.14.1)- SwiftLint (0.62.2)- SWXMLHash (7.0.2)- Yoga (0.0.0)@@ -2491,7 +2488,6 @@ DEPENDENCIES:- RNScreens (from `../node_modules/react-native-screens`)- RNSVG (from `../node_modules/react-native-svg`)- RNWorklets (from `../node_modules/react-native-worklets`)- - SQLite.swift (~> 0.14.0)- SwiftLint- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)@@ -2499,7 +2495,6 @@ SPEC REPOS:trunk:- Macaw- OpenSSL-Universal- - SQLite.swift- SwiftLint- SWXMLHash@@ -2765,7 +2760,7 @@ SPEC CHECKSUMS:ExpoUI: b8da9e23eb4cef30e4797bc6de102a9178e6cd2bExpoWebBrowser: 6e3d90e3fe1952d21a8494872b1e1a485cd50e2fFBLazyVector: 24e62c765683b8d89006a88a2c8f5cf019f0074d- hermes-engine: a384bd37273f335daf0965c0fd0aa46f197e0175+ hermes-engine: f684a01742a30bb5d7332da47aa184074d8ae9b4Macaw: 7af8ea57aa2cab35b4a52a45e6f85eea753ea9aeOpenSSL-Universal: 6082b0bf950e5636fe0d78def171184e2b3899c2RCTDeprecation: a4c521821fab57cbb125b36effe84d897d0dfa12@@ -2776,7 +2771,7 @@ SPEC CHECKSUMS:React: e2dc35338068bbd299c66f043ae0d7f25de8499eReact-callinvoker: 28b25d21b124c26cebaea713ba7d801b9351dc48React-Core: 02ed7d2ffb70437bdf2aba074a13078a7b0b9ff0- React-Core-prebuilt: b87f4cdecb2229f7d0943644cef9a084d433983d+ React-Core-prebuilt: 43e26cf5ff020a09ab026c6f6277ad013fa0044fReact-CoreModules: b3a5a42dadcde3b5d47b325bd912eb2ced89e146React-cxxreact: fe8f88dda044e5905e99a00f41b7a874c3908716React-debug: 92944dc4d89f56d640e75498266cbde557a48189@@ -2846,18 +2841,17 @@ SPEC CHECKSUMS:ReactAppDependencyProvider: 25c9c516839be2c5e3d3344f95dc7da5f7e63fc2ReactCodegen: 8538eb1bd61031ede1dadd7d725ee1d42dbdceaaReactCommon: 7dfc3250793bf36cf221096ff59e1179e13eef7f- ReactNativeDependencies: b8c3edb93459c2b4531665273e0e904ab3c12ba2+ ReactNativeDependencies: 2c0483134df7ac059c1f1c484b4fbf6a40451fb5RNCAsyncStorage: 2ad919e88b8bc2cd80e8697ce66d04d006743283RNGestureHandler: 2ff61eac036eaf89f6818bf4ed9c39771a17d134RNReanimated: f735b1747a7a93bda7ca102c6d37a3cf54b6d5e8RNScreens: 991cc417cd396602a6cf59a42139e5a9d91462a9RNSVG: 04044c3abcf177fd674a1a3d13097efa1adebcbeRNWorklets: 4931990f73bc8f347586918b2e13f11dfadf3b75- SQLite.swift: 2992550ebf3c5b268bf4352603e3df87d2a4ed72SwiftLint: f84fc7d844e9cde0dc4f5013af608a269e317abaSWXMLHash: dd733a457e9c4fe93b1538654057aefae4acb382Yoga: 77dfa8673de2874e1855002ae59c68b8be9b007b-PODFILE CHECKSUM: c259fdf017060f6f592731838af8fe1d2eab07d0+PODFILE CHECKSUM: 026181d634d15e69d0bc88472aa6fd4c4a545632COCOAPODS: 1.16.2diff --git a/apps/mobile-app/ios/VaultStoreKit/Database/DatabaseClient.swift b/apps/mobile-app/ios/VaultStoreKit/Database/DatabaseClient.swiftindex aee6c8f27..0e76d9e4a 100644--- a/apps/mobile-app/ios/VaultStoreKit/Database/DatabaseClient.swift+++ b/apps/mobile-app/ios/VaultStoreKit/Database/DatabaseClient.swift@@ -1,8 +1,7 @@import Foundation-import SQLite-/// Type alias for SQLite bind values.-public typealias SqliteBindValue = Binding?+/// A query parameter: nil, String, Int, Int64, Double, Bool, Data, or a base64 string behind `av-base64-to-blob:`.+public typealias SqliteBindValue = Any?/// Protocol for core database operations needed by repositories./// Abstracts the SQLite database connection to allow for testing and flexibility.diff --git a/apps/mobile-app/ios/VaultStoreKit/Database/Mappers/ItemMapper.swift b/apps/mobile-app/ios/VaultStoreKit/Database/Mappers/ItemMapper.swiftindex 90b157c12..c4a2ef17b 100644--- a/apps/mobile-app/ios/VaultStoreKit/Database/Mappers/ItemMapper.swift+++ b/apps/mobile-app/ios/VaultStoreKit/Database/Mappers/ItemMapper.swift@@ -1,5 +1,4 @@import Foundation-import SQLiteimport VaultModelsimport VaultUtils@@ -32,11 +31,9 @@ public struct ItemRow {self.itemType = itemTypeself.folderId = row["FolderId"] as? String- // Handle logo data - can be base64 string or Blob+ // BLOB columns arrive as base64 textif let logoBase64 = row["Logo"] as? String {self.logo = Data(base64Encoded: logoBase64)- } else if let logoBlob = row["Logo"] as? SQLite.Blob {- self.logo = Data(logoBlob.bytes)} else {self.logo = nil}diff --git a/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift b/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swiftindex d2c371aac..0437e6404 100644--- a/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift+++ b/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift@@ -1,5 +1,4 @@import Foundation-import SQLite3import RustCoreFrameworkimport VaultModelsimport VaultUtils@@ -25,7 +24,8 @@ public final class VaultSyncEngine {private let vaultStore: VaultStoreprivate let webApiService: WebApiService- private var staging: OpaquePointer?+ /// The engine's staging database, held in the Rust core's memory.+ private var staging: SqliteMemoryDatabase?private var runLog = VaultSyncRunLog(operation: "")public init(vaultStore: VaultStore, webApiService: WebApiService) {@@ -115,14 +115,23 @@ public final class VaultSyncEngine {return [:]case "dbQuery":let db = try database(named: command["db"] as? String ?? "")- return ["rows": try Self.query(db, sql: command["sql"] as? String ?? "", params: command["params"] as? [Any] ?? [])]+ let rowsJson = try db.query(sql: command["sql"] as? String ?? "", paramsJson: try Self.serializeJson(command["params"] ?? []))+ return ["rows": try Self.parseJsonArray(rowsJson)]case "dbExec":let db = try database(named: command["db"] as? String ?? "")- try Self.exec(db, statements: command["statements"] as? [[String: Any]] ?? [])+ try db.exec(statementsJson: try Self.serializeJson(command["statements"] ?? []))return [:]case "dbExport":- let db = try database(named: command["db"] as? String ?? "")- return ["bytes": try Self.serialize(db).base64EncodedString()]+ let bytes: Data+ switch command["db"] as? String ?? "" {+ case "local":+ bytes = try vaultStore.exportDatabase()+ case "staging":+ bytes = try database(named: "staging").export()+ case let name:+ throw AppError.unknownError(message: "Unknown database \(name)")+ }+ return ["bytes": bytes.base64EncodedString()]case "vaultStore":return try storeVault(command)case "vaultLoad":@@ -240,13 +249,13 @@ public final class VaultSyncEngine {// MARK: - SQLite- private func database(named name: String) throws -> OpaquePointer {+ private func database(named name: String) throws -> SqliteMemoryDatabase {switch name {case "local":guard let connection = vaultStore.dbConnection else {throw AppError.unknownError(message: "The vault is not unlocked")}- return connection.handle+ return connectioncase "staging":guard let staging = staging else {throw AppError.unknownError(message: "The staging database is not open")@@ -257,160 +266,24 @@ public final class VaultSyncEngine {}}- /// Open the staging database: from SQLite bytes, or fresh with the current client schema.+ /// Open the staging database in memory: from SQLite bytes, or fresh with the current client schema.private func openStaging(bytesBase64: String?) throws {closeStaging()- var db: OpaquePointer?- guard sqlite3_open(":memory:", &db) == SQLITE_OK, let opened = db else {- throw AppError.databaseMemoryFailed- }- staging = opened-+ let opened: SqliteMemoryDatabaseif let bytesBase64 = bytesBase64, let bytes = Data(base64Encoded: bytesBase64) {- let count = Int32(bytes.count)- let result = bytes.withUnsafeBytes { (pointer: UnsafeRawBufferPointer) -> Int32 in- guard let base = pointer.baseAddress, let buffer = sqlite3_malloc(count) else { return SQLITE_ERROR }- memcpy(buffer, base, Int(count))- return sqlite3_deserialize(opened, "main", buffer.assumingMemoryBound(to: UInt8.self), Int64(count), Int64(count), UInt32(SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE))- }- guard result == SQLITE_OK else {- throw AppError.databaseOpenFailed- }- try Self.execute(opened, "PRAGMA foreign_keys = OFF")+ opened = try SqliteMemoryDatabase.fromBytes(bytes: bytes)} else {- try Self.execute(opened, VaultSql.completeSchema)- // The schema script ends by turning foreign keys on; the engine inserts rows in codec order, not FK order.- try Self.execute(opened, "PRAGMA foreign_keys = OFF")+ opened = try SqliteMemoryDatabase.withSchema(schemaSql: VaultSql.completeSchema)}+ // The schema script ends by turning foreign keys on; the engine inserts rows in codec order, not FK order.+ try opened.executeBatch(sql: "PRAGMA foreign_keys = OFF")+ staging = opened}private func closeStaging() {- if let staging = staging {- sqlite3_close(staging)- }staging = nil}- private static func execute(_ db: OpaquePointer, _ sql: String) throws {- var errorMessage: UnsafeMutablePointer<CChar>?- guard sqlite3_exec(db, sql, nil, nil, &errorMessage) == SQLITE_OK else {- let message = errorMessage.map { String(cString: $0) } ?? "unknown error"- sqlite3_free(errorMessage)- throw AppError.unknownError(message: "SQL failed: \(message)")- }- }-- private static func serialize(_ db: OpaquePointer) throws -> Data {- var size: sqlite3_int64 = 0- guard let serialized = sqlite3_serialize(db, "main", &size, 0) else {- throw AppError.unknownError(message: "Failed to serialize database")- }- let data = Data(bytes: serialized, count: Int(size))- sqlite3_free(serialized)- return data- }-- private static let sqliteTransient = unsafeBitCast(-1, to: sqlite3_destructor_type.self)-- private static func bind(_ statement: OpaquePointer, params: [Any]) {- for (index, param) in params.enumerated() {- let position = Int32(index + 1)- switch param {- case is NSNull:- sqlite3_bind_null(statement, position)- case let value as Bool:- sqlite3_bind_int64(statement, position, value ? 1 : 0)- case let value as Int:- sqlite3_bind_int64(statement, position, Int64(value))- case let value as Int64:- sqlite3_bind_int64(statement, position, value)- case let value as Double:- sqlite3_bind_double(statement, position, value)- case let value as String:- sqlite3_bind_text(statement, position, value, -1, sqliteTransient)- case let value as [String: Any]:- if let base64 = value["__b64"] as? String, let bytes = Data(base64Encoded: base64) {- bytes.withUnsafeBytes { pointer in- _ = sqlite3_bind_blob(statement, position, pointer.baseAddress, Int32(bytes.count), sqliteTransient)- }- } else {- sqlite3_bind_text(statement, position, (try? serializeJson(value)) ?? "{}", -1, sqliteTransient)- }- case let value as NSNumber:- if CFNumberIsFloatType(value) {- sqlite3_bind_double(statement, position, value.doubleValue)- } else {- sqlite3_bind_int64(statement, position, value.int64Value)- }- default:- sqlite3_bind_text(statement, position, "\(param)", -1, sqliteTransient)- }- }- }-- private static func query(_ db: OpaquePointer, sql: String, params: [Any]) throws -> [[String: Any]] {- var statement: OpaquePointer?- guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK, let prepared = statement else {- throw AppError.unknownError(message: "Failed to prepare query: \(String(cString: sqlite3_errmsg(db))) (\(sql))")- }- defer { sqlite3_finalize(prepared) }- bind(prepared, params: params)-- let columnCount = sqlite3_column_count(prepared)- let columns = (0..<columnCount).map { String(cString: sqlite3_column_name(prepared, $0)) }- var rows: [[String: Any]] = []- while true {- let step = sqlite3_step(prepared)- if step == SQLITE_DONE {- break- }- guard step == SQLITE_ROW else {- throw AppError.unknownError(message: "Query failed: \(String(cString: sqlite3_errmsg(db))) (\(sql))")- }- var row: [String: Any] = [:]- for index in 0..<columnCount {- switch sqlite3_column_type(prepared, index) {- case SQLITE_INTEGER:- row[columns[Int(index)]] = sqlite3_column_int64(prepared, index)- case SQLITE_FLOAT:- row[columns[Int(index)]] = sqlite3_column_double(prepared, index)- case SQLITE_TEXT:- row[columns[Int(index)]] = sqlite3_column_text(prepared, index).map { String(cString: $0) } ?? ""- case SQLITE_BLOB:- let size = Int(sqlite3_column_bytes(prepared, index))- let bytes = sqlite3_column_blob(prepared, index).map { Data(bytes: $0, count: size) } ?? Data()- row[columns[Int(index)]] = ["__b64": bytes.base64EncodedString()]- default:- row[columns[Int(index)]] = NSNull()- }- }- rows.append(row)- }- return rows- }-- private static func exec(_ db: OpaquePointer, statements: [[String: Any]]) throws {- try execute(db, "BEGIN")- do {- for entry in statements {- let sql = entry["sql"] as? String ?? ""- var statement: OpaquePointer?- guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK, let prepared = statement else {- throw AppError.unknownError(message: "Failed to prepare statement: \(String(cString: sqlite3_errmsg(db))) (\(sql))")- }- defer { sqlite3_finalize(prepared) }- bind(prepared, params: entry["params"] as? [Any] ?? [])- guard sqlite3_step(prepared) == SQLITE_DONE else {- throw AppError.unknownError(message: "Statement failed: \(String(cString: sqlite3_errmsg(db))) (\(sql))")- }- }- try execute(db, "COMMIT")- } catch {- try? execute(db, "ROLLBACK")- throw error- }- }-// MARK: - JSONprivate static func parseJson(_ json: String) throws -> [String: Any] {@@ -420,6 +293,13 @@ public final class VaultSyncEngine {return object}+ private static func parseJsonArray(_ json: String) throws -> [Any] {+ guard let data = json.data(using: .utf8), let array = try JSONSerialization.jsonObject(with: data) as? [Any] else {+ throw AppError.parseError(message: "Engine rows are not a JSON array")+ }+ return array+ }+private static func serializeJson(_ object: Any) throws -> String {let data = try JSONSerialization.data(withJSONObject: object, options: [.fragmentsAllowed])guard let json = String(data: data, encoding: .utf8) else {diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swiftindex 7aca71084..75cc688e9 100644--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift@@ -1,5 +1,5 @@import Foundation-import SQLite+import RustCoreFrameworkimport VaultUtils/// Extension for the VaultStore class to handle database management@@ -74,55 +74,22 @@ extension VaultStore {decryptedDbData = decoded}- // Step 2: Clean up any existing connection+ // Step 2: Open the bytes in the Rust core's memory directly without persisting to the filesystem.self.dbConnection = nil-- // Step 3: Write decrypted data to temp file- let tempDbPath = FileManager.default.temporaryDirectory.appendingPathComponent("temp_db.sqlite")+ let opened: SqliteMemoryDatabasedo {- try decryptedDbData.write(to: tempDbPath)+ opened = try SqliteMemoryDatabase.fromBytes(bytes: decryptedDbData)+ _ = try opened.queryValues(sql: "SELECT count(*) FROM sqlite_master", params: [])} catch {- throw AppError.databaseTempWriteFailed- }-- // Step 4: Open source database from temp file- let sourceConnection: Connection- do {- sourceConnection = try Connection(tempDbPath.path)- } catch {- try? FileManager.default.removeItem(at: tempDbPath)throw AppError.databaseOpenFailed}- // Step 5: Create in-memory database connection- do {- self.dbConnection = try Connection(":memory:")- } catch {- try? FileManager.default.removeItem(at: tempDbPath)- throw AppError.databaseMemoryFailed- }-- // Step 6: Use SQLite backup API to copy entire database with full schema preservation- // This preserves foreign keys, indexes, triggers, views, and all other schema objects- do {- let backup = try sourceConnection.backup(usingConnection: self.dbConnection!)- try backup.step()- backup.finish()- } catch {- try? FileManager.default.removeItem(at: tempDbPath)- throw AppError.databaseBackupFailed- }-- // Clean up temp file- try? FileManager.default.removeItem(at: tempDbPath)-- // Step 7: Set pragmas+ // Step 3: Set pragmasdo {- try self.dbConnection?.execute("PRAGMA journal_mode = WAL")- try self.dbConnection?.execute("PRAGMA synchronous = NORMAL")- try self.dbConnection?.execute("PRAGMA foreign_keys = ON")+ try opened.executeBatch(sql: "PRAGMA foreign_keys = ON")} catch {throw AppError.databasePragmaFailed}+ self.dbConnection = opened}}diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+DatabaseClient.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+DatabaseClient.swiftindex b1bfbd285..6509cc1d0 100644--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+DatabaseClient.swift+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+DatabaseClient.swift@@ -1,5 +1,4 @@import Foundation-import SQLite/// Extension to make VaultStore conform to DatabaseClient protocol./// This allows VaultStore to be used with the repository pattern.diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swiftindex 19a58f944..0ab79583a 100644--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swift+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swift@@ -1,5 +1,4 @@import Foundation-import SQLiteimport VaultModelsimport VaultUtilsdiff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Query.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Query.swiftindex 71d1ec4b7..52e047060 100644--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Query.swift+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Query.swift@@ -1,5 +1,5 @@import Foundation-import SQLite+import RustCoreFrameworkimport VaultModelsimport VaultUtils@@ -7,188 +7,61 @@ import VaultUtilsextension VaultStore {// MARK: - Core Database Operations (DatabaseClient Protocol)+ /// Prefix repositories put in front of base64 text to bind it as a BLOB.+ private static let blobParamPrefix = "av-base64-to-blob:"+/// Execute a SELECT query on the database- public func executeQuery(_ query: String, params: [Binding?]) throws -> [[String: Any]] {+ public func executeQuery(_ query: String, params: [SqliteBindValue]) throws -> [[String: Any]] {return try executeQuery(query, params: params, blobPrefix: "")}/// Execute a SELECT query on the database, returning BLOB columns as base64 behind `blobPrefix`.- public func executeQuery(_ query: String, params: [Binding?], blobPrefix: String) throws -> [[String: Any]] {- guard let dbConnection = self.dbConnection else {- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])- }-- var params = params- for (index, param) in params.enumerated() {- if let base64String = param as? String {- if base64String.hasPrefix("av-base64-to-blob:") {- let base64 = String(base64String.dropFirst("av-base64-to-blob:".count))- if let data = Data(base64Encoded: base64) {- params[index] = Blob(bytes: [UInt8](data))- }- }- }- }-- let statement = try dbConnection.prepare(query)- var results: [[String: Any]] = []-- for row in try statement.run(params) {+ public func executeQuery(_ query: String, params: [SqliteBindValue], blobPrefix: String) throws -> [[String: Any]] {+ let result = try requireDatabase().queryValues(sql: query, params: params.map(Self.toSqlValue))+ return result.rows.map { row invar rowDict: [String: Any] = [:]- for (index, column) in statement.columnNames.enumerated() {- let value = row[index]- switch value {- case let data as SQLite.Blob:- let binaryData = Data(data.bytes)- rowDict[column] = blobPrefix + binaryData.base64EncodedString()- case let number as Int64:- rowDict[column] = number- case let number as Double:- rowDict[column] = number- case let text as String:- rowDict[column] = text- case .none:- rowDict[column] = NSNull()- default:- rowDict[column] = value- }+ for (index, column) in result.columns.enumerated() {+ rowDict[column] = Self.fromSqlValue(row[index], blobPrefix: blobPrefix)}- results.append(rowDict)+ return rowDict}-- return results}/// Execute an UPDATE, INSERT, or DELETE query on the database (which will modify the database).- public func executeUpdate(_ query: String, params: [Binding?]) throws -> Int {- guard let dbConnection = self.dbConnection else {- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])- }-- var params = params- for (index, param) in params.enumerated() {- if let base64String = param as? String {- if base64String.hasPrefix("av-base64-to-blob:") {- let base64 = String(base64String.dropFirst("av-base64-to-blob:".count))- if let data = Data(base64Encoded: base64) {- params[index] = Blob(bytes: [UInt8](data))- }- }- }- }-- let statement = try dbConnection.prepare(query)- try statement.run(params)- return dbConnection.changes+ public func executeUpdate(_ query: String, params: [SqliteBindValue]) throws -> Int {+ return Int(try requireDatabase().execute(sql: query, params: params.map(Self.toSqlValue)))}- /// Execute a raw SQL command on the database without parameters (for DDL operations like CREATE TABLE).- ///- /// Note: Migration SQL scripts handle their own transactions and PRAGMA statements.- /// PRAGMA foreign_keys statements MUST be executed outside of transactions to take effect.+ /// Execute a raw SQL script (one or more statements) without parameters.+ /// Migration SQL scripts handle their own transactions and PRAGMA statements.public func executeRaw(_ query: String) throws {- guard let dbConnection = self.dbConnection else {- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])- }-- // Split the query by semicolons to handle multiple statements- let statements = query.components(separatedBy: ";")-- for statement in statements {- let trimmedStatement = statement.smartTrim()-- // Skip empty statements and SQL comments- if trimmedStatement.isEmpty || trimmedStatement.hasPrefix("--") {- continue- }-- // Execute all statements including PRAGMA and transaction control- // This allows migration SQL to properly control its own transactions and PRAGMA settings- try dbConnection.execute(trimmedStatement)- }+ try requireDatabase().executeBatch(sql: query)}/// Begin a transaction on the database. This is required for all database operations that modify the database.public func beginTransaction() throws {- guard let dbConnection = self.dbConnection else {- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])- }- try dbConnection.execute("BEGIN TRANSACTION")+ try requireDatabase().executeBatch(sql: "BEGIN TRANSACTION")}- /// Persist the in-memory database to encrypted local storage using VACUUM INTO.- /// Produces a fully faithful, compact copy (schema + data), unlike CTAS copies.+ /// Persist the in-memory database to encrypted local storage.public func persistDatabaseToEncryptedStorage() throws {- guard let dbConnection = self.dbConnection else {- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])- }-- // Make sure we are not inside an explicit transaction; VACUUM INTO must run outside.- // If you have your own transaction management, ensure it's committed before calling this.- // Optional: give SQLite time to resolve locks- try? dbConnection.execute("PRAGMA busy_timeout=5000")-- // End any lingering transaction (no-op if none).- _ = try? dbConnection.execute("END")-- // Prepare a fresh temp file path for VACUUM INTO; it must NOT already exist.- let tempDbURL = FileManager.default.temporaryDirectory.appendingPathComponent("temp_db.sqlite")- if FileManager.default.fileExists(atPath: tempDbURL.path) {- try FileManager.default.removeItem(at: tempDbURL)- }-- // Quote the target path safely for SQL (VACUUM INTO does not accept parameters in some builds).- // Escape single quotes per SQL rules.- let quotedPath = "'" + tempDbURL.path.replacingOccurrences(of: "'", with: "''") + "'"-- // Run VACUUM INTO to create a compact, faithful copy of the current DB.- // Must be executed with no active transaction and no attached target needed.- // This preserves schema, indexes, triggers, views, pragmas like page_size, auto_vacuum, encoding, user_version, etc.- // Retry VACUUM INTO a few times if we hit "statements in progress"- var lastError: Error?- for attempt in 1...5 {- do {- try dbConnection.execute("VACUUM INTO \(quotedPath)")- lastError = nil- break- } catch {- lastError = error- let msg = String(describing: error).lowercased()- if msg.contains("statements in progress") || msg.contains("locked") {- Thread.sleep(forTimeInterval: 0.15 * Double(attempt)) // backoff- continue- } else {- break- }- }- }- if let err = lastError {- print("❌ VACUUM INTO failed after retries:", err)- throw NSError(domain: "VaultStore", code: 6,- userInfo: [NSLocalizedDescriptionKey:- "VACUUM INTO failed: \(err.localizedDescription)"])- }-- // Read -> encrypt -> store the compact copy- let rawData = try Data(contentsOf: tempDbURL)- let base64String = rawData.base64EncodedString()- let encryptedBase64Data = try encrypt(data: Data(base64String.utf8))- let encryptedBase64String = encryptedBase64Data.base64EncodedString()- try storeEncryptedDatabase(encryptedBase64String)+ let database = try requireDatabase()+ // End any open transactions.+ _ = try? database.executeBatch(sql: "END")+ let encrypted = try encrypt(data: try exportDatabase())+ try storeEncryptedDatabase(encrypted.base64EncodedString())+ }- // Clean up temp file- try? FileManager.default.removeItem(at: tempDbURL)+ /// The database as SQLite file bytes, compacted first when no transaction is open.+ public func exportDatabase() throws -> Data {+ let database = try requireDatabase()+ _ = try? database.executeBatch(sql: "VACUUM")+ return try database.export()}/// Commit a transaction on the database. This is required for all database operations that modify the database.- /// Committing a transaction will also trigger a persist from the in-memory database to the encrypted database file.- /// It also atomically marks the vault as dirty and increments the mutation sequence for proper sync tracking.public func commitTransaction() throws {- guard let dbConnection = self.dbConnection else {- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])- }-- try dbConnection.execute("COMMIT")+ try requireDatabase().executeBatch(sql: "COMMIT")try persistDatabaseToEncryptedStorage()// Atomically mark vault as dirty and increment mutation sequence@@ -199,15 +72,10 @@ extension VaultStore {/// Rollback a transaction on the database on error.public func rollbackTransaction() throws {- guard let dbConnection = self.dbConnection else {- throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])- }- try dbConnection.execute("ROLLBACK")+ try requireDatabase().executeBatch(sql: "ROLLBACK")}/// Persist the in-memory database to encrypted storage and mark as dirty.- /// Used after migrations where SQL handles its own transactions but we need to persist and sync.- /// This does NOT commit any SQL transaction - it just persists the current state of the database.public func persistAndMarkDirty() throws {try persistDatabaseToEncryptedStorage()@@ -217,6 +85,55 @@ extension VaultStore {_ = incrementMutationSequence()}+ /// The open database, or the error every query path reports while the vault is locked.+ internal func requireDatabase() throws -> SqliteMemoryDatabase {+ guard let dbConnection = self.dbConnection else {+ throw NSError(domain: "VaultStore", code: 4, userInfo: [NSLocalizedDescriptionKey: "Database not initialized"])+ }+ return dbConnection+ }++ private static func toSqlValue(_ param: SqliteBindValue) -> SqlValue {+ switch param {+ case nil:+ return .null+ case let value as SqlValue:+ return value+ case let value as Data:+ return .blob(value)+ case let value as String:+ if value.hasPrefix(blobParamPrefix), let bytes = Data(base64Encoded: String(value.dropFirst(blobParamPrefix.count))) {+ return .blob(bytes)+ }+ return .text(value)+ case let value as Bool:+ return .integer(value ? 1 : 0)+ case let value as Int:+ return .integer(Int64(value))+ case let value as Int64:+ return .integer(value)+ case let value as Double:+ return .real(value)+ default:+ return .text(String(describing: param!))+ }+ }++ private static func fromSqlValue(_ value: SqlValue, blobPrefix: String) -> Any {+ switch value {+ case .null:+ return NSNull()+ case .integer(let number):+ return number+ case .real(let number):+ return number+ case .text(let text):+ return text+ case .blob(let bytes):+ return blobPrefix + bytes.base64EncodedString()+ }+ }+// MARK: - Items (Using Repository Pattern)/// Get all items from the database using the new field-based model.diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore.swiftindex 61310e066..0e89486af 100644--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift@@ -1,5 +1,6 @@import Foundation-import SQLite+import RustCoreFramework+import UIKitimport LocalAuthenticationimport CryptoKitimport CommonCrypto@@ -28,7 +29,8 @@ public class VaultStore {internal var autoLockTimeout: Int = VaultConstants.defaultAutoLockTimeout/// The database connection for the decrypted in-memory vault.- internal var dbConnection: Connection?+ /// The live vault, held in the Rust core's memory. Nil while vault is locked.+ internal var dbConnection: SqliteMemoryDatabase?/// The encryption key for the vault.internal var encryptionKey: Data?