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';
|
2021-10-01 15:13:10 +00:00
|
|
|
import 'package:cake_wallet/store/yat/yat_store.dart';
|
2022-01-14 13:18:03 +00:00
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2021-07-07 13:50:55 +00:00
|
|
|
|
2021-08-16 09:42:16 +00:00
|
|
|
const unstoppableDomains = [
|
2022-01-14 13:18:03 +00:00
|
|
|
'crypto',
|
|
|
|
'zil',
|
|
|
|
'x',
|
|
|
|
'coin',
|
|
|
|
'wallet',
|
|
|
|
'bitcoin',
|
|
|
|
'888',
|
|
|
|
'nft',
|
|
|
|
'dao',
|
|
|
|
'blockchain'
|
|
|
|
];
|
2021-07-07 13:50:55 +00:00
|
|
|
|
2021-07-07 14:31:16 +00:00
|
|
|
Future<ParsedAddress> parseAddressFromDomain(
|
|
|
|
String domain, String ticker) async {
|
2021-07-07 13:50:55 +00:00
|
|
|
try {
|
2021-07-16 10:28:46 +00:00
|
|
|
final formattedName = OpenaliasRecord.formatDomainName(domain);
|
|
|
|
final domainParts = formattedName.split('.');
|
2021-07-07 13:50:55 +00:00
|
|
|
final name = domainParts.last;
|
|
|
|
|
|
|
|
if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) {
|
2021-08-27 17:46:24 +00:00
|
|
|
try {
|
2021-09-20 14:56:27 +00:00
|
|
|
final addresses = await fetchYatAddress(domain, ticker);
|
2021-08-27 17:46:24 +00:00
|
|
|
|
2021-09-20 14:56:27 +00:00
|
|
|
if (addresses?.isEmpty ?? true) {
|
|
|
|
return ParsedAddress(
|
2022-01-14 13:18:03 +00:00
|
|
|
addresses: [domain], parseFrom: ParseFrom.yatRecord);
|
2021-08-27 17:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ParsedAddress(
|
2022-01-14 13:18:03 +00:00
|
|
|
addresses: addresses, name: domain, parseFrom: ParseFrom.yatRecord);
|
2021-08-27 17:46:24 +00:00
|
|
|
} catch (e) {
|
2021-09-20 14:56:27 +00:00
|
|
|
return ParsedAddress(addresses: [domain]);
|
2021-08-27 17:46:24 +00:00
|
|
|
}
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 09:42:16 +00:00
|
|
|
if (unstoppableDomains.any((domain) => name.contains(domain))) {
|
2022-01-14 13:18:03 +00:00
|
|
|
final address = await fetchUnstoppableDomainAddress(domain, ticker);
|
2021-07-07 13:50:55 +00:00
|
|
|
|
|
|
|
if (address?.isEmpty ?? true) {
|
2021-09-20 14:56:27 +00:00
|
|
|
return ParsedAddress(addresses: [domain]);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
return ParsedAddress(
|
2021-09-20 14:56:27 +00:00
|
|
|
addresses: [address],
|
2021-07-16 10:44:12 +00:00
|
|
|
name: domain,
|
2021-07-16 10:28:46 +00:00
|
|
|
parseFrom: ParseFrom.unstoppableDomains);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 13:18:03 +00:00
|
|
|
final record = await OpenaliasRecord.fetchAddressAndName(
|
|
|
|
formattedName: formattedName, ticker: ticker);
|
2021-07-07 13:50:55 +00:00
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
if (record == null || record.address.contains(formattedName)) {
|
2021-09-20 14:56:27 +00:00
|
|
|
return ParsedAddress(addresses: [domain]);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
return ParsedAddress(
|
2021-09-20 14:56:27 +00:00
|
|
|
addresses: [record.address],
|
2021-07-16 10:28:46 +00:00
|
|
|
name: record.name,
|
2022-01-18 07:46:13 +00:00
|
|
|
description: record.description,
|
2021-07-16 10:28:46 +00:00
|
|
|
parseFrom: ParseFrom.openAlias);
|
2021-07-07 13:50:55 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
|
2021-09-20 14:56:27 +00:00
|
|
|
return ParsedAddress(addresses: [domain]);
|
2022-01-14 13:18:03 +00:00
|
|
|
}
|