From b37eca45208fba32bf440e21af06e20bb17350f9 Mon Sep 17 00:00:00 2001 From: valere Date: Fri, 26 May 2023 18:22:47 +0200 Subject: [PATCH] fix rust crypto still need CryptoMetadataEntity for global pref --- .../internal/crypto/store/RustCryptoStore.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/matrix-sdk-android/src/rustCrypto/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt b/matrix-sdk-android/src/rustCrypto/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt index 9f3bd8d949..b242a3ed34 100644 --- a/matrix-sdk-android/src/rustCrypto/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt +++ b/matrix-sdk-android/src/rustCrypto/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt @@ -57,6 +57,8 @@ import org.matrix.android.sdk.internal.crypto.store.db.model.createPrimaryKey import org.matrix.android.sdk.internal.crypto.store.db.query.getById import org.matrix.android.sdk.internal.crypto.store.db.query.getOrCreate import org.matrix.android.sdk.internal.di.CryptoDatabase +import org.matrix.android.sdk.internal.di.DeviceId +import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.android.sdk.internal.util.time.Clock import timber.log.Timber @@ -74,11 +76,45 @@ private val loggerTag = LoggerTag("RealmCryptoStore", LoggerTag.CRYPTO) internal class RustCryptoStore @Inject constructor( @CryptoDatabase private val realmConfiguration: RealmConfiguration, private val clock: Clock, + @UserId private val userId: String, + @DeviceId private val deviceId: String, private val myDeviceLastSeenInfoEntityMapper: MyDeviceLastSeenInfoEntityMapper, private val olmMachine: dagger.Lazy, private val matrixCoroutineDispatchers: MatrixCoroutineDispatchers, ) : IMXCommonCryptoStore { + // still needed on rust due to the global crypto settings + init { + // Ensure CryptoMetadataEntity is inserted in DB + doRealmTransaction("init", realmConfiguration) { realm -> + var currentMetadata = realm.where().findFirst() + + var deleteAll = false + + if (currentMetadata != null) { + // Check credentials + // The device id may not have been provided in credentials. + // Check it only if provided, else trust the stored one. + if (currentMetadata.userId != userId || deviceId != currentMetadata.deviceId) { + Timber.w("## open() : Credentials do not match, close this store and delete data") + deleteAll = true + currentMetadata = null + } + } + + if (currentMetadata == null) { + if (deleteAll) { + realm.deleteAll() + } + + // Metadata not found, or database cleaned, create it + realm.createObject(CryptoMetadataEntity::class.java, userId).apply { + deviceId = this@RustCryptoStore.deviceId + } + } + } + } + /** * Retrieve a device by its identity key. *