diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/RealmCryptoStore.kt index 7f4d4c0c0a..7fa7c47a1e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/RealmCryptoStore.kt @@ -313,6 +313,25 @@ internal class RealmCryptoStore(private val realmConfiguration: RealmConfigurati signingInfo.setSelfSignedKey(keyEntity) } + // Only for me + if (userSigningKey != null) { + val existingUSK = signingInfo.getUserSigningKey() + if (existingUSK != null && existingUSK.publicKeyBase64 == userSigningKey.unpaddedBase64PublicKey) { + // update signatures? + existingUSK.putSignatures(userSigningKey.signatures) + existingUSK.usages = userSigningKey.usages?.toTypedArray()?.let { RealmList(*it) } + ?: RealmList() + } else { + val keyEntity = realm.createObject(KeyInfoEntity::class.java).apply { + this.publicKeyBase64 = userSigningKey.unpaddedBase64PublicKey + this.usages = userSigningKey.usages?.toTypedArray()?.let { RealmList(*it) } + ?: RealmList() + this.putSignatures(userSigningKey.signatures) + } + signingInfo.setUserSignedKey(keyEntity) + } + } + userEntity.crossSigningInfoEntity = signingInfo } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/model/CrossSigningInfoEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/model/CrossSigningInfoEntity.kt index f07085003f..00657d3558 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/model/CrossSigningInfoEntity.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/store/db/model/CrossSigningInfoEntity.kt @@ -46,4 +46,14 @@ internal open class CrossSigningInfoEntity( .forEach { crossSigningKeys.remove(it) } info?.let { crossSigningKeys.add(it) } } + + fun getUserSigningKey() = crossSigningKeys.firstOrNull { it.usages.contains(KeyUsage.USER_SIGNING.value) } + + fun setUserSignedKey(info: KeyInfoEntity?) { + crossSigningKeys + .filter { it.usages.contains(KeyUsage.USER_SIGNING.value) } + .forEach { crossSigningKeys.remove(it) } + info?.let { crossSigningKeys.add(it) } + } + }