Skip to content

Commit 1d2b668

Browse files
committed
chore: replace getAddressNetwork with bitkit-core method
1 parent e6a3969 commit 1d2b668

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

app/src/main/java/to/bitkit/models/Network.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ fun Network.toCoreNetworkType(): NetworkType = when (this) {
2424
Network.SIGNET -> NetworkType.SIGNET
2525
Network.REGTEST -> NetworkType.REGTEST
2626
}
27+
28+
fun NetworkType.toLdkNetwork(): Network = when (this) {
29+
NetworkType.BITCOIN -> Network.BITCOIN
30+
NetworkType.TESTNET -> Network.TESTNET
31+
NetworkType.SIGNET -> Network.SIGNET
32+
NetworkType.REGTEST -> Network.REGTEST
33+
}

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import to.bitkit.models.Toast
8686
import to.bitkit.models.TransactionSpeed
8787
import to.bitkit.models.safe
8888
import to.bitkit.models.toActivityFilter
89+
import to.bitkit.models.toLdkNetwork
8990
import to.bitkit.models.toTxType
9091
import to.bitkit.repositories.ActivityRepo
9192
import to.bitkit.repositories.BackupRepo
@@ -747,9 +748,17 @@ class AppViewModel @Inject constructor(
747748
}
748749

749750
private fun validateOnChainAddress(invoice: OnChainInvoice) {
750-
// Check network mismatch
751-
val addressNetwork = NetworkValidationHelper.getAddressNetwork(invoice.address)
752-
if (NetworkValidationHelper.isNetworkMismatch(addressNetwork, Env.network)) {
751+
val validatedAddress = runCatching { validateBitcoinAddress(invoice.address) }
752+
.getOrElse {
753+
showAddressValidationError(
754+
titleRes = R.string.other__scan_err_decoding,
755+
descriptionRes = R.string.wallet__error_invalid_bitcoin_address,
756+
testTag = "InvalidAddressToast",
757+
)
758+
return
759+
}
760+
761+
if (NetworkValidationHelper.isNetworkMismatch(validatedAddress.network.toLdkNetwork(), Env.network)) {
753762
showAddressValidationError(
754763
titleRes = R.string.other__scan_err_decoding,
755764
descriptionRes = R.string.other__scan__error__generic,
@@ -1000,11 +1009,20 @@ class AppViewModel @Inject constructor(
10001009
}
10011010
}
10021011

1003-
@Suppress("LongMethod", "CyclomaticComplexMethod")
1012+
@Suppress("LongMethod", "CyclomaticComplexMethod", "ReturnCount")
10041013
private suspend fun onScanOnchain(invoice: OnChainInvoice, scanResult: String) {
1005-
// Check network mismatch
1006-
val addressNetwork = NetworkValidationHelper.getAddressNetwork(invoice.address)
1007-
if (NetworkValidationHelper.isNetworkMismatch(addressNetwork, Env.network)) {
1014+
val validatedAddress = runCatching { validateBitcoinAddress(invoice.address) }
1015+
.getOrElse {
1016+
toast(
1017+
type = Toast.ToastType.ERROR,
1018+
title = context.getString(R.string.other__scan_err_decoding),
1019+
description = context.getString(R.string.wallet__error_invalid_bitcoin_address),
1020+
testTag = "InvalidAddressToast",
1021+
)
1022+
return
1023+
}
1024+
1025+
if (NetworkValidationHelper.isNetworkMismatch(validatedAddress.network.toLdkNetwork(), Env.network)) {
10081026
toast(
10091027
type = Toast.ToastType.ERROR,
10101028
title = context.getString(R.string.other__scan_err_decoding),
@@ -1484,18 +1502,9 @@ class AppViewModel @Inject constructor(
14841502
when (_sendUiState.value.payMethod) {
14851503
SendMethod.ONCHAIN -> {
14861504
val address = _sendUiState.value.address
1487-
// TODO validate early, validate network & address types, showing detailed errors
1488-
val validatedAddress = runCatching { validateBitcoinAddress(address) }
1489-
.getOrElse { e ->
1490-
Logger.error("Invalid bitcoin send address: '$address'", e, context = TAG)
1491-
toast(Exception(context.getString(R.string.wallet__error_invalid_bitcoin_address)))
1492-
hideSheet()
1493-
return
1494-
}
1495-
14961505
val tags = _sendUiState.value.selectedTags
14971506

1498-
sendOnchain(validatedAddress.address, amount, tags = tags)
1507+
sendOnchain(address, amount, tags = tags)
14991508
.onSuccess { txId ->
15001509
Logger.info("Onchain send result txid: $txId", context = TAG)
15011510
handlePaymentSuccess(

0 commit comments

Comments
 (0)