diff --git a/lib/src/domain/common/openalias.dart b/lib/src/domain/common/openalias.dart deleted file mode 100644 index 8f567bce6..000000000 --- a/lib/src/domain/common/openalias.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'package:basic_utils/basic_utils.dart'; - -String formatDomainName(String name) { - String formattedName = name; - - if (name.contains(".")) { - formattedName = name.replaceAll("@", "."); - } - - return formattedName; -} - -Future> fetchXmrAddressAndRecipientName(String name) async { - final map = {"recipient_address" : name, "recipient_name" : name}; - - try { - await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true).then((txtRecord) { - if (txtRecord != null) { - String record; - - for (int i = 0; i < txtRecord.length; i++) { - record = txtRecord[i].data; - - if (record.contains("oa1:xmr") && record.contains("recipient_address")) { - record = record.replaceAll('\"', ""); - - final dataList = record.split(";"); - - map["recipient_address"] = dataList.where((item) => (item.contains("recipient_address"))) - .toString().replaceAll("oa1:xmr recipient_address=", "") - .replaceAll("(", "").replaceAll(")", "").trim(); - - final recipientName = dataList.where((item) => (item.contains("recipient_name"))).toString() - .replaceAll("(", "").replaceAll(")", "").trim(); - - if (recipientName.isNotEmpty) { - map["recipient_name"] = recipientName.replaceAll("recipient_name=", ""); - } - - break; - } - } - } - }); - } catch (e) { - print("${e.toString()}"); - } - - return map; -} \ No newline at end of file diff --git a/lib/src/domain/common/openalias_record.dart b/lib/src/domain/common/openalias_record.dart new file mode 100644 index 000000000..dffc8487a --- /dev/null +++ b/lib/src/domain/common/openalias_record.dart @@ -0,0 +1,61 @@ +import 'package:basic_utils/basic_utils.dart'; + +class OpenaliasRecord { + + OpenaliasRecord({this.name}); + + String name; + String address; + + String get recordName => name; + String get recordAddress => address; + + String formatDomainName() { + String formattedName = name; + + if (name.contains("@")) { + formattedName = name.replaceAll("@", "."); + } + + return formattedName; + } + + Future fetchAddressAndName(String name) async { + this.name = name; + address = name; + + try { + final txtRecord = await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true); + + if (txtRecord != null) { + + for (RRecord element in txtRecord) { + String record = element.data; + + if (record.contains("oa1:xmr") && record.contains("recipient_address")) { + record = record.replaceAll('\"', ""); + + final dataList = record.split(";"); + + address = dataList.where((item) => (item.contains("recipient_address"))) + .toString().replaceAll("oa1:xmr recipient_address=", "") + .replaceAll("(", "").replaceAll(")", "").trim(); + + final recipientName = dataList.where((item) => (item.contains("recipient_name"))).toString() + .replaceAll("(", "").replaceAll(")", "").trim(); + + if (recipientName.isNotEmpty) { + this.name = recipientName.replaceAll("recipient_name=", ""); + } + + break; + } + } + } + } catch (e) { + print("${e.toString()}"); + } + } + +} + diff --git a/lib/src/screens/send/send_page.dart b/lib/src/screens/send/send_page.dart index 94b2dd739..a77498648 100644 --- a/lib/src/screens/send/send_page.dart +++ b/lib/src/screens/send/send_page.dart @@ -22,7 +22,6 @@ import 'package:cake_wallet/src/domain/common/calculate_estimated_fee.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/domain/common/sync_status.dart'; import 'package:cake_wallet/src/stores/sync/sync_store.dart'; -import 'package:cake_wallet/src/domain/common/openalias.dart'; class SendPage extends BasePage { @override @@ -57,34 +56,38 @@ class SendFormState extends State { @override void initState() { - _focusNode.addListener(() async { + _focusNode.addListener(() { if (!_focusNode.hasFocus &&_addressController.text.isNotEmpty) { - await fetchXmrAddressAndRecipientName(formatDomainName(_addressController.text)).then((map) async { - - if (_addressController.text != map["recipient_address"]) { - _addressController.text = map["recipient_address"]; - - await showDialog( - context: context, - builder: (BuildContext context) { - return AlertDialog( - title: Text(S.of(context).openalias_alert_title), - content: Text(S.of(context).openalias_alert_content(map["recipient_name"])), - actions: [ - FlatButton( - child: Text(S.of(context).ok), - onPressed: () => Navigator.of(context).pop()) - ], - ); - }); - } - }); + getOpenaliasRecord(context); } }); super.initState(); } + void getOpenaliasRecord(BuildContext context) async { + final sendStore = Provider.of(context); + final isOpenalias = await sendStore.isOpenaliasRecord(_addressController.text); + + if (isOpenalias) { + _addressController.text = sendStore.recordAddress; + + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text(S.of(context).openalias_alert_title), + content: Text(S.of(context).openalias_alert_content(sendStore.recordName)), + actions: [ + FlatButton( + child: Text(S.of(context).ok), + onPressed: () => Navigator.of(context).pop()) + ], + ); + }); + } + } + @override Widget build(BuildContext context) { final settingsStore = Provider.of(context); diff --git a/lib/src/stores/send/send_store.dart b/lib/src/stores/send/send_store.dart index dbad353c4..5f6bd415a 100644 --- a/lib/src/stores/send/send_store.dart +++ b/lib/src/stores/send/send_store.dart @@ -11,6 +11,7 @@ import 'package:cake_wallet/src/stores/price/price_store.dart'; import 'package:cake_wallet/src/stores/send/sending_state.dart'; import 'package:cake_wallet/src/stores/settings/settings_store.dart'; import 'package:cake_wallet/generated/i18n.dart'; +import 'package:cake_wallet/src/domain/common/openalias_record.dart'; part 'send_store.g.dart'; @@ -32,6 +33,8 @@ abstract class SendStoreBase with Store { SettingsStore settingsStore; PriceStore priceStore; Box transactionDescriptions; + String recordName; + String recordAddress; @observable SendingState state; @@ -53,6 +56,7 @@ abstract class SendStoreBase with Store { NumberFormat _cryptoNumberFormat; NumberFormat _fiatNumberFormat; String _lastRecipientAddress; + OpenaliasRecord _openaliasRecord; @action Future createTransaction( @@ -154,6 +158,16 @@ abstract class SendStoreBase with Store { } } + Future isOpenaliasRecord(String name) async { + _openaliasRecord = OpenaliasRecord(name: name); + await _openaliasRecord.fetchAddressAndName(_openaliasRecord.formatDomainName()); + + recordAddress = _openaliasRecord.recordAddress; + recordName = _openaliasRecord.recordName; + + return recordAddress != name; + } + void validateAddress(String value, {CryptoCurrency cryptoCurrency}) { // XMR (95, 106), ADA (59, 92, 105), BCH (42), BNB (42), BTC (34, 42), DASH (34), EOS (42), // ETH (42), LTC (34), NANO (64, 65), TRX (34), USDT (42), XLM (56), XRP (34)