From 91091a8d19c3ac7a4dfb21a87c19ec3d68c3f8e4 Mon Sep 17 00:00:00 2001 From: Oleksandr Sobol Date: Thu, 20 Feb 2020 22:50:22 +0200 Subject: [PATCH] CWA-69 | added focusNode to address_text_field, got recipient name in the openalias, added focusNode listener in the send_page --- lib/src/domain/common/openalias.dart | 31 ++++++++++++++------ lib/src/screens/send/send_page.dart | 39 +++++++++++++++++++++---- lib/src/widgets/address_text_field.dart | 3 ++ 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/lib/src/domain/common/openalias.dart b/lib/src/domain/common/openalias.dart index bdbb224f4..73afb6a3d 100644 --- a/lib/src/domain/common/openalias.dart +++ b/lib/src/domain/common/openalias.dart @@ -1,15 +1,17 @@ import 'package:basic_utils/basic_utils.dart'; String formatDomainName(String name) { - if (!name.contains(".")) { - return ""; + String formattedName = name; + + if (name.contains(".")) { + formattedName = name.replaceAll("@", "."); } - return name.replaceAll("@", "."); + return formattedName; } -Future fetchXmrAddress(String name) async { - String xmrAddress = ""; +Future> fetchXmrAddressAndRecipientName(String name) async { + final map = {"recipient_address" : name, "recipient_name" : name}; await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true).then((txtRecord) { if (txtRecord != null) { @@ -20,14 +22,25 @@ Future fetchXmrAddress(String name) async { if (record.contains("oa1:xmr") && record.contains("recipient_address")) { record = record.replaceAll('\"', ""); - xmrAddress = record.split(" ").where((item) => (item.contains("recipient_address"))) - .toString().replaceAll("recipient_address=", "").replaceAll("\;", "") - .replaceAll("(", "").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(); + + if (recipientName.isNotEmpty) { + map["recipient_name"] = recipientName.replaceAll("recipient_name=", "") + .replaceAll("(", "").replaceAll(")", "").trim(); + } + break; } } } }); - return xmrAddress; + return map; } \ No newline at end of file diff --git a/lib/src/screens/send/send_page.dart b/lib/src/screens/send/send_page.dart index a3cb73c58..33f2eb254 100644 --- a/lib/src/screens/send/send_page.dart +++ b/lib/src/screens/send/send_page.dart @@ -49,10 +49,42 @@ class SendFormState extends State { final _cryptoAmountController = TextEditingController(); final _fiatAmountController = TextEditingController(); + final _focusNode = FocusNode(); + bool _effectsInstalled = false; final _formKey = GlobalKey(); + @override + void initState() { + _focusNode.addListener(() async { + if (_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("XMR Recipient Detected"), + content: Text("You will be sending funds to\n${map["recipient_name"]}"), + actions: [ + FlatButton( + child: Text(S.of(context).ok), + onPressed: () => Navigator.of(context).pop()) + ], + ); + }); + } + }); + } + }); + + super.initState(); + } + @override Widget build(BuildContext context) { final settingsStore = Provider.of(context); @@ -151,6 +183,7 @@ class SendFormState extends State { AddressTextField( controller: _addressController, placeholder: S.of(context).send_monero_address, + focusNode: _focusNode, onURIScanned: (uri) { var address = ''; var amount = ''; @@ -359,12 +392,6 @@ class SendFormState extends State { // Hack. Don't ask me. FocusScope.of(context).requestFocus(FocusNode()); - final domainName = formatDomainName(_addressController.text); - - if (domainName != "") { - await fetchXmrAddress(domainName).then((address) => _addressController.text = address); - } - if (_formKey.currentState.validate()) { await showDialog( context: context, diff --git a/lib/src/widgets/address_text_field.dart b/lib/src/widgets/address_text_field.dart index c457ee485..a13132e55 100644 --- a/lib/src/widgets/address_text_field.dart +++ b/lib/src/widgets/address_text_field.dart @@ -18,6 +18,7 @@ class AddressTextField extends StatelessWidget { AddressTextFieldOption.addressBook ], this.onURIScanned, + this.focusNode, this.validator}); static const prefixIconWidth = 34.0; @@ -30,6 +31,7 @@ class AddressTextField extends StatelessWidget { final Function(Uri) onURIScanned; final List options; final FormFieldValidator validator; + FocusNode focusNode; @override Widget build(BuildContext context) { @@ -37,6 +39,7 @@ class AddressTextField extends StatelessWidget { onFieldSubmitted: (_) => FocusScope.of(context).unfocus(), enabled: isActive, controller: controller, + focusNode: focusNode, decoration: InputDecoration( suffixIcon: SizedBox( width: prefixIconWidth * options.length +