cake_wallet/lib/entities/parse_address_from_domain.dart

127 lines
4.7 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';
2023-09-21 14:16:27 +00:00
import 'package:cake_wallet/di.dart';
2023-09-25 13:47:39 +00:00
import 'package:cake_wallet/entities/ens_record.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-09-21 14:16:27 +00:00
import 'package:cake_wallet/ethereum/ethereum.dart';
import 'package:cake_wallet/store/app_store.dart';
2023-01-24 18:24:46 +00:00
import 'package:cake_wallet/twitter/twitter_api.dart';
import 'package:cw_core/crypto_currency.dart';
2023-10-03 14:55:38 +00:00
import 'package:cw_core/wallet_base.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',
'wallet',
'bitcoin',
'888',
'nft',
'dao',
'blockchain',
'polygon',
'klever',
'hi',
'kresus',
'anime',
'manga',
'binanceus'
2023-01-31 19:39:08 +00:00
];
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 Exception('Unexpected token: $type for getAddressFromStringPattern');
2023-01-24 18:24:46 +00:00
}
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-02-09 10:23:13 +00:00
final addressFromBio = extractAddressByType(
2023-02-13 22:23:57 +00:00
raw: twitterUser.description, type: CryptoCurrency.fromString(ticker));
2023-02-09 10:23:13 +00:00
if (addressFromBio != null) {
return ParsedAddress.fetchTwitterAddress(address: addressFromBio, name: text);
}
2023-02-13 22:23:57 +00:00
final tweets = twitterUser.tweets;
2023-02-12 22:38:12 +00:00
if (tweets != null) {
var subString = StringBuffer();
tweets.forEach((item) {
subString.writeln(item.text);
});
final userTweetsText = subString.toString();
final addressFromPinnedTweet =
2023-09-25 13:47:39 +00:00
extractAddressByType(raw: userTweetsText, type: CryptoCurrency.fromString(ticker));
2023-02-12 22:38:12 +00:00
if (addressFromPinnedTweet != null) {
return ParsedAddress.fetchTwitterAddress(address: addressFromPinnedTweet, 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-09-29 15:58:19 +00:00
if (text.endsWith(".eth")) {
2023-10-03 14:55:38 +00:00
WalletBase? wallet = getIt.get<AppStore>().wallet!;
2023-09-25 13:47:39 +00:00
final address = await EnsRecord.fetchEnsAddress(text, wallet: wallet);
2023-09-29 15:28:23 +00:00
if (address.isNotEmpty && address != "0x0000000000000000000000000000000000000000") {
2023-09-25 13:47:39 +00:00
return ParsedAddress.fetchEnsAddress(name: text, address: address);
}
}
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]);
}
}