CWA-69 | reworked OpenaliasRecord class

Oleksandr Sobol committed Feb 25, 2020 at 14:45 UTC 652ba167774cf744bfc7367e529b3482afe448de
3 files changed +35 -35
lib/src/domain/common/openalias_record.dart
+30 -29
@@ -2,15 +2,12 @@ import 'package:basic_utils/basic_utils.dart';
2
3 class OpenaliasRecord {
4
5 - OpenaliasRecord({this.name});
5 + OpenaliasRecord({this.address, this.name});
6
7 - String name;
8 - String address;
7 + final String name;
8 + final String address;
9
10 - String get recordName => name;
11 - String get recordAddress => address;
12 -
13 - String formatDomainName() {
10 + static String formatDomainName(String name) {
11 String formattedName = name;
12
13 if (name.contains("@")) {
@@ -20,41 +17,45 @@ class OpenaliasRecord {
17 return formattedName;
18 }
19
23 - Future<void> fetchAddressAndName(String name) async {
24 - this.name = name;
25 - address = name;
20 + static Future<OpenaliasRecord> fetchAddressAndName(String formattedName) async {
21 + String address = formattedName;
22 + String name = formattedName;
23
27 - try {
28 - final txtRecord = await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true);
24 + if (formattedName.contains(".")) {
25 + try {
26 + final txtRecord = await DnsUtils.lookupRecord(formattedName, RRecordType.TXT, dnssec: true);
27
30 - if (txtRecord != null) {
28 + if (txtRecord != null) {
29
32 - for (RRecord element in txtRecord) {
33 - String record = element.data;
30 + for (RRecord element in txtRecord) {
31 + String record = element.data;
32
35 - if (record.contains("oa1:xmr") && record.contains("recipient_address")) {
36 - record = record.replaceAll('\"', "");
33 + if (record.contains("oa1:xmr") && record.contains("recipient_address")) {
34 + record = record.replaceAll('\"', "");
35
38 - final dataList = record.split(";");
36 + final dataList = record.split(";");
37
40 - address = dataList.where((item) => (item.contains("recipient_address")))
41 - .toString().replaceAll("oa1:xmr recipient_address=", "")
42 - .replaceAll("(", "").replaceAll(")", "").trim();
38 + address = dataList.where((item) => (item.contains("recipient_address")))
39 + .toString().replaceAll("oa1:xmr recipient_address=", "")
40 + .replaceAll("(", "").replaceAll(")", "").trim();
41
44 - final recipientName = dataList.where((item) => (item.contains("recipient_name"))).toString()
45 - .replaceAll("(", "").replaceAll(")", "").trim();
42 + final recipientName = dataList.where((item) => (item.contains("recipient_name"))).toString()
43 + .replaceAll("(", "").replaceAll(")", "").trim();
44
47 - if (recipientName.isNotEmpty) {
48 - this.name = recipientName.replaceAll("recipient_name=", "");
49 - }
45 + if (recipientName.isNotEmpty) {
46 + name = recipientName.replaceAll("recipient_name=", "");
47 + }
48
51 - break;
49 + break;
50 + }
51 }
52 }
53 + } catch (e) {
54 + print("${e.toString()}");
55 }
55 - } catch (e) {
56 - print("${e.toString()}");
56 }
57 +
58 + return OpenaliasRecord(address: address, name: name);
59 }
60
61 }
lib/src/screens/send/send_page.dart
+1 -1
@@ -65,7 +65,7 @@ class SendFormState extends State<SendForm> {
65 super.initState();
66 }
67
68 - void getOpenaliasRecord(BuildContext context) async {
68 + Future<void> getOpenaliasRecord(BuildContext context) async {
69 final sendStore = Provider.of<SendStore>(context);
70 final isOpenalias = await sendStore.isOpenaliasRecord(_addressController.text);
71
lib/src/stores/send/send_store.dart
+4 -5
@@ -56,7 +56,6 @@ abstract class SendStoreBase with Store {
56 NumberFormat _cryptoNumberFormat;
57 NumberFormat _fiatNumberFormat;
58 String _lastRecipientAddress;
59 - OpenaliasRecord _openaliasRecord;
59
60 @action
61 Future createTransaction(
@@ -159,11 +158,11 @@ abstract class SendStoreBase with Store {
158 }
159
160 Future<bool> isOpenaliasRecord(String name) async {
162 - _openaliasRecord = OpenaliasRecord(name: name);
163 - await _openaliasRecord.fetchAddressAndName(_openaliasRecord.formatDomainName());
161 + final _openaliasRecord = await OpenaliasRecord
162 + .fetchAddressAndName(OpenaliasRecord.formatDomainName(name));
163
165 - recordAddress = _openaliasRecord.recordAddress;
166 - recordName = _openaliasRecord.recordName;
164 + recordAddress = _openaliasRecord.address;
165 + recordName = _openaliasRecord.name;
166
167 return recordAddress != name;
168 }