CWA-173 | created OpenaliasRecord class, renamed fetchXmrAddressAndRecipientName method on fetchAddressAndName, used forEach instead of for loop in this method, checked entered xmr address in the send store

Oleksandr Sobol committed Feb 25, 2020 at 11:28 UTC 0efe8af3e0982292d07a8af9a105221313e0de7a
4 files changed +100 -72
lib/src/domain/common/openalias.dart deleted
-50
@@ -1,50 +0,0 @@
1 -import 'package:basic_utils/basic_utils.dart';
2 -
3 -String formatDomainName(String name) {
4 - String formattedName = name;
5 -
6 - if (name.contains(".")) {
7 - formattedName = name.replaceAll("@", ".");
8 - }
9 -
10 - return formattedName;
11 -}
12 -
13 -Future<Map<String, String>> fetchXmrAddressAndRecipientName(String name) async {
14 - final map = {"recipient_address" : name, "recipient_name" : name};
15 -
16 - try {
17 - await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true).then((txtRecord) {
18 - if (txtRecord != null) {
19 - String record;
20 -
21 - for (int i = 0; i < txtRecord.length; i++) {
22 - record = txtRecord[i].data;
23 -
24 - if (record.contains("oa1:xmr") && record.contains("recipient_address")) {
25 - record = record.replaceAll('\"', "");
26 -
27 - final dataList = record.split(";");
28 -
29 - map["recipient_address"] = dataList.where((item) => (item.contains("recipient_address")))
30 - .toString().replaceAll("oa1:xmr recipient_address=", "")
31 - .replaceAll("(", "").replaceAll(")", "").trim();
32 -
33 - final recipientName = dataList.where((item) => (item.contains("recipient_name"))).toString()
34 - .replaceAll("(", "").replaceAll(")", "").trim();
35 -
36 - if (recipientName.isNotEmpty) {
37 - map["recipient_name"] = recipientName.replaceAll("recipient_name=", "");
38 - }
39 -
40 - break;
41 - }
42 - }
43 - }
44 - });
45 - } catch (e) {
46 - print("${e.toString()}");
47 - }
48 -
49 - return map;
50 -}
\ No newline at end of file
lib/src/domain/common/openalias_record.dart new
+61
@@ -0,0 +1,61 @@
1 +import 'package:basic_utils/basic_utils.dart';
2 +
3 +class OpenaliasRecord {
4 +
5 + OpenaliasRecord({this.name});
6 +
7 + String name;
8 + String address;
9 +
10 + String get recordName => name;
11 + String get recordAddress => address;
12 +
13 + String formatDomainName() {
14 + String formattedName = name;
15 +
16 + if (name.contains("@")) {
17 + formattedName = name.replaceAll("@", ".");
18 + }
19 +
20 + return formattedName;
21 + }
22 +
23 + Future<void> fetchAddressAndName(String name) async {
24 + this.name = name;
25 + address = name;
26 +
27 + try {
28 + final txtRecord = await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true);
29 +
30 + if (txtRecord != null) {
31 +
32 + for (RRecord element in txtRecord) {
33 + String record = element.data;
34 +
35 + if (record.contains("oa1:xmr") && record.contains("recipient_address")) {
36 + record = record.replaceAll('\"', "");
37 +
38 + final dataList = record.split(";");
39 +
40 + address = dataList.where((item) => (item.contains("recipient_address")))
41 + .toString().replaceAll("oa1:xmr recipient_address=", "")
42 + .replaceAll("(", "").replaceAll(")", "").trim();
43 +
44 + final recipientName = dataList.where((item) => (item.contains("recipient_name"))).toString()
45 + .replaceAll("(", "").replaceAll(")", "").trim();
46 +
47 + if (recipientName.isNotEmpty) {
48 + this.name = recipientName.replaceAll("recipient_name=", "");
49 + }
50 +
51 + break;
52 + }
53 + }
54 + }
55 + } catch (e) {
56 + print("${e.toString()}");
57 + }
58 + }
59 +
60 +}
61 +
lib/src/screens/send/send_page.dart
+25 -22
@@ -22,7 +22,6 @@ import 'package:cake_wallet/src/domain/common/calculate_estimated_fee.dart';
22 import 'package:cake_wallet/generated/i18n.dart';
23 import 'package:cake_wallet/src/domain/common/sync_status.dart';
24 import 'package:cake_wallet/src/stores/sync/sync_store.dart';
25 -import 'package:cake_wallet/src/domain/common/openalias.dart';
25
26 class SendPage extends BasePage {
27 @override
@@ -57,34 +56,38 @@ class SendFormState extends State<SendForm> {
56
57 @override
58 void initState() {
60 - _focusNode.addListener(() async {
59 + _focusNode.addListener(() {
60 if (!_focusNode.hasFocus &&_addressController.text.isNotEmpty) {
62 - await fetchXmrAddressAndRecipientName(formatDomainName(_addressController.text)).then((map) async {
63 -
64 - if (_addressController.text != map["recipient_address"]) {
65 - _addressController.text = map["recipient_address"];
66 -
67 - await showDialog<void>(
68 - context: context,
69 - builder: (BuildContext context) {
70 - return AlertDialog(
71 - title: Text(S.of(context).openalias_alert_title),
72 - content: Text(S.of(context).openalias_alert_content(map["recipient_name"])),
73 - actions: <Widget>[
74 - FlatButton(
75 - child: Text(S.of(context).ok),
76 - onPressed: () => Navigator.of(context).pop())
77 - ],
78 - );
79 - });
80 - }
81 - });
61 + getOpenaliasRecord(context);
62 }
63 });
64
65 super.initState();
66 }
67
68 + void getOpenaliasRecord(BuildContext context) async {
69 + final sendStore = Provider.of<SendStore>(context);
70 + final isOpenalias = await sendStore.isOpenaliasRecord(_addressController.text);
71 +
72 + if (isOpenalias) {
73 + _addressController.text = sendStore.recordAddress;
74 +
75 + await showDialog<void>(
76 + context: context,
77 + builder: (BuildContext context) {
78 + return AlertDialog(
79 + title: Text(S.of(context).openalias_alert_title),
80 + content: Text(S.of(context).openalias_alert_content(sendStore.recordName)),
81 + actions: <Widget>[
82 + FlatButton(
83 + child: Text(S.of(context).ok),
84 + onPressed: () => Navigator.of(context).pop())
85 + ],
86 + );
87 + });
88 + }
89 + }
90 +
91 @override
92 Widget build(BuildContext context) {
93 final settingsStore = Provider.of<SettingsStore>(context);
lib/src/stores/send/send_store.dart
+14
@@ -11,6 +11,7 @@ import 'package:cake_wallet/src/stores/price/price_store.dart';
11 import 'package:cake_wallet/src/stores/send/sending_state.dart';
12 import 'package:cake_wallet/src/stores/settings/settings_store.dart';
13 import 'package:cake_wallet/generated/i18n.dart';
14 +import 'package:cake_wallet/src/domain/common/openalias_record.dart';
15
16 part 'send_store.g.dart';
17
@@ -32,6 +33,8 @@ abstract class SendStoreBase with Store {
33 SettingsStore settingsStore;
34 PriceStore priceStore;
35 Box<TransactionDescription> transactionDescriptions;
36 + String recordName;
37 + String recordAddress;
38
39 @observable
40 SendingState state;
@@ -53,6 +56,7 @@ abstract class SendStoreBase with Store {
56 NumberFormat _cryptoNumberFormat;
57 NumberFormat _fiatNumberFormat;
58 String _lastRecipientAddress;
59 + OpenaliasRecord _openaliasRecord;
60
61 @action
62 Future createTransaction(
@@ -154,6 +158,16 @@ abstract class SendStoreBase with Store {
158 }
159 }
160
161 + Future<bool> isOpenaliasRecord(String name) async {
162 + _openaliasRecord = OpenaliasRecord(name: name);
163 + await _openaliasRecord.fetchAddressAndName(_openaliasRecord.formatDomainName());
164 +
165 + recordAddress = _openaliasRecord.recordAddress;
166 + recordName = _openaliasRecord.recordName;
167 +
168 + return recordAddress != name;
169 + }
170 +
171 void validateAddress(String value, {CryptoCurrency cryptoCurrency}) {
172 // XMR (95, 106), ADA (59, 92, 105), BCH (42), BNB (42), BTC (34, 42), DASH (34), EOS (42),
173 // ETH (42), LTC (34), NANO (64, 65), TRX (34), USDT (42), XLM (56), XRP (34)