2023-01-24 18:24:46 +00:00
|
|
|
import 'package:cake_wallet/core/address_validator.dart';
|
2022-03-15 09:11:53 +00:00
|
|
|
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';
|
2021-07-07 13:50:55 +00:00
|
|
|
import 'package:cake_wallet/entities/openalias_record.dart';
|
2021-07-07 14:31:16 +00:00
|
|
|
import 'package:cake_wallet/entities/parsed_address.dart';
|
2021-07-07 13:50:55 +00:00
|
|
|
import 'package:cake_wallet/entities/unstoppable_domain_address.dart';
|
2022-03-15 09:11:53 +00:00
|
|
|
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';
|
2022-06-28 15:46:32 +00:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2022-06-13 11:41:46 +00:00
|
|
|
import 'package:cake_wallet/entities/fio_address_provider.dart';
|
2022-03-15 09:11:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-03-15 09:11:53 +00:00
|
|
|
final YatService yatService;
|
2022-06-28 15:46:32 +00:00
|
|
|
final WalletType walletType;
|
2023-01-24 18:24:46 +00:00
|
|
|
|
2022-03-15 09:11:53 +00:00
|
|
|
static const unstoppableDomains = [
|
2023-01-31 19:39:08 +00:00
|
|
|
'crypto',
|
|
|
|
'zil',
|
|
|
|
'x',
|
|
|
|
'wallet',
|
|
|
|
'bitcoin',
|
|
|
|
'888',
|
|
|
|
'nft',
|
|
|
|
'dao',
|
2023-03-14 15:16:20 +00:00
|
|
|
'blockchain',
|
2023-08-10 12:38:53 +00:00
|
|
|
'polygon',
|
|
|
|
'klever',
|
|
|
|
'hi',
|
|
|
|
'kresus',
|
|
|
|
'anime',
|
|
|
|
'manga',
|
|
|
|
'binanceus'
|
2023-01-31 19:39:08 +00:00
|
|
|
];
|
2021-07-07 13:50:55 +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) {
|
2023-04-21 18:36:47 +00:00
|
|
|
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]'), '');
|
|
|
|
}
|
|
|
|
|
2022-03-15 09:11:53 +00:00
|
|
|
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('.')) {
|
2022-06-13 11:41:46 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-06-13 11:41:46 +00:00
|
|
|
}
|
2022-03-15 09:11:53 +00:00
|
|
|
if (text.hasOnlyEmojis) {
|
2022-06-28 15:46:32 +00:00
|
|
|
if (walletType != WalletType.haven) {
|
|
|
|
final addresses = await yatService.fetchYatAddress(text, ticker);
|
|
|
|
return ParsedAddress.fetchEmojiAddress(addresses: addresses, name: text);
|
|
|
|
}
|
2021-08-27 17:46:24 +00:00
|
|
|
}
|
2022-03-15 09:11:53 +00:00
|
|
|
final formattedName = OpenaliasRecord.formatDomainName(text);
|
|
|
|
final domainParts = formattedName.split('.');
|
|
|
|
final name = domainParts.last;
|
2021-07-07 13:50:55 +00:00
|
|
|
|
2022-03-15 09:11:53 +00:00
|
|
|
if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) {
|
|
|
|
return ParsedAddress(addresses: [text]);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 13:55:21 +00:00
|
|
|
if (unstoppableDomains.any((domain) => name.trim() == domain)) {
|
2022-03-15 09:11:53 +00:00
|
|
|
final address = await fetchUnstoppableDomainAddress(text, ticker);
|
|
|
|
return ParsedAddress.fetchUnstoppableDomainAddress(address: address, name: text);
|
|
|
|
}
|
2021-07-07 13:50:55 +00:00
|
|
|
|
2023-09-25 13:47:39 +00:00
|
|
|
if (text.contains(".")) {
|
|
|
|
var wallet = getIt.get<AppStore>().wallet!;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 09:11:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 09:11:53 +00:00
|
|
|
return ParsedAddress(addresses: [text]);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
2022-01-14 13:18:03 +00:00
|
|
|
}
|