CWA-69 | reworked OpenaliasRecord class

This commit is contained in:
Oleksandr Sobol 2020-02-25 14:45:06 +02:00
parent 0efe8af3e0
commit 652ba16777
3 changed files with 35 additions and 35 deletions

View file

@ -2,15 +2,12 @@ import 'package:basic_utils/basic_utils.dart';
class OpenaliasRecord { class OpenaliasRecord {
OpenaliasRecord({this.name}); OpenaliasRecord({this.address, this.name});
String name; final String name;
String address; final String address;
String get recordName => name; static String formatDomainName(String name) {
String get recordAddress => address;
String formatDomainName() {
String formattedName = name; String formattedName = name;
if (name.contains("@")) { if (name.contains("@")) {
@ -20,12 +17,13 @@ class OpenaliasRecord {
return formattedName; return formattedName;
} }
Future<void> fetchAddressAndName(String name) async { static Future<OpenaliasRecord> fetchAddressAndName(String formattedName) async {
this.name = name; String address = formattedName;
address = name; String name = formattedName;
if (formattedName.contains(".")) {
try { try {
final txtRecord = await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true); final txtRecord = await DnsUtils.lookupRecord(formattedName, RRecordType.TXT, dnssec: true);
if (txtRecord != null) { if (txtRecord != null) {
@ -45,7 +43,7 @@ class OpenaliasRecord {
.replaceAll("(", "").replaceAll(")", "").trim(); .replaceAll("(", "").replaceAll(")", "").trim();
if (recipientName.isNotEmpty) { if (recipientName.isNotEmpty) {
this.name = recipientName.replaceAll("recipient_name=", ""); name = recipientName.replaceAll("recipient_name=", "");
} }
break; break;
@ -57,5 +55,8 @@ class OpenaliasRecord {
} }
} }
return OpenaliasRecord(address: address, name: name);
}
} }

View file

@ -65,7 +65,7 @@ class SendFormState extends State<SendForm> {
super.initState(); super.initState();
} }
void getOpenaliasRecord(BuildContext context) async { Future<void> getOpenaliasRecord(BuildContext context) async {
final sendStore = Provider.of<SendStore>(context); final sendStore = Provider.of<SendStore>(context);
final isOpenalias = await sendStore.isOpenaliasRecord(_addressController.text); final isOpenalias = await sendStore.isOpenaliasRecord(_addressController.text);

View file

@ -56,7 +56,6 @@ abstract class SendStoreBase with Store {
NumberFormat _cryptoNumberFormat; NumberFormat _cryptoNumberFormat;
NumberFormat _fiatNumberFormat; NumberFormat _fiatNumberFormat;
String _lastRecipientAddress; String _lastRecipientAddress;
OpenaliasRecord _openaliasRecord;
@action @action
Future createTransaction( Future createTransaction(
@ -159,11 +158,11 @@ abstract class SendStoreBase with Store {
} }
Future<bool> isOpenaliasRecord(String name) async { Future<bool> isOpenaliasRecord(String name) async {
_openaliasRecord = OpenaliasRecord(name: name); final _openaliasRecord = await OpenaliasRecord
await _openaliasRecord.fetchAddressAndName(_openaliasRecord.formatDomainName()); .fetchAddressAndName(OpenaliasRecord.formatDomainName(name));
recordAddress = _openaliasRecord.recordAddress; recordAddress = _openaliasRecord.address;
recordName = _openaliasRecord.recordName; recordName = _openaliasRecord.name;
return recordAddress != name; return recordAddress != name;
} }