fix openAlias address resolv

Serhii committed Jan 31, 2023 at 21:39 UTC ef01554fdbfbf47bc042aaf42403a8aa84526bde
23 files changed +83 -103
lib/entities/openalias_record.dart
+54 -56
@@ -1,5 +1,4 @@
1 import 'package:basic_utils/basic_utils.dart';
2 -import 'package:cw_core/wallet_type.dart';
2
3 class OpenaliasRecord {
4 OpenaliasRecord({
@@ -22,69 +21,68 @@ class OpenaliasRecord {
21 return formattedName;
22 }
23
25 - static Future<OpenaliasRecord> fetchAddressAndName({
24 + static Future<List<RRecord>?> lookupOpenAliasRecord(String name) async {
25 + try {
26 + final txtRecord = await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true);
27 +
28 + return txtRecord;
29 + } catch (e) {
30 + print("${e.toString()}");
31 + return null;
32 + }
33 + }
34 +
35 + static OpenaliasRecord fetchAddressAndName({
36 required String formattedName,
37 required String ticker,
28 - }) async {
38 + required List<RRecord> txtRecord,
39 + }) {
40 String address = formattedName;
41 String name = formattedName;
42 String note = '';
43
33 - if (formattedName.contains(".")) {
34 - try {
35 - final txtRecord = await DnsUtils.lookupRecord(
36 - formattedName, RRecordType.TXT,
37 - dnssec: true);
38 -
39 - if (txtRecord != null) {
40 - for (RRecord element in txtRecord) {
41 - String record = element.data;
42 -
43 - if (record.contains("oa1:$ticker") &&
44 - record.contains("recipient_address")) {
45 - record = record.replaceAll('\"', "");
46 -
47 - final dataList = record.split(";");
48 -
49 - address = dataList
50 - .where((item) => (item.contains("recipient_address")))
51 - .toString()
52 - .replaceAll("oa1:$ticker recipient_address=", "")
53 - .replaceAll("(", "")
54 - .replaceAll(")", "")
55 - .trim();
56 -
57 - final recipientName = dataList
58 - .where((item) => (item.contains("recipient_name")))
59 - .toString()
60 - .replaceAll("(", "")
61 - .replaceAll(")", "")
62 - .trim();
63 -
64 - if (recipientName.isNotEmpty) {
65 - name = recipientName.replaceAll("recipient_name=", "");
66 - }
67 -
68 - final description = dataList
69 - .where((item) => (item.contains("tx_description")))
70 - .toString()
71 - .replaceAll("(", "")
72 - .replaceAll(")", "")
73 - .trim();
74 -
75 - if (description.isNotEmpty) {
76 - note = description.replaceAll("tx_description=", "");
77 - }
78 -
79 - break;
80 - }
81 - }
44 + for (RRecord element in txtRecord) {
45 + String record = element.data;
46 +
47 + if (record.contains("oa1:$ticker") && record.contains("recipient_address")) {
48 + record = record.replaceAll('\"', "");
49 +
50 + final dataList = record.split(";");
51 +
52 + address = dataList
53 + .where((item) => (item.contains("recipient_address")))
54 + .toString()
55 + .replaceAll("oa1:$ticker recipient_address=", "")
56 + .replaceAll("(", "")
57 + .replaceAll(")", "")
58 + .trim();
59 +
60 + final recipientName = dataList
61 + .where((item) => (item.contains("recipient_name")))
62 + .toString()
63 + .replaceAll("(", "")
64 + .replaceAll(")", "")
65 + .trim();
66 +
67 + if (recipientName.isNotEmpty) {
68 + name = recipientName.replaceAll("recipient_name=", "");
69 }
83 - } catch (e) {
84 - print("${e.toString()}");
70 +
71 + final description = dataList
72 + .where((item) => (item.contains("tx_description")))
73 + .toString()
74 + .replaceAll("(", "")
75 + .replaceAll(")", "")
76 + .trim();
77 +
78 + if (description.isNotEmpty) {
79 + note = description.replaceAll("tx_description=", "");
80 + }
81 +
82 + break;
83 }
86 - }
84
88 - return OpenaliasRecord(address: address, name: name, description: note);
85 }
86 + return OpenaliasRecord(address: address, name: name, description: note);
87 +}
88 }
lib/entities/parse_address_from_domain.dart
+25 -20
@@ -11,21 +11,22 @@ import 'package:cake_wallet/entities/fio_address_provider.dart';
11
12 class AddressResolver {
13 AddressResolver({required this.yatService, required this.walletType});
14 +
15 final YatService yatService;
16 final WalletType walletType;
17
18 static const unstoppableDomains = [
18 - 'crypto',
19 - 'zil',
20 - 'x',
21 - 'coin',
22 - 'wallet',
23 - 'bitcoin',
24 - '888',
25 - 'nft',
26 - 'dao',
27 - 'blockchain'
28 -];
19 + 'crypto',
20 + 'zil',
21 + 'x',
22 + 'coin',
23 + 'wallet',
24 + 'bitcoin',
25 + '888',
26 + 'nft',
27 + 'dao',
28 + 'blockchain'
29 + ];
30
31 static String? extractAddressByType({required String raw, required CryptoCurrency type}) {
32 final addressPattern = AddressValidator.getAddressFromStringPattern(type);
@@ -43,18 +44,18 @@ class AddressResolver {
44 if (text.startsWith('@') && !text.substring(1).contains('@')) {
45 final formattedName = text.substring(1);
46 final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName);
46 - final address = extractAddressByType(raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker));
47 + final address = extractAddressByType(
48 + raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker));
49 if (address != null) {
48 - return ParsedAddress.fetchTwitterAddress(address: address, name: '$text (Twitter)');
50 + return ParsedAddress.fetchTwitterAddress(address: address, name: text);
51 }
52 }
53 if (!text.startsWith('@') && text.contains('@') && !text.contains('.')) {
54 final bool isFioRegistered = await FioAddressProvider.checkAvail(text);
55 if (isFioRegistered) {
56 final address = await FioAddressProvider.getPubAddress(text, ticker);
55 - return ParsedAddress.fetchFioAddress(address: address, name: '$text (FIO)');
56 - }
57 -
57 + return ParsedAddress.fetchFioAddress(address: address, name: text);
58 + }
59 }
60 if (text.hasOnlyEmojis) {
61 if (walletType != WalletType.haven) {
@@ -75,10 +76,14 @@ class AddressResolver {
76 return ParsedAddress.fetchUnstoppableDomainAddress(address: address, name: text);
77 }
78
78 - final record = await OpenaliasRecord.fetchAddressAndName(
79 - formattedName: formattedName, ticker: ticker);
80 - return ParsedAddress.fetchOpenAliasAddress(record: record, name: '$text (OpenAlias)');
81 -
79 + if (formattedName.contains(".")) {
80 + final txtRecord = await OpenaliasRecord.lookupOpenAliasRecord(formattedName);
81 + if (txtRecord != null) {
82 + final record = await OpenaliasRecord.fetchAddressAndName(
83 + formattedName: formattedName, ticker: ticker, txtRecord: txtRecord);
84 + return ParsedAddress.fetchOpenAliasAddress(record: record, name: text);
85 + }
86 + }
87 } catch (e) {
88 print(e.toString());
89 }
lib/entities/parsed_address.dart
+1 -5
@@ -40,11 +40,7 @@ class ParsedAddress {
40 );
41 }
42
43 - factory ParsedAddress.fetchOpenAliasAddress({OpenaliasRecord? record, required String name}){
44 - final formattedName = OpenaliasRecord.formatDomainName(name);
45 - if (record == null || record.address.contains(formattedName)) {
46 - return ParsedAddress(addresses: [name]);
47 - }
43 + factory ParsedAddress.fetchOpenAliasAddress({required OpenaliasRecord record, required String name}){
44 return ParsedAddress(
45 addresses: [record.address],
46 name: record.name,
lib/src/screens/send/widgets/extract_address_from_parsed.dart
+3 -3
@@ -20,17 +20,17 @@ Future<String> extractAddressFromParsed(
20 break;
21 case ParseFrom.openAlias:
22 title = S.of(context).address_detected;
23 - content = S.of(context).openalias_alert_content(parsedAddress.name);
23 + content = S.of(context).openalias_alert_content('${parsedAddress.name} (OpenAlias)');
24 address = parsedAddress.addresses.first;
25 break;
26 case ParseFrom.fio:
27 title = S.of(context).address_detected;
28 - content = S.of(context).openalias_alert_content(parsedAddress.name);
28 + content = S.of(context).openalias_alert_content('${parsedAddress.name} (FIO)');
29 address = parsedAddress.addresses.first;
30 break;
31 case ParseFrom.twitter:
32 title = S.of(context).address_detected;
33 - content = S.of(context).openalias_alert_content(parsedAddress.name);
33 + content = S.of(context).openalias_alert_content('${parsedAddress.name} (Twitter)');
34 address = parsedAddress.addresses.first;
35 break;
36 case ParseFrom.yatRecord:
res/values/strings_ar.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason":"امسح بصمة إصبعك للمصادقة",
399 "version":"الإصدار ${currentVersion}",
400
401 - "openalias_alert_title":"تم ايجاد العنوان",
401 "openalias_alert_content":"سوف ترسل الأموال إلى\n${recipient_name}",
402
403 "card_address":"العنوان:",
res/values/strings_de.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Scannen Sie Ihren Fingerabdruck zur Authentifizierung",
399 "version" : "Version ${currentVersion}",
400
401 - "openalias_alert_title" : "Adresse Erkannt",
401 "openalias_alert_content" : "Sie senden Geld an\n${recipient_name}",
402
403 "card_address" : "Adresse:",
res/values/strings_en.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Scan your fingerprint to authenticate",
399 "version" : "Version ${currentVersion}",
400
401 - "openalias_alert_title" : "Address Detected",
401 "openalias_alert_content" : "You will be sending funds to\n${recipient_name}",
402
403 "card_address" : "Address:",
res/values/strings_es.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Escanee su huella digital para autenticar",
399 "version" : "Versión ${currentVersion}",
400
401 - "openalias_alert_title" : "Destinatario detectado",
401 "openalias_alert_content" : "Enviará fondos a\n${recipient_name}",
402
403 "card_address" : "Dirección:",
res/values/strings_fr.arb
-1
@@ -396,7 +396,6 @@
396 "biometric_auth_reason" : "Scannez votre empreinte digitale pour vous authentifier",
397 "version" : "Version ${currentVersion}",
398
399 - "openalias_alert_title" : "Adresse Détectée",
399 "openalias_alert_content" : "Vous allez envoyer des fonds à\n${recipient_name}",
400
401 "card_address" : "Adresse :",
res/values/strings_hi.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "प्रमाणित करने के लिए अपने फ़िंगरप्रिंट को स्कैन करें",
399 "version" : "संस्करण ${currentVersion}",
400
401 - "openalias_alert_title" : "पता मिला",
401 "openalias_alert_content" : "आपको धनराशि भेजी जाएगी\n${recipient_name}",
402
403 "card_address" : "पता:",
res/values/strings_hr.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Skenirajte svoj otisak prsta za autentifikaciju",
399 "version" : "Verzija ${currentVersion}",
400
401 - "openalias_alert_title" : "Otkrivena je adresa",
401 "openalias_alert_content" : "Poslat ćete sredstva primatelju\n${recipient_name}",
402
403 "card_address" : "Adresa:",
res/values/strings_it.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Scansiona la tua impronta per autenticarti",
399 "version" : "Versione ${currentVersion}",
400
401 - "openalias_alert_title" : "Indirizzo Rilevato",
401 "openalias_alert_content" : "Invierai i tuoi fondi a\n${recipient_name}",
402
403 "card_address" : "Indirizzo:",
res/values/strings_ja.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "प指紋をスキャンして認証する",
399 "version" : "バージョン ${currentVersion}",
400
401 - "openalias_alert_title" : "アドレスが検出されました",
401 "openalias_alert_content" : "に送金します\n${recipient_name}",
402
403 "card_address" : "住所:",
res/values/strings_ko.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "지문을 스캔하여 인증",
399 "version" : "버전 ${currentVersion}",
400
401 - "openalias_alert_title" : "주소 감지됨",
401 "openalias_alert_content" : "당신은에 자금을 보낼 것입니다\n${recipient_name}",
402
403 "card_address" : "주소:",
res/values/strings_my.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "စစ်မှန်ကြောင်းအထောက်အထားပြရန် သင့်လက်ဗွေကို စကန်ဖတ်ပါ။",
399 "version" : "ဗားရှင်း ${currentVersion}",
400
401 - "openalias_alert_title" : "လိပ်စာကို ရှာတွေ့သည်။",
401 "openalias_alert_content" : "သင်သည် \n${recipient_name} သို့ ရန်ပုံငွေများ ပေးပို့ပါမည်",
402
403 "card_address" : "လိပ်စာ-",
res/values/strings_nl.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Scan uw vingerafdruk om te verifiëren",
399 "version" : "Versie ${currentVersion}",
400
401 - "openalias_alert_title" : "Adres Gedetecteerd",
401 "openalias_alert_content" : "U stuurt geld naar\n${recipient_name}",
402
403 "card_address" : "Adres:",
res/values/strings_pl.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Zeskanuj swój odcisk palca, aby uwierzytelnić",
399 "version" : "Wersja ${currentVersion}",
400
401 - "openalias_alert_title" : "Wykryto Adres",
401 "openalias_alert_content" : "Wysyłasz środki na\n${recipient_name}",
402
403 "card_address" : "Adres:",
res/values/strings_pt.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Digitalize sua impressão digital para autenticar",
399 "version" : "Versão ${currentVersion}",
400
401 - "openalias_alert_title" : "Endereço Detectado",
401 "openalias_alert_content" : "Você enviará fundos para\n${recipient_name}",
402
403 "card_address" : "Endereço:",
res/values/strings_ru.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Отсканируйте свой отпечаток пальца для аутентификации",
399 "version" : "Версия ${currentVersion}",
400
401 - "openalias_alert_title" : "Адрес Обнаружен",
401 "openalias_alert_content" : "Вы будете отправлять средства\n${recipient_name}",
402
403 "card_address" : "Адрес:",
res/values/strings_th.arb
-1
@@ -396,7 +396,6 @@
396 "biometric_auth_reason" : "สแกนลายนิ้วมือของคุณเพื่อยืนยันตัวตน",
397 "version" : "เวอร์ชัน ${currentVersion}",
398
399 - "openalias_alert_title" : "พบที่อยู่",
399 "openalias_alert_content" : "คุณกำลังจะส่งเงินไปยัง\n${recipient_name}",
400
401 "card_address" : "ที่อยู่:",
res/values/strings_tr.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "Kimlik doğrulaması için parmak izini okutun",
399 "version" : "Sürüm ${currentVersion}",
400
401 - "openalias_alert_title" : "Adres tespit edildi",
401 "openalias_alert_content" : "Parayı buraya gönderceksin:\n${recipient_name}",
402
403 "card_address" : "Adres:",
res/values/strings_uk.arb
-1
@@ -397,7 +397,6 @@
397 "biometric_auth_reason" : "Відскануйте свій відбиток пальця для аутентифікації",
398 "version" : "Версія ${currentVersion}",
399
400 - "openalias_alert_title" : "Виявлено адресу",
400 "openalias_alert_content" : "Ви будете відправляти кошти\n${recipient_name}",
401
402 "card_address" : "Адреса:",
res/values/strings_zh.arb
-1
@@ -398,7 +398,6 @@
398 "biometric_auth_reason" : "扫描指纹进行身份认证",
399 "version" : "版本 ${currentVersion}",
400
401 - "openalias_alert_title" : "检测到地址",
401 "openalias_alert_content" : "您将汇款至\n${recipient_name}",
402
403 "card_address" : "地址:",