@@ -86,6 +86,7 @@ import to.bitkit.models.Toast
8686import to.bitkit.models.TransactionSpeed
8787import to.bitkit.models.safe
8888import to.bitkit.models.toActivityFilter
89+ import to.bitkit.models.toLdkNetwork
8990import to.bitkit.models.toTxType
9091import to.bitkit.repositories.ActivityRepo
9192import 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