Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix sentry errors #1058

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import com.tari.android.wallet.service.connection.TariWalletServiceConnection
import io.reactivex.disposables.CompositeDisposable
import org.apache.commons.io.IOUtils

private const val REGEX_ONION = "(.+::[A-Za-z0-9 ]{64}::/onion3/[A-Za-z0-9]+:[\\d]+)"
private const val REGEX_IPV4 = "(.+::[A-Za-z0-9 ]{64}::/ip4/[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}/tcp/[0-9]{2,6})"

class BaseNodes(
private val context: Context,
private val baseNodeSharedRepository: BaseNodeSharedRepository,
private val networkRepository: NetworkRepository,
) {
private val logger = Logger.t(this::class.simpleName)

private val compositeDisposable = CompositeDisposable()

private val serviceConnection = TariWalletServiceConnection()
Expand All @@ -45,9 +50,6 @@ class BaseNodes(
}.addTo(compositeDisposable)
}

val onionRegex = "(.+::[A-Za-z0-9 ]{64}::/onion3/[A-Za-z0-9]+:[\\d]+)"
val ipV4Regex = "(.+::[A-Za-z0-9 ]{64}::/ip4/[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}/tcp/[0-9]{2,6})"

/**
* Returns the list of base nodes in the resource file base_nodes.txt as pairs of
* ({name}, {public_key_hex}, {public_address}).
Expand All @@ -57,20 +59,20 @@ class BaseNodes(
context.resources.openRawResource(getBaseNodeResource(networkRepository.currentNetwork!!.network)),
"UTF-8"
)
Logger.t(this::class.simpleName).e("baseNodeList: $fileContent")
logger.i("baseNodeList: $fileContent")
val list = mutableListOf<BaseNodeDto>()
val onionBaseNodes = findAndAddBaseNode(fileContent, onionRegex).toList()
val ipV4BaseNodes = findAndAddBaseNode(fileContent, ipV4Regex).toList()
val onionBaseNodes = findAndAddBaseNode(fileContent, REGEX_ONION).toList()
val ipV4BaseNodes = findAndAddBaseNode(fileContent, REGEX_IPV4).toList()
list.addAll(onionBaseNodes)
list.addAll(ipV4BaseNodes)
list.sortedBy { it.name }
}

private fun findAndAddBaseNode(fileContent: String, regex: String): Sequence<BaseNodeDto> {
return Regex(regex).findAll(fileContent).map { matchResult ->
val tripleString = matchResult.value.split("::")
Logger.t(this::class.simpleName).i("baseNodeList0: $tripleString, baseNodeList1: ${tripleString[1]}, baseNodeList2: ${tripleString[2]}")
BaseNodeDto(tripleString[0], tripleString[1], tripleString[2])
val (name, publicKeyHex, address) = matchResult.value.split("::")
logger.i("baseNodeList0: $name, baseNodeList1: $publicKeyHex, baseNodeList2: $address")
BaseNodeDto(name, publicKeyHex, address)
}
}

Expand All @@ -95,21 +97,21 @@ class BaseNodes(

fun startSync() {
try {
Logger.t(this::class.simpleName).i("startSync")
logger.i("startSync")
//essential for wallet creation flow
val baseNode = baseNodeSharedRepository.currentBaseNode ?: return
serviceConnection.currentState.service ?: return
if (EventBus.walletState.publishSubject.value != WalletState.Running) return

Logger.t(this::class.simpleName).i("startSync:publicKeyHex: ${baseNode.publicKeyHex}")
Logger.t(this::class.simpleName).i("startSync:address: ${baseNode.address}")
Logger.t(this::class.simpleName).i("startSync:address: ${Gson().toJson(baseNodeSharedRepository.userBaseNodes)}")
logger.i("startSync:publicKeyHex: ${baseNode.publicKeyHex}")
logger.i("startSync:address: ${baseNode.address}")
logger.i("startSync:userBaseNodes: ${Gson().toJson(baseNodeSharedRepository.userBaseNodes)}")
val baseNodeKeyFFI = FFIPublicKey(HexString(baseNode.publicKeyHex))
FFIWallet.instance?.addBaseNodePeer(baseNodeKeyFFI, baseNode.address)
baseNodeKeyFFI.destroy()
walletService.getWithError { error, wallet -> wallet.startBaseNodeSync(error) }
} catch (e: Throwable) {
Logger.t(this::class.simpleName).i("startSync")
logger.i("startSync:error connecting to base node: ${e.message}")
setNextBaseNode()
startSync()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ abstract class CommonFragment<Binding : ViewBinding, VM : CommonViewModel> : Fra

override fun onResume() {
super.onResume()
if (!isAdded) return

if (blockScreenRecording) {
requireActivity().window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.daasuu.ei.Ease
import com.daasuu.ei.EasingInterpolator
Expand All @@ -49,7 +50,7 @@ import com.tari.android.wallet.application.WalletState
import com.tari.android.wallet.databinding.FragmentLocalAuthBinding
import com.tari.android.wallet.event.EventBus
import com.tari.android.wallet.extension.addTo
import com.tari.android.wallet.extension.observe
import com.tari.android.wallet.extension.launchAndRepeatOnLifecycle
import com.tari.android.wallet.infrastructure.security.biometric.BiometricAuthenticationException
import com.tari.android.wallet.ui.common.CommonFragment
import com.tari.android.wallet.ui.extension.doOnGlobalLayout
Expand Down Expand Up @@ -78,11 +79,15 @@ class LocalAuthFragment : CommonFragment<FragmentLocalAuthBinding, LocalAuthView
}

private fun observeUi() = with(viewModel) {
observe(secureState) {
ui.continueBtn.setVisible(it.pinCodeSecured)
ui.authTypeBiometrics.setVisible(it.biometricsAvailable)
ui.secureWithPasscode.setVisible(!it.pinCodeSecured)
ui.secureWithBiometrics.setVisible(it.biometricsAvailable && !it.biometricsSecured)
viewLifecycleOwner.launchAndRepeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
secureState.collect { state ->
ui.continueBtn.setVisible(state.pinCodeSecured)
ui.authTypeBiometrics.setVisible(state.biometricsAvailable)
ui.secureWithPasscode.setVisible(!state.pinCodeSecured)
ui.secureWithBiometrics.setVisible(state.biometricsAvailable && !state.biometricsSecured)
}
}
}
}

Expand Down Expand Up @@ -122,10 +127,11 @@ class LocalAuthFragment : CommonFragment<FragmentLocalAuthBinding, LocalAuthView
lifecycleScope.launch {
try {
val subtitle = string(onboarding_auth_biometric_prompt)
if (viewModel.authService.authenticate(this@LocalAuthFragment, string(onboarding_auth_title), subtitle))
if (viewModel.authService.authenticate(this@LocalAuthFragment, string(onboarding_auth_title), subtitle)) {
viewModel.securedWithBiometrics()
}
} catch (exception: BiometricAuthenticationException) {
viewModel.logger.i( exception.message + "Biometric authentication failed")
viewModel.logger.i(exception.message + "Biometric authentication failed")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.tari.android.wallet.ui.fragment.onboarding.localAuth

import androidx.lifecycle.MutableLiveData
import com.tari.android.wallet.extension.addTo
import com.tari.android.wallet.infrastructure.backup.BackupManager
import com.tari.android.wallet.infrastructure.security.biometric.BiometricAuthenticationService
import com.tari.android.wallet.ui.common.CommonViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject

class LocalAuthViewModel : CommonViewModel() {
Expand All @@ -15,8 +17,8 @@ class LocalAuthViewModel : CommonViewModel() {
@Inject
lateinit var backupManager: BackupManager

val secureState = MutableLiveData(SecureState(true, false, false))

private val _secureState = MutableStateFlow(SecureState())
val secureState = _secureState.asStateFlow()

init {
component.inject(this)
Expand All @@ -28,16 +30,18 @@ class LocalAuthViewModel : CommonViewModel() {
}.addTo(compositeDisposable)
}

fun updateState() {
secureState.value = SecureState(
authService.isBiometricAuthAvailable,
securityPrefRepository.pinCode != null,
securityPrefRepository.biometricsAuth == true
)
private fun updateState() {
_secureState.update {
SecureState(
biometricsAvailable = authService.isBiometricAuthAvailable,
pinCodeSecured = securityPrefRepository.pinCode != null,
biometricsSecured = securityPrefRepository.biometricsAuth == true,
)
}
}

fun securedWithBiometrics() {
securityPrefRepository.biometricsAuth = true
secureState.value = secureState.value!!.copy(biometricsSecured = true)
_secureState.update { it.copy(biometricsSecured = true) }
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package com.tari.android.wallet.ui.fragment.onboarding.localAuth

data class SecureState(val biometricsAvailable: Boolean, val pinCodeSecured: Boolean, val biometricsSecured: Boolean)
data class SecureState(
val biometricsAvailable: Boolean = true,
val pinCodeSecured: Boolean = false,
val biometricsSecured: Boolean = false,
)