Skip to content
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
3 changes: 3 additions & 0 deletions app/src/main/java/to/bitkit/ext/TrezorExceptionExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ fun Throwable.isTrezorUserCancellation(): Boolean =
it is TrezorException.PinCancelled ||
it is TrezorException.PassphraseCancelled
}

fun Throwable.isTrezorDeviceBusy(): Boolean =
generateSequence(this) { it.cause }.any { it is TrezorException.DeviceBusy }
320 changes: 222 additions & 98 deletions app/src/main/java/to/bitkit/repositories/TrezorRepo.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import kotlinx.coroutines.launch
import to.bitkit.R
import to.bitkit.repositories.HwWalletRepo
import to.bitkit.repositories.HwWalletRepo.Companion.DEVICE_LABEL_MAX_LENGTH
import to.bitkit.ext.isTrezorDeviceBusy
import to.bitkit.repositories.resolveHwWalletName
import to.bitkit.utils.TrezorErrorPresenter
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds

Expand Down Expand Up @@ -110,25 +112,29 @@ class HwConnectViewModel @Inject constructor(
}
}
}
.onFailure {
onConnectFailed(resolvedDeviceId, resolvedDeviceModel)
.onFailure { error ->
onConnectFailed(resolvedDeviceId, resolvedDeviceModel, error)
return@launch
}
}
hwWalletRepo.connect(resolvedDeviceId)
.onSuccess { onConnected(resolvedDeviceId, it) }
.onFailure { onConnectFailed(resolvedDeviceId, resolvedDeviceModel) }
.onFailure { error -> onConnectFailed(resolvedDeviceId, resolvedDeviceModel, error) }
connectJob = null
}
}

private fun onConnectFailed(deviceId: String, deviceModel: String) {
private fun onConnectFailed(deviceId: String, deviceModel: String, error: Throwable) {
_uiState.update {
it.copy(
isConnecting = false,
foundDeviceId = deviceId,
deviceModel = deviceModel,
errorMessage = context.getString(R.string.hardware__connect_error),
errorMessage = if (error.isTrezorDeviceBusy()) {
TrezorErrorPresenter.userMessage(context, error)
} else {
context.getString(R.string.hardware__connect_error)
},
)
}
setEffect(
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/to/bitkit/utils/TrezorErrorPresenter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package to.bitkit.utils

import android.content.Context
import to.bitkit.R
import to.bitkit.ext.isTrezorDeviceBusy

object TrezorErrorPresenter {
fun userMessage(context: Context, error: Throwable): String {
if (error.isTrezorDeviceBusy()) {
return context.getString(R.string.hardware__device_busy)
}
return userMessage(
context = context,
error = error,
fallback = context.getString(R.string.hardware__connect_error),
)
}

fun userMessage(context: Context, error: Throwable, fallback: String): String {
if (error.isTrezorDeviceBusy()) {
return context.getString(R.string.hardware__device_busy)
}
return error.message?.takeIf { it.isNotBlank() } ?: fallback
}
}
23 changes: 15 additions & 8 deletions app/src/main/java/to/bitkit/viewmodels/TransferViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import to.bitkit.data.CacheStore
import to.bitkit.data.SettingsStore
import to.bitkit.env.Defaults
import to.bitkit.ext.amountOnClose
import to.bitkit.ext.isTrezorDeviceBusy
import to.bitkit.ext.isTrezorUserCancellation
import to.bitkit.models.HwFundingBroadcastResult
import to.bitkit.models.HwFundingTransaction
Expand All @@ -48,6 +49,7 @@ import to.bitkit.repositories.WalletRepo
import to.bitkit.ui.shared.toast.ToastEventBus
import to.bitkit.utils.AppError
import to.bitkit.utils.Logger
import to.bitkit.utils.TrezorErrorPresenter
import javax.inject.Inject
import kotlin.math.min
import kotlin.math.roundToLong
Expand Down Expand Up @@ -637,12 +639,21 @@ class TransferViewModel @Inject constructor(
}

private suspend fun handleHardwareTransferFailure(e: Throwable, deviceId: String) {
if (e.isTrezorUserCancellation()) {
Logger.info("Hardware transfer cancelled on device for '$deviceId'", context = TAG)
return
}
if (e.isTrezorDeviceBusy()) {
Logger.warn("Hardware transfer blocked by busy Trezor for '$deviceId'", e, context = TAG)
ToastEventBus.send(
type = Toast.ToastType.ERROR,
title = context.getString(R.string.common__error),
description = TrezorErrorPresenter.userMessage(context, e),
)
return
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}
when (e) {
is HardwareReconnectError -> {
if (e.isTrezorUserCancellation()) {
Logger.info("Hardware transfer cancelled on device for '$deviceId'", context = TAG)
return
}
Logger.error("Failed to reconnect hardware device", e, context = TAG)
showHardwareReconnectError(deviceId)
}
Expand All @@ -655,10 +666,6 @@ class TransferViewModel @Inject constructor(
showHardwareFundingError(e)
}
else -> {
if (e.isTrezorUserCancellation()) {
Logger.info("Hardware transfer cancelled on device for '$deviceId'", context = TAG)
return
}
Logger.error("Hardware transfer failed", e, context = TAG)
ToastEventBus.send(e)
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
<string name="hardware__connection_badge_connected_usb">Connected via USB</string>
<string name="hardware__connection_badge_disconnected_bluetooth">Disconnected via Bluetooth</string>
<string name="hardware__connection_badge_disconnected_usb">Disconnected via USB</string>
<string name="hardware__device_busy">Your Trezor is busy. Unlock it on the device, then try again.</string>
<string name="hardware__device_model_trezor">Trezor</string>
<string name="hardware__found_header">Found &lt;accent&gt;Trezor&lt;/accent&gt;</string>
<string name="hardware__found_text">Would you like to securely pair this %1$s with Bitkit?</string>
Expand Down
19 changes: 19 additions & 0 deletions app/src/test/java/to/bitkit/ext/TrezorExceptionExtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ class TrezorExceptionExtTest {
assertFalse(AppError("sign failed").isTrezorUserCancellation())
assertFalse(TrezorException.Timeout().isTrezorUserCancellation())
}

@Test
fun `isTrezorDeviceBusy returns true for device busy exceptions`() {
assertTrue(TrezorException.DeviceBusy().isTrezorDeviceBusy())
}

@Test
fun `isTrezorDeviceBusy walks the cause chain`() {
val error = AppError(TrezorException.DeviceBusy())

assertTrue(error.isTrezorDeviceBusy())
}

@Test
fun `isTrezorDeviceBusy returns false for other errors`() {
assertFalse(TrezorException.Timeout().isTrezorDeviceBusy())
assertFalse(TrezorException.UserCancelled().isTrezorDeviceBusy())
assertFalse(AppError("sign failed").isTrezorDeviceBusy())
}
}
Loading
Loading