Merge branch 'main' of https://github.com/cake-tech/cake_wallet into CW-282-add-trocador-exchange

This commit is contained in:
Godwin Asuquo
2023-02-06 21:48:31 +02:00
38 changed files with 377 additions and 244 deletions
+50 -52
View File
@@ -1,5 +1,4 @@
import 'package:basic_utils/basic_utils.dart';
import 'package:cw_core/wallet_type.dart';
class OpenaliasRecord {
OpenaliasRecord({
@@ -22,69 +21,68 @@ class OpenaliasRecord {
return formattedName;
}
static Future<OpenaliasRecord> fetchAddressAndName({
static Future<List<RRecord>?> lookupOpenAliasRecord(String name) async {
try {
final txtRecord = await DnsUtils.lookupRecord(name, RRecordType.TXT, dnssec: true);
return txtRecord;
} catch (e) {
print("${e.toString()}");
return null;
}
}
static OpenaliasRecord fetchAddressAndName({
required String formattedName,
required String ticker,
}) async {
required List<RRecord> txtRecord,
}) {
String address = formattedName;
String name = formattedName;
String note = '';
if (formattedName.contains(".")) {
try {
final txtRecord = await DnsUtils.lookupRecord(
formattedName, RRecordType.TXT,
dnssec: true);
for (RRecord element in txtRecord) {
String record = element.data;
if (txtRecord != null) {
for (RRecord element in txtRecord) {
String record = element.data;
if (record.contains("oa1:$ticker") && record.contains("recipient_address")) {
record = record.replaceAll('\"', "");
if (record.contains("oa1:$ticker") &&
record.contains("recipient_address")) {
record = record.replaceAll('\"', "");
final dataList = record.split(";");
final dataList = record.split(";");
address = dataList
.where((item) => (item.contains("recipient_address")))
.toString()
.replaceAll("oa1:$ticker recipient_address=", "")
.replaceAll("(", "")
.replaceAll(")", "")
.trim();
address = dataList
.where((item) => (item.contains("recipient_address")))
.toString()
.replaceAll("oa1:$ticker recipient_address=", "")
.replaceAll("(", "")
.replaceAll(")", "")
.trim();
final recipientName = dataList
.where((item) => (item.contains("recipient_name")))
.toString()
.replaceAll("(", "")
.replaceAll(")", "")
.trim();
final recipientName = dataList
.where((item) => (item.contains("recipient_name")))
.toString()
.replaceAll("(", "")
.replaceAll(")", "")
.trim();
if (recipientName.isNotEmpty) {
name = recipientName.replaceAll("recipient_name=", "");
}
final description = dataList
.where((item) => (item.contains("tx_description")))
.toString()
.replaceAll("(", "")
.replaceAll(")", "")
.trim();
if (description.isNotEmpty) {
note = description.replaceAll("tx_description=", "");
}
break;
}
}
if (recipientName.isNotEmpty) {
name = recipientName.replaceAll("recipient_name=", "");
}
} catch (e) {
print("${e.toString()}");
}
}
return OpenaliasRecord(address: address, name: name, description: note);
final description = dataList
.where((item) => (item.contains("tx_description")))
.toString()
.replaceAll("(", "")
.replaceAll(")", "")
.trim();
if (description.isNotEmpty) {
note = description.replaceAll("tx_description=", "");
}
break;
}
}
return OpenaliasRecord(address: address, name: name, description: note);
}
}
+46 -21
View File
@@ -1,40 +1,61 @@
import 'package:cake_wallet/core/address_validator.dart';
import 'package:cake_wallet/core/yat_service.dart';
import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/parsed_address.dart';
import 'package:cake_wallet/entities/unstoppable_domain_address.dart';
import 'package:cake_wallet/entities/emoji_string_extension.dart';
import 'package:cake_wallet/twitter/twitter_api.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cake_wallet/entities/fio_address_provider.dart';
class AddressResolver {
AddressResolver({required this.yatService, required this.walletType});
final YatService yatService;
final WalletType walletType;
static const unstoppableDomains = [
'crypto',
'zil',
'x',
'coin',
'wallet',
'bitcoin',
'888',
'nft',
'dao',
'blockchain'
];
'crypto',
'zil',
'x',
'coin',
'wallet',
'bitcoin',
'888',
'nft',
'dao',
'blockchain'
];
static String? extractAddressByType({required String raw, required CryptoCurrency type}) {
final addressPattern = AddressValidator.getAddressFromStringPattern(type);
if (addressPattern == null) {
throw 'Unexpected token: $type for getAddressFromStringPattern';
}
final match = RegExp(addressPattern).firstMatch(raw);
return match?.group(0)?.replaceAll(RegExp('[^0-9a-zA-Z]'), '');
}
Future<ParsedAddress> resolve(String text, String ticker) async {
try {
if (text.contains('@') && !text.contains('.')) {
if (text.startsWith('@') && !text.substring(1).contains('@')) {
final formattedName = text.substring(1);
final twitterUser = await TwitterApi.lookupUserByName(userName: formattedName);
final address = extractAddressByType(
raw: twitterUser.description ?? '', type: CryptoCurrency.fromString(ticker));
if (address != null) {
return ParsedAddress.fetchTwitterAddress(address: address, name: text);
}
}
if (!text.startsWith('@') && text.contains('@') && !text.contains('.')) {
final bool isFioRegistered = await FioAddressProvider.checkAvail(text);
if (isFioRegistered) {
final address = await FioAddressProvider.getPubAddress(text, ticker);
return ParsedAddress.fetchFioAddress(address: address, name: text);
}
}
}
if (text.hasOnlyEmojis) {
if (walletType != WalletType.haven) {
@@ -55,10 +76,14 @@ class AddressResolver {
return ParsedAddress.fetchUnstoppableDomainAddress(address: address, name: text);
}
final record = await OpenaliasRecord.fetchAddressAndName(
formattedName: formattedName, ticker: ticker);
return ParsedAddress.fetchOpenAliasAddress(record: record, name: text);
if (formattedName.contains(".")) {
final txtRecord = await OpenaliasRecord.lookupOpenAliasRecord(formattedName);
if (txtRecord != null) {
final record = await OpenaliasRecord.fetchAddressAndName(
formattedName: formattedName, ticker: ticker, txtRecord: txtRecord);
return ParsedAddress.fetchOpenAliasAddress(record: record, name: text);
}
}
} catch (e) {
print(e.toString());
}
+10 -7
View File
@@ -1,8 +1,7 @@
import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/yat_record.dart';
import 'package:flutter/material.dart';
enum ParseFrom { unstoppableDomains, openAlias, yatRecord, fio, notParsed }
enum ParseFrom { unstoppableDomains, openAlias, yatRecord, fio, notParsed, twitter }
class ParsedAddress {
ParsedAddress({
@@ -41,11 +40,7 @@ class ParsedAddress {
);
}
factory ParsedAddress.fetchOpenAliasAddress({OpenaliasRecord? record, required String name}){
final formattedName = OpenaliasRecord.formatDomainName(name);
if (record == null || record.address.contains(formattedName)) {
return ParsedAddress(addresses: [name]);
}
factory ParsedAddress.fetchOpenAliasAddress({required OpenaliasRecord record, required String name}){
return ParsedAddress(
addresses: [record.address],
name: record.name,
@@ -62,6 +57,14 @@ class ParsedAddress {
);
}
factory ParsedAddress.fetchTwitterAddress({required String address, required String name}){
return ParsedAddress(
addresses: [address],
name: name,
parseFrom: ParseFrom.twitter,
);
}
final List<String> addresses;
final String name;
final String description;