diff --git a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt index dd4f8a4..7f7bc4d 100644 --- a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt +++ b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt @@ -7,10 +7,7 @@ import android.content.Intent import app.dapk.db.DapkDb import app.dapk.st.BuildConfig import app.dapk.st.SharedPreferencesDelegate -import app.dapk.st.core.BuildMeta -import app.dapk.st.core.CoreAndroidModule -import app.dapk.st.core.CoroutineDispatchers -import app.dapk.st.core.SingletonFlows +import app.dapk.st.core.* import app.dapk.st.core.extensions.ErrorTracker import app.dapk.st.core.extensions.unsafeLazy import app.dapk.st.directory.DirectoryModule @@ -208,7 +205,7 @@ internal class MatrixModules( installAuthService(credentialsStore) installEncryptionService(store.knownDevicesStore()) - val olmAccountStore = OlmPersistenceWrapper(store.olmStore()) + val olmAccountStore = OlmPersistenceWrapper(store.olmStore(), AndroidBase64()) val singletonFlows = SingletonFlows(coroutineDispatchers) val olm = OlmWrapper( olmStore = olmAccountStore, @@ -417,3 +414,13 @@ class TaskRunnerAdapter(private val matrixTaskRunner: suspend (MatrixTask) -> Ma } } } + +class AndroidBase64 : Base64 { + override fun encode(input: ByteArray): String { + return android.util.Base64.encodeToString(input, android.util.Base64.DEFAULT) + } + + override fun decode(input: String): ByteArray { + return android.util.Base64.decode(input, android.util.Base64.DEFAULT) + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/app/dapk/st/core/Base64.kt b/core/src/main/kotlin/app/dapk/st/core/Base64.kt new file mode 100644 index 0000000..3ad71f3 --- /dev/null +++ b/core/src/main/kotlin/app/dapk/st/core/Base64.kt @@ -0,0 +1,6 @@ +package app.dapk.st.core + +interface Base64 { + fun encode(input: ByteArray): String + fun decode(input: String): ByteArray +} \ No newline at end of file diff --git a/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmPersistenceWrapper.kt b/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmPersistenceWrapper.kt index 36869c1..c924186 100644 --- a/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmPersistenceWrapper.kt +++ b/domains/olm/src/main/kotlin/app/dapk/st/olm/OlmPersistenceWrapper.kt @@ -1,5 +1,6 @@ package app.dapk.st.olm +import app.dapk.st.core.Base64 import app.dapk.st.domain.OlmPersistence import app.dapk.st.domain.SerializedObject import app.dapk.st.matrix.common.Curve25519 @@ -10,10 +11,10 @@ import org.matrix.olm.OlmInboundGroupSession import org.matrix.olm.OlmOutboundGroupSession import org.matrix.olm.OlmSession import java.io.* -import java.util.* class OlmPersistenceWrapper( private val olmPersistence: OlmPersistence, + private val base64: Base64, ) : OlmStore { override suspend fun read(): OlmAccount? { @@ -49,21 +50,21 @@ class OlmPersistenceWrapper( override suspend fun readInbound(sessionId: SessionId): OlmInboundGroupSession? { return olmPersistence.readInbound(sessionId)?.value?.deserialize() } -} -private fun T.serialize(): String { - val baos = ByteArrayOutputStream() - ObjectOutputStream(baos).use { - it.writeObject(this) + private fun T.serialize(): String { + val baos = ByteArrayOutputStream() + ObjectOutputStream(baos).use { + it.writeObject(this) + } + return base64.encode(baos.toByteArray()) } - return Base64.getEncoder().encode(baos.toByteArray()).toString(Charsets.UTF_8) -} -@Suppress("UNCHECKED_CAST") -private fun String.deserialize(): T { - val decoded = Base64.getDecoder().decode(this) - val baos = ByteArrayInputStream(decoded) - return ObjectInputStream(baos).use { - it.readObject() as T + @Suppress("UNCHECKED_CAST") + private fun String.deserialize(): T { + val decoded = base64.decode(this) + val baos = ByteArrayInputStream(decoded) + return ObjectInputStream(baos).use { + it.readObject() as T + } } } diff --git a/test-harness/src/test/kotlin/test/TestMatrix.kt b/test-harness/src/test/kotlin/test/TestMatrix.kt index c5de509..d38e91f 100644 --- a/test-harness/src/test/kotlin/test/TestMatrix.kt +++ b/test-harness/src/test/kotlin/test/TestMatrix.kt @@ -1,6 +1,7 @@ package test import TestUser +import app.dapk.st.core.Base64 import app.dapk.st.core.CoroutineDispatchers import app.dapk.st.core.SingletonFlows import app.dapk.st.domain.StoreModule @@ -81,7 +82,7 @@ class TestMatrix( installAuthService(storeModule.credentialsStore(), AuthConfig(forceHttp = false)) installEncryptionService(storeModule.knownDevicesStore()) - val olmAccountStore = OlmPersistenceWrapper(storeModule.olmStore()) + val olmAccountStore = OlmPersistenceWrapper(storeModule.olmStore(), JavaBase64()) val olm = OlmWrapper( olmStore = olmAccountStore, singletonFlows = SingletonFlows(coroutineDispatchers), @@ -271,3 +272,13 @@ class TestMatrix( suspend fun deviceId() = storeModule.credentialsStore().credentials()!!.deviceId suspend fun userId() = storeModule.credentialsStore().credentials()!!.userId } + +class JavaBase64 : Base64 { + override fun encode(input: ByteArray): String { + return java.util.Base64.getEncoder().encode(input).toString(Charsets.UTF_8) + } + + override fun decode(input: String): ByteArray { + return java.util.Base64.getDecoder().decode(input) + } +} \ No newline at end of file