mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 12:29:51 +00:00
CW-1073 Implement Monero wallet definition URI scheme (#2323)
* feat: add optional parameter to customize address extraction pattern * refactor: add parameter to control address extraction surrounding whitespace validation * fix: ensure proper handling of unmounted context in address extraction logic * test: add comprehensive unit tests for AddressResolver and AddressValidator classes
This commit is contained in:
parent
c6cb48096d
commit
150becb679
6 changed files with 401 additions and 30 deletions
|
@ -38,9 +38,9 @@ class AddressValidator extends TextValidator {
|
|||
'|[0-9a-zA-Z]{105}|addr1[0-9a-zA-Z]{98}';
|
||||
case CryptoCurrency.btc:
|
||||
pattern =
|
||||
'${P2pkhAddress.regex.pattern}|${P2shAddress.regex.pattern}|${RegExp(r'(bc|tb)1q[ac-hj-np-z02-9]{25,39}}').pattern}|${P2trAddress.regex.pattern}|${P2wshAddress.regex.pattern}|${SilentPaymentAddress.regex.pattern}';
|
||||
'${P2pkhAddress.regex.pattern}|${P2shAddress.regex.pattern}|${P2wpkhAddress.regex.pattern}|${P2trAddress.regex.pattern}|${P2wshAddress.regex.pattern}|${SilentPaymentAddress.regex.pattern}';
|
||||
case CryptoCurrency.ltc:
|
||||
pattern = '^${RegExp(r'ltc1q[ac-hj-np-z02-9]{25,39}').pattern}\$|^${MwebAddress.regex.pattern}\$';
|
||||
pattern = '${P2wpkhAddress.regex.pattern}|${MwebAddress.regex.pattern}';
|
||||
case CryptoCurrency.nano:
|
||||
pattern = '[0-9a-zA-Z_]+';
|
||||
case CryptoCurrency.banano:
|
||||
|
@ -335,10 +335,6 @@ class AddressValidator extends TextValidator {
|
|||
}
|
||||
}
|
||||
|
||||
if (pattern != null) {
|
||||
return "$BEFORE_REGEX($pattern)$AFTER_REGEX";
|
||||
}
|
||||
|
||||
return null;
|
||||
return pattern != null ? "($pattern)" : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,13 +165,19 @@ class AddressResolver {
|
|||
"zone"
|
||||
];
|
||||
|
||||
static String? extractAddressByType({required String raw, required CryptoCurrency type}) {
|
||||
final addressPattern = AddressValidator.getAddressFromStringPattern(type);
|
||||
static String? extractAddressByType(
|
||||
{required String raw,
|
||||
required CryptoCurrency type,
|
||||
bool requireSurroundingWhitespaces = true}) {
|
||||
var addressPattern = AddressValidator.getAddressFromStringPattern(type);
|
||||
|
||||
if (addressPattern == null) {
|
||||
throw Exception('Unexpected token: $type for getAddressFromStringPattern');
|
||||
}
|
||||
|
||||
if (requireSurroundingWhitespaces)
|
||||
addressPattern = "$BEFORE_REGEX$addressPattern$AFTER_REGEX";
|
||||
|
||||
final match = RegExp(addressPattern, multiLine: true).firstMatch(raw);
|
||||
return match?.group(0)?.replaceAllMapped(RegExp('[^0-9a-zA-Z]|bitcoincash:|nano_|ban_'),
|
||||
(Match match) {
|
||||
|
|
|
@ -8,6 +8,8 @@ import 'choose_yat_address_alert.dart';
|
|||
Future<String> extractAddressFromParsed(
|
||||
BuildContext context,
|
||||
ParsedAddress parsedAddress) async {
|
||||
if (!context.mounted) return parsedAddress.addresses.first;
|
||||
|
||||
var title = '';
|
||||
var content = '';
|
||||
var address = '';
|
||||
|
@ -95,16 +97,17 @@ Future<String> extractAddressFromParsed(
|
|||
content += S.of(context).choose_address;
|
||||
|
||||
address = await showPopUp<String?>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
|
||||
return WillPopScope(
|
||||
context: context,
|
||||
builder: (context) => PopScope(
|
||||
child: ChooseYatAddressAlert(
|
||||
alertTitle: title,
|
||||
alertContent: content,
|
||||
addresses: parsedAddress.addresses),
|
||||
onWillPop: () async => false);
|
||||
}) ?? '';
|
||||
addresses: parsedAddress.addresses,
|
||||
),
|
||||
canPop: false,
|
||||
),
|
||||
) ??
|
||||
'';
|
||||
|
||||
if (address.isEmpty) {
|
||||
return parsedAddress.name;
|
||||
|
@ -113,22 +116,20 @@ Future<String> extractAddressFromParsed(
|
|||
return address;
|
||||
case ParseFrom.contact:
|
||||
case ParseFrom.notParsed:
|
||||
address = parsedAddress.addresses.first;
|
||||
return address;
|
||||
return parsedAddress.addresses.first;
|
||||
}
|
||||
|
||||
await showPopUp<void>(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
|
||||
return AlertWithOneAction(
|
||||
alertTitle: title,
|
||||
headerTitleText: profileName.isEmpty ? null : profileName,
|
||||
headerImageProfileUrl: profileImageUrl.isEmpty ? null : profileImageUrl,
|
||||
alertContent: content,
|
||||
buttonText: S.of(context).ok,
|
||||
buttonAction: () => Navigator.of(context).pop());
|
||||
});
|
||||
context: context,
|
||||
builder: (context) => AlertWithOneAction(
|
||||
alertTitle: title,
|
||||
headerTitleText: profileName.isEmpty ? null : profileName,
|
||||
headerImageProfileUrl: profileImageUrl.isEmpty ? null : profileImageUrl,
|
||||
alertContent: content,
|
||||
buttonText: S.of(context).ok,
|
||||
buttonAction: () => Navigator.of(context).pop(),
|
||||
),
|
||||
);
|
||||
|
||||
return address;
|
||||
}
|
||||
|
|
|
@ -74,7 +74,10 @@ class WalletRestoreFromQRCode {
|
|||
static String? _extractAddressFromUrl(String rawString, WalletType type) {
|
||||
try {
|
||||
return AddressResolver.extractAddressByType(
|
||||
raw: rawString, type: walletTypeToCryptoCurrency(type));
|
||||
raw: rawString,
|
||||
type: walletTypeToCryptoCurrency(type),
|
||||
requireSurroundingWhitespaces: false,
|
||||
);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue