CW-924-Don't-prompt-to-save-to-address-book-on-standard-Bitcoin-Litecoin-Bitcoin-Cash-addresses (#2043)

* don't prompt regular btc ltc bch address

* minor fix
This commit is contained in:
Serhii 2025-03-04 03:58:56 +02:00 committed by GitHub
parent 074a38704e
commit dcd978eb38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View file

@ -281,6 +281,9 @@ class AddressValidator extends TextValidator {
}
}
static String get silentPaymentAddressPattern => SilentPaymentAddress.regex.pattern;
static String get mWebAddressPattern => MwebAddress.regex.pattern;
static String? getAddressFromStringPattern(CryptoCurrency type) {
String? pattern = null;

View file

@ -1,4 +1,5 @@
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/core/address_validator.dart';
import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/entities/contact_record.dart';
import 'package:cake_wallet/core/execution_state.dart';
@ -544,6 +545,10 @@ class SendPage extends BasePage {
);
newContactAddress = newContactAddress ?? sendViewModel.newContactAddress();
if (newContactAddress?.address != null && isRegularElectrumAddress(newContactAddress!.address)) {
newContactAddress = null;
}
if (sendViewModel.coinTypeToSpendFrom != UnspentCoinType.any) newContactAddress = null;
if (newContactAddress != null && sendViewModel.showAddressBookPopup) {
@ -664,4 +669,35 @@ class SendPage extends BasePage {
),
context: context);
}
bool isRegularElectrumAddress(String address) {
final supportedTypes = [CryptoCurrency.btc, CryptoCurrency.ltc, CryptoCurrency.bch];
final excludedPatterns = [
RegExp(AddressValidator.silentPaymentAddressPattern),
RegExp(AddressValidator.mWebAddressPattern)
];
final trimmed = address.trim();
bool isValid = false;
for (var type in supportedTypes) {
final addressPattern = AddressValidator.getAddressFromStringPattern(type);
if (addressPattern != null) {
final regex = RegExp('^$addressPattern\$');
if (regex.hasMatch(trimmed)) {
isValid = true;
break;
}
}
}
for (var pattern in excludedPatterns) {
if (pattern.hasMatch(trimmed)) {
return false;
}
}
return isValid;
}
}