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';
|
|
|
|
|
|
|
|
const topLevelDomain = 'crypto';
|
|
|
|
|
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-07-16 10:28:46 +00:00
|
|
|
return ParsedAddress(address: domain);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name.contains(topLevelDomain)) {
|
2021-07-16 10:28:46 +00:00
|
|
|
final address =
|
|
|
|
await fetchUnstoppableDomainAddress(formattedName, ticker);
|
2021-07-07 13:50:55 +00:00
|
|
|
|
|
|
|
if (address?.isEmpty ?? true) {
|
2021-07-16 10:28:46 +00:00
|
|
|
return ParsedAddress(address: domain);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
return ParsedAddress(
|
|
|
|
address: address,
|
|
|
|
name: formattedName,
|
|
|
|
parseFrom: ParseFrom.unstoppableDomains);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
final record = await OpenaliasRecord.fetchAddressAndName(formattedName);
|
2021-07-07 13:50:55 +00:00
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
if (record == null || record.address.contains(formattedName)) {
|
|
|
|
return ParsedAddress(address: domain);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
return ParsedAddress(
|
|
|
|
address: record.address,
|
|
|
|
name: record.name,
|
|
|
|
parseFrom: ParseFrom.openAlias);
|
2021-07-07 13:50:55 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:28:46 +00:00
|
|
|
return ParsedAddress(address: domain);
|
2021-07-07 13:50:55 +00:00
|
|
|
}
|