extracting base64 in order to provide the android version

This commit is contained in:
Adam Brown 2022-03-17 19:06:53 +00:00
parent fd4566c234
commit 7a025b9d9c
4 changed files with 45 additions and 20 deletions

View File

@ -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)
}
}

View File

@ -0,0 +1,6 @@
package app.dapk.st.core
interface Base64 {
fun encode(input: ByteArray): String
fun decode(input: String): ByteArray
}

View File

@ -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 : Serializable> T.serialize(): String {
private fun <T : Serializable> T.serialize(): String {
val baos = ByteArrayOutputStream()
ObjectOutputStream(baos).use {
it.writeObject(this)
}
return Base64.getEncoder().encode(baos.toByteArray()).toString(Charsets.UTF_8)
}
return base64.encode(baos.toByteArray())
}
@Suppress("UNCHECKED_CAST")
private fun <T : Serializable> String.deserialize(): T {
val decoded = Base64.getDecoder().decode(this)
@Suppress("UNCHECKED_CAST")
private fun <T : Serializable> String.deserialize(): T {
val decoded = base64.decode(this)
val baos = ByteArrayInputStream(decoded)
return ObjectInputStream(baos).use {
it.readObject() as T
}
}
}

View File

@ -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)
}
}