Skip to content

Commit

Permalink
bug fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Nov 2, 2024
1 parent 20e8108 commit 74fa76d
Show file tree
Hide file tree
Showing 25 changed files with 307 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class API(
val configFlow: Flow<ConfigEntity>
get() = configRepository.stream


suspend fun tonapiFetch(
url: String,
options: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class AccountRepository(
private val migrationHelper = RNMigrationHelper(rnLegacy)

private val _selectedStateFlow = MutableStateFlow<SelectedState>(SelectedState.Initialization)
val selectedStateFlow = _selectedStateFlow.stateIn(scope, SharingStarted.Eagerly,
val selectedStateFlow = _selectedStateFlow.stateIn(
scope,
SharingStarted.Eagerly,
SelectedState.Initialization
)
val selectedWalletFlow = selectedStateFlow.filterNotNull().filterIsInstance<SelectedState.Wallet>().map {
Expand Down Expand Up @@ -112,15 +114,13 @@ class AccountRepository(

private suspend fun migrationFromRN() = withContext(Dispatchers.IO) {
val (selectedId, wallets) = migrationHelper.loadLegacy()
if (wallets.isEmpty()) {
_selectedStateFlow.value = SelectedState.Empty
} else {
if (wallets.isNotEmpty()) {
database.insertAccounts(wallets)
for (wallet in wallets) {
val token = rnLegacy.getTonProof(wallet.id) ?: continue
storageSource.setTonProofToken(wallet.publicKey, token)
}
setSelectedWallet(selectedId)
storageSource.setSelectedId(selectedId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.tonapps.wallet.data.account.source
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.tonapps.blockchain.ton.extensions.getPrivateKey
import com.tonapps.blockchain.ton.extensions.hex
import com.tonapps.extensions.getByteArray
import com.tonapps.extensions.putByteArray
Expand Down Expand Up @@ -47,10 +48,7 @@ internal class VaultSource(context: Context) {
}

fun getPrivateKey(publicKey: PublicKeyEd25519): PrivateKeyEd25519? {
val seed = prefs.getByteArray(privateKey(publicKey)) ?: return null
val privateKey = PrivateKeyEd25519(seed)
seed.clear()
return privateKey
return prefs.getPrivateKey(privateKey(publicKey))
}

private fun privateKey(publicKey: PublicKeyEd25519) = key(PRIVATE_KEY_PREFIX, publicKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BackupRepository(
init {
scope.launch(Dispatchers.IO) {
if (rnLegacy.isRequestMigration()) {
localDataSource.clear()
// localDataSource.clear()
migrationFromRN()
}
_stream.value = localDataSource.getAllBackups()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.tonapps.wallet.data.core
import android.content.Context
import android.os.Parcelable
import android.util.Log
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.extensions.cacheFolder
import com.tonapps.extensions.file
import com.tonapps.extensions.toByteArray
Expand Down Expand Up @@ -47,6 +48,7 @@ abstract class BlobDataSource<D>(
val string = String(bytes)
fromJSON(string)
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
null
}
}
Expand All @@ -73,9 +75,13 @@ abstract class BlobDataSource<D>(
}

private fun setDiskCache(key: String, value: D) {
val file = diskFile(key)
val bytes = onMarshall(value)
file.writeBytes(bytes)
try {
val file = diskFile(key)
val bytes = onMarshall(value)
file.writeBytes(bytes)
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
}
}

private fun diskFile(key: String): File {
Expand All @@ -98,6 +104,7 @@ abstract class BlobDataSource<D>(
val bytes = file.readBytes()
if (bytes.isEmpty()) null else bytes
} catch (e: IOException) {
FirebaseCrashlytics.getInstance().recordException(e)
null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ data class WalletCurrency(
"GBP", // Great Britain Pound
"CHF", // Swiss Franc
"CNY", // China Yuan
"GEL", // Georgian Lari
"KRW", // South Korean Won
"IDR", // Indonesian Rupiah
"INR", // Indian Rupee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LockScreen(
}
}

fun biometric(result: BiometricPrompt.AuthenticationResult) {
fun biometric() {
hide()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.extensions.activity
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
Expand All @@ -28,6 +29,7 @@ object PasscodeBiometric {
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
// Yes. Biometric right now is only UI feature.
continuation.resume(true)
}
})
Expand All @@ -54,6 +56,7 @@ object PasscodeBiometric {

biometricPrompt.authenticate(builder.build())
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
callback.onAuthenticationError(BiometricPrompt.ERROR_HW_NOT_PRESENT, "Unknown error")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonapps.wallet.data.passcode

import android.content.Context
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.extensions.logError
import com.tonapps.wallet.data.account.AccountRepository
import com.tonapps.wallet.data.passcode.source.PasscodeStore
Expand Down Expand Up @@ -47,6 +48,7 @@ class PasscodeHelper(
store.setPinCode(code)
true
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
context.logError(e)
false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.tonapps.wallet.data.passcode

import android.content.Context
import android.util.Log
import androidx.biometric.BiometricPrompt
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.extensions.logError
import com.tonapps.wallet.data.account.AccountRepository
import com.tonapps.wallet.data.passcode.dialog.PasscodeDialog
Expand All @@ -10,11 +12,13 @@ import com.tonapps.wallet.data.settings.SettingsRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import uikit.navigation.Navigation
import java.util.concurrent.atomic.AtomicBoolean

class PasscodeManager(
private val accountRepository: AccountRepository,
Expand All @@ -30,16 +34,17 @@ class PasscodeManager(
get() = lockscreen.stateFlow

init {
scope.launch(Dispatchers.IO) {
settingsRepository.isMigratedFlow.onEach {
lockscreen.init()
}
}.launchIn(scope)
}

fun lockscreenBiometric(result: BiometricPrompt.AuthenticationResult) {
lockscreen.biometric(result)
fun lockscreenBiometric() {
lockscreen.biometric()
}

fun deleteAll() {
settingsRepository.biometric = false
scope.launch {
reset()
}
Expand Down Expand Up @@ -77,6 +82,7 @@ class PasscodeManager(
migration(context, code)
true
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
context.logError(e)
false
}
Expand All @@ -97,6 +103,7 @@ class PasscodeManager(
}
true
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
context.logError(e)
false
}
Expand All @@ -108,10 +115,33 @@ class PasscodeManager(
}

suspend fun reset() = withContext(Dispatchers.IO) {
settingsRepository.lockScreen = false
settingsRepository.biometric = false
helper.reset()
rnLegacy.clearMnemonic()
}

suspend fun confirmationByBiometric(
context: Context,
title: String
): Boolean = withContext(Dispatchers.Main) {
try {
if (isRequestMigration()) {
val passcode = rnLegacy.exportPasscodeWithBiometry()
if (passcode.isBlank()) {
throw Exception("failed to request passcode")
}
migration(context, passcode)
true
} else {
PasscodeBiometric.showPrompt(context, title)
}
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
false
}
}

suspend fun confirmation(
context: Context,
title: String
Expand Down Expand Up @@ -146,6 +176,7 @@ class PasscodeManager(
}
throw Exception("biometry is disabled")
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
return null
}
}
Expand All @@ -165,6 +196,7 @@ class PasscodeManager(
migration(context, passcode)
true
} catch (e: Throwable) {
FirebaseCrashlytics.getInstance().recordException(e)
context.logError(e)
false
}
Expand All @@ -175,10 +207,10 @@ class PasscodeManager(
code: String
) = withContext(Dispatchers.Main) {
val navigation = Navigation.from(context)
navigation?.toast("...", true, 0)
navigation?.migrationLoader(true)
accountRepository.importPrivateKeysFromRNLegacy(code)
save(code)
navigation?.toast("...", false, 0)
navigation?.migrationLoader(false)
}

fun confirmationFlow(context: Context, title: String) = flow {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tonapps.wallet.data.rn

import android.content.Context
import androidx.fragment.app.FragmentActivity
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.wallet.data.rn.data.RNVaultState
import com.tonapps.wallet.data.rn.expo.SecureStoreModule
import com.tonapps.wallet.data.rn.expo.SecureStoreOptions
Expand Down Expand Up @@ -66,7 +67,12 @@ internal class RNSeedStorage(context: Context) {
}

suspend fun hasPinCode(): Boolean {
return readState() != null
return try {
readState() != null
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
false
}
}

suspend fun removeAll() {
Expand All @@ -79,7 +85,7 @@ internal class RNSeedStorage(context: Context) {
}

suspend fun get(passcode: String): RNVaultState = withContext(Dispatchers.IO) {
val state = readState() ?: throw Exception("Seed state is null")
val state = readState() ?: return@withContext RNVaultState()
val json = JSONObject(ScryptBox.decrypt(passcode, state))
RNVaultState.of(json)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package com.tonapps.wallet.data.rn
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.os.Build
import android.os.SystemClock
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.sqlite.SQLiteHelper
import org.json.JSONArray
import org.json.JSONObject
import java.util.concurrent.ConcurrentHashMap
import android.database.CursorWindow

internal class RNSql(context: Context): SQLiteHelper(context, DATABASE_NAME, DATABASE_VERSION) {

Expand All @@ -27,6 +28,10 @@ internal class RNSql(context: Context): SQLiteHelper(context, DATABASE_NAME, DAT
override fun onConfigure(db: SQLiteDatabase) {
super.onConfigure(db)
db.execSQL("PRAGMA foreign_keys=OFF;")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
db.setMaxSqlCacheSize(SQLiteDatabase.MAX_SQL_CACHE_SIZE)
}
initCursorWindowSize()
}

fun getValue(key: String): String? {
Expand Down Expand Up @@ -96,4 +101,14 @@ internal class RNSql(context: Context): SQLiteHelper(context, DATABASE_NAME, DAT
setValue(key, value.toString())
}

private fun initCursorWindowSize() {
try {
val field = CursorWindow::class.java.getDeclaredField("sCursorWindowSize")
field.isAccessible = true
field.set(null, 100 * 1024 * 1024) //the 100MB is the new size
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -79,6 +80,9 @@ class SettingsRepository(
private val _walletPush = MutableEffectFlow<Unit>()
val walletPush = _walletPush.shareIn(scope, SharingStarted.Eagerly)

private val _isMigratedFlow = MutableStateFlow<Boolean?>(null)
val isMigratedFlow = _isMigratedFlow.asStateFlow().filterNotNull()

fun notifyWalletPush() {
_walletPush.tryEmit(Unit)
}
Expand Down Expand Up @@ -387,6 +391,7 @@ class SettingsRepository(
searchEngine = legacyValues.searchEngine
}

_isMigratedFlow.value = true
_currencyFlow.tryEmit(currency)
_languageFlow.tryEmit(language)
_hiddenBalancesFlow.tryEmit(hiddenBalances)
Expand Down
Loading

0 comments on commit 74fa76d

Please sign in to comment.