Files
hash-wallet/lib/core/amount_validator.dart
T
Konstantin UllrichandGitHub e7b8229429 Handle network errors, improve Lightning setup, and add deposit events (#3127)
* fix: send all for lightning

* fix: adjust crypto amount parsing logic for send page outputs

* feat: add event handling for deposit transactions in Bitcoin and Lightning wallets

- Introduced `onCreateDepositTransactionEvent` and `onUpdateDepositTransactionEvent` callbacks to manage deposit transaction updates.
- Updated event listener logic to handle unclaimed and claimed deposit events.
- Added support for processing deposit transaction info with `ElectrumTransactionInfo`.

* fix: handle network errors on username validation and improve Lightning address setup

- Added network error handling for username validation with a fallback error.
- Enhanced Lightning address setup logic with error handling and a fail-safe mechanism to disable Lightning usage if the setup fails.

* fix: enhance error handling for Lightning and update validation patterns

* fix: remove redundant raw string mark from amount validation pattern

* fix: normalize crypto amount input by replacing commas with dots
2026-03-25 20:45:23 +01:00

172 lines
4.9 KiB
Dart

import 'package:cake_wallet/core/amount_parsing_proxy.dart';
import 'package:cake_wallet/core/validator.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/currency.dart';
class AmountValidator extends TextValidator {
AmountValidator({
required CryptoCurrency currency,
required AmountParsingProxy amountParsingProxy,
bool isAutovalidate = false,
String? minValue,
String? maxValue,
}) {
symbolsAmountValidator =
SymbolsAmountValidator(isAutovalidate: isAutovalidate);
decimalAmountValidator = DecimalAmountValidator(currency: currency,isAutovalidate: isAutovalidate);
amountMinValidator = AmountMinValidator(
minValue: minValue,
isAutovalidate: isAutovalidate,
amountParsingProxy: amountParsingProxy,
cryptoCurrency: currency,
);
amountMaxValidator = AmountMaxValidator(
maxValue: maxValue,
isAutovalidate: isAutovalidate,
amountParsingProxy: amountParsingProxy,
cryptoCurrency: currency,
);
}
late final AmountMinValidator amountMinValidator;
late final AmountMaxValidator amountMaxValidator;
late final SymbolsAmountValidator symbolsAmountValidator;
late final DecimalAmountValidator decimalAmountValidator;
String? call(String? value) {
if (value == null || value.isEmpty) {
return S.current.error_text_amount;
}
//* Validate for Text(length, symbols, decimals etc)
final textValidation = symbolsAmountValidator(value) ?? decimalAmountValidator(value);
//* Validate for Comparison(Value greater than min and less than )
final comparisonValidation = amountMinValidator(value) ?? amountMaxValidator(value);
return textValidation ?? comparisonValidation;
}
}
class SymbolsAmountValidator extends TextValidator {
SymbolsAmountValidator({required bool isAutovalidate})
: super(
errorMessage: S.current.error_text_amount,
pattern: _pattern(),
isAutovalidate: isAutovalidate,
minLength: 0,
maxLength: 0,
);
static String _pattern() => r'^(?:\d+(?:[.\,]\d+)?|[.\,]\d+)$';
}
class DecimalAmountValidator extends TextValidator {
DecimalAmountValidator({required Currency currency, required bool isAutovalidate})
: super(
errorMessage: S.current.decimal_places_error,
pattern: _pattern(currency),
isAutovalidate: isAutovalidate,
minLength: 0,
maxLength: 0,
);
static String _pattern(Currency currency) =>
'^([0-9]+([.\,][0-9]{1,${currency.decimals}})?|[.\,][0-9]{1,${currency.decimals}})\$';
}
class AllAmountValidator extends TextValidator {
AllAmountValidator()
: super(
errorMessage: S.current.error_text_amount,
pattern: S.current.all,
minLength: 0,
maxLength: 0,
);
}
class AmountMinValidator extends Validator<String> {
final String? minValue;
final bool isAutovalidate;
final AmountParsingProxy amountParsingProxy;
final CryptoCurrency cryptoCurrency;
AmountMinValidator({
this.minValue,
required this.isAutovalidate,
required this.amountParsingProxy,
required this.cryptoCurrency,
}) : super(errorMessage: S.current.error_text_input_below_minimum_limit);
@override
bool isValid(String? value) {
if (value == null || value.isEmpty) {
return isAutovalidate ? true : false;
}
if (minValue == null || minValue == "null") {
return true;
}
value = amountParsingProxy.getCanonicalCryptoAmount(value, cryptoCurrency);
final valueInDouble = parseToDouble(value);
final minInDouble = parseToDouble(minValue ?? '');
if (valueInDouble == null || minInDouble == null) {
return false;
}
return valueInDouble >= minInDouble;
}
double? parseToDouble(String value) {
final data = double.tryParse(value.replaceAll(',', '.'));
return data;
}
}
class AmountMaxValidator extends Validator<String> {
final String? maxValue;
final bool isAutovalidate;
final AmountParsingProxy amountParsingProxy;
final CryptoCurrency cryptoCurrency;
AmountMaxValidator({
this.maxValue,
required this.isAutovalidate,
required this.amountParsingProxy,
required this.cryptoCurrency,
}) : super(errorMessage: S.current.error_text_input_above_maximum_limit);
@override
bool isValid(String? value) {
if (value == null || value.isEmpty) {
return isAutovalidate ? true : false;
}
if (maxValue == null || maxValue == "null") {
return true;
}
value = amountParsingProxy.getCanonicalCryptoAmount(value, cryptoCurrency);
final valueInDouble = parseToDouble(value);
final maxInDouble = parseToDouble(maxValue ?? '');
if (valueInDouble == null || maxInDouble == null) {
return false;
}
return valueInDouble <= maxInDouble;
}
double? parseToDouble(String value) {
return double.tryParse(value.replaceAll(',', '.'));
}
}