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

This commit is contained in:
Oleksandr Sobol 2020-02-25 11:28:33 +02:00
parent 28085ce057
commit 0efe8af3e0
4 changed files with 100 additions and 72 deletions

View file

@ -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<Map<String, String>> 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;
}

View file

@ -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<void> 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()}");
}
}
}

View file

@ -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<SendForm> {
@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<void>(
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: <Widget>[
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<SendStore>(context);
final isOpenalias = await sendStore.isOpenaliasRecord(_addressController.text);
if (isOpenalias) {
_addressController.text = sendStore.recordAddress;
await showDialog<void>(
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: <Widget>[
FlatButton(
child: Text(S.of(context).ok),
onPressed: () => Navigator.of(context).pop())
],
);
});
}
}
@override
Widget build(BuildContext context) {
final settingsStore = Provider.of<SettingsStore>(context);

View file

@ -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<TransactionDescription> 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<bool> 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)