mirror of
https://github.com/ouchadam/small-talk.git
synced 2025-02-17 20:50:48 +01:00
extracting base64 in order to provide the android version
This commit is contained in:
parent
fd4566c234
commit
7a025b9d9c
@ -7,10 +7,7 @@ import android.content.Intent
|
|||||||
import app.dapk.db.DapkDb
|
import app.dapk.db.DapkDb
|
||||||
import app.dapk.st.BuildConfig
|
import app.dapk.st.BuildConfig
|
||||||
import app.dapk.st.SharedPreferencesDelegate
|
import app.dapk.st.SharedPreferencesDelegate
|
||||||
import app.dapk.st.core.BuildMeta
|
import app.dapk.st.core.*
|
||||||
import app.dapk.st.core.CoreAndroidModule
|
|
||||||
import app.dapk.st.core.CoroutineDispatchers
|
|
||||||
import app.dapk.st.core.SingletonFlows
|
|
||||||
import app.dapk.st.core.extensions.ErrorTracker
|
import app.dapk.st.core.extensions.ErrorTracker
|
||||||
import app.dapk.st.core.extensions.unsafeLazy
|
import app.dapk.st.core.extensions.unsafeLazy
|
||||||
import app.dapk.st.directory.DirectoryModule
|
import app.dapk.st.directory.DirectoryModule
|
||||||
@ -208,7 +205,7 @@ internal class MatrixModules(
|
|||||||
installAuthService(credentialsStore)
|
installAuthService(credentialsStore)
|
||||||
installEncryptionService(store.knownDevicesStore())
|
installEncryptionService(store.knownDevicesStore())
|
||||||
|
|
||||||
val olmAccountStore = OlmPersistenceWrapper(store.olmStore())
|
val olmAccountStore = OlmPersistenceWrapper(store.olmStore(), AndroidBase64())
|
||||||
val singletonFlows = SingletonFlows(coroutineDispatchers)
|
val singletonFlows = SingletonFlows(coroutineDispatchers)
|
||||||
val olm = OlmWrapper(
|
val olm = OlmWrapper(
|
||||||
olmStore = olmAccountStore,
|
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)
|
||||||
|
}
|
||||||
|
}
|
6
core/src/main/kotlin/app/dapk/st/core/Base64.kt
Normal file
6
core/src/main/kotlin/app/dapk/st/core/Base64.kt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package app.dapk.st.core
|
||||||
|
|
||||||
|
interface Base64 {
|
||||||
|
fun encode(input: ByteArray): String
|
||||||
|
fun decode(input: String): ByteArray
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package app.dapk.st.olm
|
package app.dapk.st.olm
|
||||||
|
|
||||||
|
import app.dapk.st.core.Base64
|
||||||
import app.dapk.st.domain.OlmPersistence
|
import app.dapk.st.domain.OlmPersistence
|
||||||
import app.dapk.st.domain.SerializedObject
|
import app.dapk.st.domain.SerializedObject
|
||||||
import app.dapk.st.matrix.common.Curve25519
|
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.OlmOutboundGroupSession
|
||||||
import org.matrix.olm.OlmSession
|
import org.matrix.olm.OlmSession
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class OlmPersistenceWrapper(
|
class OlmPersistenceWrapper(
|
||||||
private val olmPersistence: OlmPersistence,
|
private val olmPersistence: OlmPersistence,
|
||||||
|
private val base64: Base64,
|
||||||
) : OlmStore {
|
) : OlmStore {
|
||||||
|
|
||||||
override suspend fun read(): OlmAccount? {
|
override suspend fun read(): OlmAccount? {
|
||||||
@ -49,21 +50,21 @@ class OlmPersistenceWrapper(
|
|||||||
override suspend fun readInbound(sessionId: SessionId): OlmInboundGroupSession? {
|
override suspend fun readInbound(sessionId: SessionId): OlmInboundGroupSession? {
|
||||||
return olmPersistence.readInbound(sessionId)?.value?.deserialize()
|
return olmPersistence.readInbound(sessionId)?.value?.deserialize()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun <T : Serializable> T.serialize(): String {
|
private fun <T : Serializable> T.serialize(): String {
|
||||||
val baos = ByteArrayOutputStream()
|
val baos = ByteArrayOutputStream()
|
||||||
ObjectOutputStream(baos).use {
|
ObjectOutputStream(baos).use {
|
||||||
it.writeObject(this)
|
it.writeObject(this)
|
||||||
|
}
|
||||||
|
return base64.encode(baos.toByteArray())
|
||||||
}
|
}
|
||||||
return Base64.getEncoder().encode(baos.toByteArray()).toString(Charsets.UTF_8)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
private fun <T : Serializable> String.deserialize(): T {
|
private fun <T : Serializable> String.deserialize(): T {
|
||||||
val decoded = Base64.getDecoder().decode(this)
|
val decoded = base64.decode(this)
|
||||||
val baos = ByteArrayInputStream(decoded)
|
val baos = ByteArrayInputStream(decoded)
|
||||||
return ObjectInputStream(baos).use {
|
return ObjectInputStream(baos).use {
|
||||||
it.readObject() as T
|
it.readObject() as T
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import TestUser
|
import TestUser
|
||||||
|
import app.dapk.st.core.Base64
|
||||||
import app.dapk.st.core.CoroutineDispatchers
|
import app.dapk.st.core.CoroutineDispatchers
|
||||||
import app.dapk.st.core.SingletonFlows
|
import app.dapk.st.core.SingletonFlows
|
||||||
import app.dapk.st.domain.StoreModule
|
import app.dapk.st.domain.StoreModule
|
||||||
@ -81,7 +82,7 @@ class TestMatrix(
|
|||||||
installAuthService(storeModule.credentialsStore(), AuthConfig(forceHttp = false))
|
installAuthService(storeModule.credentialsStore(), AuthConfig(forceHttp = false))
|
||||||
installEncryptionService(storeModule.knownDevicesStore())
|
installEncryptionService(storeModule.knownDevicesStore())
|
||||||
|
|
||||||
val olmAccountStore = OlmPersistenceWrapper(storeModule.olmStore())
|
val olmAccountStore = OlmPersistenceWrapper(storeModule.olmStore(), JavaBase64())
|
||||||
val olm = OlmWrapper(
|
val olm = OlmWrapper(
|
||||||
olmStore = olmAccountStore,
|
olmStore = olmAccountStore,
|
||||||
singletonFlows = SingletonFlows(coroutineDispatchers),
|
singletonFlows = SingletonFlows(coroutineDispatchers),
|
||||||
@ -271,3 +272,13 @@ class TestMatrix(
|
|||||||
suspend fun deviceId() = storeModule.credentialsStore().credentials()!!.deviceId
|
suspend fun deviceId() = storeModule.credentialsStore().credentials()!!.deviceId
|
||||||
suspend fun userId() = storeModule.credentialsStore().credentials()!!.userId
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user