Skip to content

Commit

Permalink
Fix regex pattern (#1695)
Browse files Browse the repository at this point in the history
* fix: regex pattern

* fix: extracting regex
  • Loading branch information
rafael-xmr authored Sep 26, 2024
1 parent fc7bea6 commit e9b5b18
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions lib/core/address_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import 'package:cake_wallet/solana/solana.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/erc20_token.dart';

const BEFORE_REGEX = '(^|\\s)';
const AFTER_REGEX = '(\$|\\s)';

class AddressValidator extends TextValidator {
AddressValidator({required CryptoCurrency type})
: super(
Expand All @@ -19,21 +22,23 @@ class AddressValidator extends TextValidator {
length: getLength(type));

static String getPattern(CryptoCurrency type) {
var pattern = "";
if (type is Erc20Token) {
return '0x[0-9a-zA-Z]';
pattern = '0x[0-9a-zA-Z]+';
}
switch (type) {
case CryptoCurrency.xmr:
return '^4[0-9a-zA-Z]{94}\$|^8[0-9a-zA-Z]{94}\$|^[0-9a-zA-Z]{106}\$';
pattern = '4[0-9a-zA-Z]{94}|8[0-9a-zA-Z]{94}|[0-9a-zA-Z]{106}';
case CryptoCurrency.ada:
return '^[0-9a-zA-Z]{59}\$|^[0-9a-zA-Z]{92}\$|^[0-9a-zA-Z]{104}\$'
'|^[0-9a-zA-Z]{105}\$|^addr1[0-9a-zA-Z]{98}\$';
pattern = '[0-9a-zA-Z]{59}|[0-9a-zA-Z]{92}|[0-9a-zA-Z]{104}'
'|[0-9a-zA-Z]{105}|addr1[0-9a-zA-Z]{98}';
case CryptoCurrency.btc:
return '^${P2pkhAddress.regex.pattern}\$|^${P2shAddress.regex.pattern}\$|^${P2wpkhAddress.regex.pattern}\$|${P2trAddress.regex.pattern}\$|^${P2wshAddress.regex.pattern}\$|^${SilentPaymentAddress.regex.pattern}\$';
pattern =
'${P2pkhAddress.regex.pattern}|${P2shAddress.regex.pattern}|${P2wpkhAddress.regex.pattern}|${P2trAddress.regex.pattern}|${P2wshAddress.regex.pattern}|${SilentPaymentAddress.regex.pattern}';
case CryptoCurrency.nano:
return '[0-9a-zA-Z_]';
pattern = '[0-9a-zA-Z_]+';
case CryptoCurrency.banano:
return '[0-9a-zA-Z_]';
pattern = '[0-9a-zA-Z_]+';
case CryptoCurrency.usdc:
case CryptoCurrency.usdcpoly:
case CryptoCurrency.usdtPoly:
Expand Down Expand Up @@ -69,11 +74,11 @@ class AddressValidator extends TextValidator {
case CryptoCurrency.dydx:
case CryptoCurrency.steth:
case CryptoCurrency.shib:
return '0x[0-9a-zA-Z]';
pattern = '0x[0-9a-zA-Z]+';
case CryptoCurrency.xrp:
return '^[0-9a-zA-Z]{34}\$|^X[0-9a-zA-Z]{46}\$';
pattern = '[0-9a-zA-Z]{34}|X[0-9a-zA-Z]{46}';
case CryptoCurrency.xhv:
return '^hvx|hvi|hvs[0-9a-zA-Z]';
pattern = 'hvx|hvi|hvs[0-9a-zA-Z]+';
case CryptoCurrency.xag:
case CryptoCurrency.xau:
case CryptoCurrency.xaud:
Expand All @@ -95,40 +100,43 @@ class AddressValidator extends TextValidator {
case CryptoCurrency.dash:
case CryptoCurrency.eos:
case CryptoCurrency.wow:
return '[0-9a-zA-Z]';
pattern = '[0-9a-zA-Z]+';
case CryptoCurrency.bch:
return '^(?!bitcoincash:)[0-9a-zA-Z]*\$|^(?!bitcoincash:)q|p[0-9a-zA-Z]{41}\$|^(?!bitcoincash:)q|p[0-9a-zA-Z]{42}\$|^bitcoincash:q|p[0-9a-zA-Z]{41}\$|^bitcoincash:q|p[0-9a-zA-Z]{42}\$';
pattern =
'(?!bitcoincash:)[0-9a-zA-Z]*|(?!bitcoincash:)q|p[0-9a-zA-Z]{41}|(?!bitcoincash:)q|p[0-9a-zA-Z]{42}|bitcoincash:q|p[0-9a-zA-Z]{41}|bitcoincash:q|p[0-9a-zA-Z]{42}';
case CryptoCurrency.bnb:
return '[0-9a-zA-Z]';
pattern = '[0-9a-zA-Z]+';
case CryptoCurrency.ltc:
return '^(?!(ltc|LTC)1)[0-9a-zA-Z]*\$|(^LTC1[A-Z0-9]*\$)|(^ltc1[a-z0-9]*\$)';
pattern = '((?!(ltc|LTC)1)[0-9a-zA-Z]*)|(LTC1[A-Z0-9]*)|(ltc1[a-z0-9]*)';
case CryptoCurrency.hbar:
return '[0-9a-zA-Z.]';
pattern = '[0-9a-zA-Z.]+';
case CryptoCurrency.zaddr:
return '^zs[0-9a-zA-Z]{75}';
pattern = 'zs[0-9a-zA-Z]{75}';
case CryptoCurrency.zec:
return '^t1[0-9a-zA-Z]{33}\$|^t3[0-9a-zA-Z]{33}\$';
pattern = 't1[0-9a-zA-Z]{33}|t3[0-9a-zA-Z]{33}';
case CryptoCurrency.dcr:
return 'D[ksecS]([0-9a-zA-Z])+';
pattern = 'D[ksecS]([0-9a-zA-Z])+';
case CryptoCurrency.rvn:
return '[Rr]([1-9a-km-zA-HJ-NP-Z]){33}';
pattern = '[Rr]([1-9a-km-zA-HJ-NP-Z]){33}';
case CryptoCurrency.near:
return '[0-9a-f]{64}';
pattern = '[0-9a-f]{64}';
case CryptoCurrency.rune:
return 'thor1[0-9a-z]{38}';
pattern = 'thor1[0-9a-z]{38}';
case CryptoCurrency.scrt:
return 'secret1[0-9a-z]{38}';
pattern = 'secret1[0-9a-z]{38}';
case CryptoCurrency.stx:
return 'S[MP][0-9a-zA-Z]+';
pattern = 'S[MP][0-9a-zA-Z]+';
case CryptoCurrency.kmd:
return 'R[0-9a-zA-Z]{33}';
pattern = 'R[0-9a-zA-Z]{33}';
case CryptoCurrency.pivx:
return 'D([1-9a-km-zA-HJ-NP-Z]){33}';
pattern = 'D([1-9a-km-zA-HJ-NP-Z]){33}';
case CryptoCurrency.btcln:
return '^(lnbc|LNBC)([0-9]{1,}[a-zA-Z0-9]+)';
pattern = '(lnbc|LNBC)([0-9]{1,}[a-zA-Z0-9]+)';
default:
return '[0-9a-zA-Z]';
pattern = '[0-9a-zA-Z]+';
}

return '$BEFORE_REGEX($pattern)$AFTER_REGEX';
}

static List<int>? getLength(CryptoCurrency type) {
Expand Down Expand Up @@ -269,56 +277,53 @@ class AddressValidator extends TextValidator {
}

static String? getAddressFromStringPattern(CryptoCurrency type) {
String? pattern = null;

switch (type) {
case CryptoCurrency.xmr:
case CryptoCurrency.wow:
return '([^0-9a-zA-Z]|^)4[0-9a-zA-Z]{94}([^0-9a-zA-Z]|\$)'
'|([^0-9a-zA-Z]|^)8[0-9a-zA-Z]{94}([^0-9a-zA-Z]|\$)'
'|([^0-9a-zA-Z]|^)[0-9a-zA-Z]{106}([^0-9a-zA-Z]|\$)';
pattern = '(4[0-9a-zA-Z]{94})'
'|(8[0-9a-zA-Z]{94})'
'|([0-9a-zA-Z]{106})';
case CryptoCurrency.btc:
return '([^0-9a-zA-Z]|^)([1mn][a-km-zA-HJ-NP-Z1-9]{25,34})([^0-9a-zA-Z]|\$)' //P2pkhAddress type
'|([^0-9a-zA-Z]|^)([23][a-km-zA-HJ-NP-Z1-9]{25,34})([^0-9a-zA-Z]|\$)' //P2shAddress type
'|([^0-9a-zA-Z]|^)((bc|tb)1q[ac-hj-np-z02-9]{25,39})([^0-9a-zA-Z]|\$)' //P2wpkhAddress type
'|([^0-9a-zA-Z]|^)((bc|tb)1q[ac-hj-np-z02-9]{40,80})([^0-9a-zA-Z]|\$)' //P2wshAddress type
'|([^0-9a-zA-Z]|^)((bc|tb)1p([ac-hj-np-z02-9]{39}|[ac-hj-np-z02-9]{59}|[ac-hj-np-z02-9]{8,89}))([^0-9a-zA-Z]|\$)' //P2trAddress type
'|${SilentPaymentAddress.regex.pattern}\$';

pattern =
'${P2pkhAddress.regex.pattern}|${P2shAddress.regex.pattern}|${P2wpkhAddress.regex.pattern}|${P2trAddress.regex.pattern}|${P2wshAddress.regex.pattern}|${SilentPaymentAddress.regex.pattern}';
case CryptoCurrency.ltc:
return '([^0-9a-zA-Z]|^)^L[a-zA-Z0-9]{26,33}([^0-9a-zA-Z]|\$)'
'|([^0-9a-zA-Z]|^)[LM][a-km-zA-HJ-NP-Z1-9]{26,33}([^0-9a-zA-Z]|\$)'
'|([^0-9a-zA-Z]|^)ltc[a-zA-Z0-9]{26,45}([^0-9a-zA-Z]|\$)';
pattern = '(L[a-zA-Z0-9]{26,33})'
'|([LM][a-km-zA-HJ-NP-Z1-9]{26,33})'
'|(ltc[a-zA-Z0-9]{26,45})';
case CryptoCurrency.eth:
return '0x[0-9a-zA-Z]{42}';
case CryptoCurrency.maticpoly:
return '0x[0-9a-zA-Z]{42}';
pattern = '0x[0-9a-zA-Z]+';
case CryptoCurrency.nano:
return 'nano_[0-9a-zA-Z]{60}';
pattern = 'nano_[0-9a-zA-Z]{60}';
case CryptoCurrency.banano:
return 'ban_[0-9a-zA-Z]{60}';
pattern = 'ban_[0-9a-zA-Z]{60}';
case CryptoCurrency.bch:
return 'bitcoincash:q[0-9a-zA-Z]{41}([^0-9a-zA-Z]|\$)'
'|bitcoincash:q[0-9a-zA-Z]{42}([^0-9a-zA-Z]|\$)'
'|([^0-9a-zA-Z]|^)q[0-9a-zA-Z]{41}([^0-9a-zA-Z]|\$)'
'|([^0-9a-zA-Z]|^)q[0-9a-zA-Z]{42}([^0-9a-zA-Z]|\$)';
pattern = '(bitcoincash:)?q[0-9a-zA-Z]{41,42}';
case CryptoCurrency.sol:
return '([^0-9a-zA-Z]|^)[1-9A-HJ-NP-Za-km-z]{43,44}([^0-9a-zA-Z]|\$)';
pattern = '[1-9A-HJ-NP-Za-km-z]+';
case CryptoCurrency.trx:
return '(T|t)[1-9A-HJ-NP-Za-km-z]{33}';
pattern = '(T|t)[1-9A-HJ-NP-Za-km-z]{33}';
default:
if (type.tag == CryptoCurrency.eth.title) {
return '0x[0-9a-zA-Z]{42}';
pattern = '0x[0-9a-zA-Z]{42}';
}
if (type.tag == CryptoCurrency.maticpoly.tag) {
return '0x[0-9a-zA-Z]{42}';
pattern = '0x[0-9a-zA-Z]{42}';
}
if (type.tag == CryptoCurrency.sol.title) {
return '([^0-9a-zA-Z]|^)[1-9A-HJ-NP-Za-km-z]{43,44}([^0-9a-zA-Z]|\$)';
pattern = '[1-9A-HJ-NP-Za-km-z]{43,44}';
}
if (type.tag == CryptoCurrency.trx.title) {
return '(T|t)[1-9A-HJ-NP-Za-km-z]{33}';
pattern = '(T|t)[1-9A-HJ-NP-Za-km-z]{33}';
}
}

return null;
if (pattern != null) {
return "$BEFORE_REGEX($pattern)$AFTER_REGEX";
}

return null;
}
}

0 comments on commit e9b5b18

Please sign in to comment.