cake_wallet/lib/entities/parse_address_from_domain.dart

94 lines
3.5 KiB
Dart
Raw Normal View History

2023-01-24 18:24:46 +00:00
import 'package:cake_wallet/core/address_validator.dart';
import 'package:cake_wallet/core/yat_service.dart';
import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/parsed_address.dart';
import 'package:cake_wallet/entities/unstoppable_domain_address.dart';
import 'package:cake_wallet/entities/emoji_string_extension.dart';
2023-01-24 18:24:46 +00:00
import 'package:cake_wallet/twitter/twitter_api.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cake_wallet/entities/fio_address_provider.dart';
class AddressResolver {
2022-10-12 17:09:57 +00:00
AddressResolver({required this.yatService, required this.walletType});
2023-01-31 19:39:08 +00:00
final YatService yatService;
final WalletType walletType;
2023-01-24 18:24:46 +00:00
static const unstoppableDomains = [
2023-01-31 19:39:08 +00:00
'crypto',
'zil',
'x',
'coin',
'wallet',
'bitcoin',
'888',
'nft',
'dao',
'blockchain'
];
2023-01-24 18:24:46 +00:00
static String? extractAddressByType({required String raw, required CryptoCurrency type}) {
final addressPattern = AddressValidator.getAddressFromStringPattern(type);
if (addressPattern == null) {
throw 'Unexpected token: $type for getAddressFromStringPattern';
}
final match = RegExp(addressPattern).firstMatch(raw);
return match?.group(0)?.replaceAll(RegExp('[^0-9a-zA-Z]'), '');
}
Future<ParsedAddress> resolve(String text, String ticker) async {
try {
2023-01-24 18:24:46 +00:00
if (text.startsWith('@') && !text.substring(1).contains('@')) {
final formattedName = text.substring(1);
final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName);
2023-01-31 19:39:08 +00:00
final address = extractAddressByType(
raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker));
2023-01-24 18:24:46 +00:00
if (address != null) {
2023-01-31 19:39:08 +00:00
return ParsedAddress.fetchTwitterAddress(address: address, name: text);
2023-01-24 18:24:46 +00:00
}
}
if (!text.startsWith('@') && text.contains('@') && !text.contains('.')) {
final bool isFioRegistered = await FioAddressProvider.checkAvail(text);
if (isFioRegistered) {
final address = await FioAddressProvider.getPubAddress(text, ticker);
2023-01-31 19:39:08 +00:00
return ParsedAddress.fetchFioAddress(address: address, name: text);
}
}
if (text.hasOnlyEmojis) {
if (walletType != WalletType.haven) {
final addresses = await yatService.fetchYatAddress(text, ticker);
return ParsedAddress.fetchEmojiAddress(addresses: addresses, name: text);
}
}
final formattedName = OpenaliasRecord.formatDomainName(text);
final domainParts = formattedName.split('.');
final name = domainParts.last;
if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) {
return ParsedAddress(addresses: [text]);
}
if (unstoppableDomains.any((domain) => name.trim() == domain)) {
final address = await fetchUnstoppableDomainAddress(text, ticker);
return ParsedAddress.fetchUnstoppableDomainAddress(address: address, name: text);
}
2023-01-31 19:39:08 +00:00
if (formattedName.contains(".")) {
final txtRecord = await OpenaliasRecord.lookupOpenAliasRecord(formattedName);
if (txtRecord != null) {
final record = await OpenaliasRecord.fetchAddressAndName(
formattedName: formattedName, ticker: ticker, txtRecord: txtRecord);
return ParsedAddress.fetchOpenAliasAddress(record: record, name: text);
}
}
} catch (e) {
print(e.toString());
}
return ParsedAddress(addresses: [text]);
}
}