mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-23 03:59:23 +00:00
Merge pull request #235 from cake-tech/CW-4-update-open-alias-resolver
update logic for Crypto address resolving
This commit is contained in:
commit
505c568a57
4 changed files with 81 additions and 45 deletions
|
@ -1,11 +1,17 @@
|
|||
import 'package:basic_utils/basic_utils.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class OpenaliasRecord {
|
||||
|
||||
OpenaliasRecord({this.address, this.name});
|
||||
OpenaliasRecord({
|
||||
this.address,
|
||||
this.name,
|
||||
this.description,
|
||||
});
|
||||
|
||||
final String name;
|
||||
final String address;
|
||||
final String description;
|
||||
|
||||
static String formatDomainName(String name) {
|
||||
String formattedName = name;
|
||||
|
@ -17,35 +23,60 @@ class OpenaliasRecord {
|
|||
return formattedName;
|
||||
}
|
||||
|
||||
static Future<OpenaliasRecord> fetchAddressAndName(String formattedName) async {
|
||||
static Future<OpenaliasRecord> fetchAddressAndName({
|
||||
@required String formattedName,
|
||||
@required String ticker,
|
||||
}) async {
|
||||
String address = formattedName;
|
||||
String name = formattedName;
|
||||
String note = '';
|
||||
|
||||
if (formattedName.contains(".")) {
|
||||
try {
|
||||
final txtRecord = await DnsUtils.lookupRecord(formattedName, RRecordType.TXT, dnssec: true);
|
||||
final txtRecord = await DnsUtils.lookupRecord(
|
||||
formattedName, 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")) {
|
||||
if (record.contains("oa1:$ticker") &&
|
||||
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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -55,8 +86,6 @@ class OpenaliasRecord {
|
|||
}
|
||||
}
|
||||
|
||||
return OpenaliasRecord(address: address, name: name);
|
||||
return OpenaliasRecord(address: address, name: name, description: note);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ 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/store/yat/yat_store.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
|
||||
const unstoppableDomains = [
|
||||
'crypto',
|
||||
|
@ -13,7 +14,8 @@ const unstoppableDomains = [
|
|||
'888',
|
||||
'nft',
|
||||
'dao',
|
||||
'blockchain'];
|
||||
'blockchain'
|
||||
];
|
||||
|
||||
Future<ParsedAddress> parseAddressFromDomain(
|
||||
String domain, String ticker) async {
|
||||
|
@ -28,22 +30,18 @@ Future<ParsedAddress> parseAddressFromDomain(
|
|||
|
||||
if (addresses?.isEmpty ?? true) {
|
||||
return ParsedAddress(
|
||||
addresses: [domain],
|
||||
parseFrom: ParseFrom.yatRecord);
|
||||
addresses: [domain], parseFrom: ParseFrom.yatRecord);
|
||||
}
|
||||
|
||||
return ParsedAddress(
|
||||
addresses: addresses,
|
||||
name: domain,
|
||||
parseFrom: ParseFrom.yatRecord);
|
||||
addresses: addresses, name: domain, parseFrom: ParseFrom.yatRecord);
|
||||
} catch (e) {
|
||||
return ParsedAddress(addresses: [domain]);
|
||||
}
|
||||
}
|
||||
|
||||
if (unstoppableDomains.any((domain) => name.contains(domain))) {
|
||||
final address =
|
||||
await fetchUnstoppableDomainAddress(domain, ticker);
|
||||
final address = await fetchUnstoppableDomainAddress(domain, ticker);
|
||||
|
||||
if (address?.isEmpty ?? true) {
|
||||
return ParsedAddress(addresses: [domain]);
|
||||
|
@ -55,7 +53,8 @@ Future<ParsedAddress> parseAddressFromDomain(
|
|||
parseFrom: ParseFrom.unstoppableDomains);
|
||||
}
|
||||
|
||||
final record = await OpenaliasRecord.fetchAddressAndName(formattedName);
|
||||
final record = await OpenaliasRecord.fetchAddressAndName(
|
||||
formattedName: formattedName, ticker: ticker);
|
||||
|
||||
if (record == null || record.address.contains(formattedName)) {
|
||||
return ParsedAddress(addresses: [domain]);
|
||||
|
@ -64,6 +63,7 @@ Future<ParsedAddress> parseAddressFromDomain(
|
|||
return ParsedAddress(
|
||||
addresses: [record.address],
|
||||
name: record.name,
|
||||
description: record.description,
|
||||
parseFrom: ParseFrom.openAlias);
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
enum ParseFrom {unstoppableDomains, openAlias, yatRecord, notParsed}
|
||||
enum ParseFrom { unstoppableDomains, openAlias, yatRecord, notParsed }
|
||||
|
||||
class ParsedAddress {
|
||||
ParsedAddress({
|
||||
this.addresses,
|
||||
this.name = '',
|
||||
this.parseFrom = ParseFrom.notParsed});
|
||||
this.description = '',
|
||||
this.parseFrom = ParseFrom.notParsed,
|
||||
});
|
||||
|
||||
final List<String> addresses;
|
||||
final String name;
|
||||
final String description;
|
||||
final ParseFrom parseFrom;
|
||||
}
|
|
@ -22,7 +22,7 @@ class Output = OutputBase with _$Output;
|
|||
|
||||
abstract class OutputBase with Store {
|
||||
OutputBase(this._wallet, this._settingsStore, this._fiatConversationStore)
|
||||
:_cryptoNumberFormat = NumberFormat(cryptoNumberPattern) {
|
||||
: _cryptoNumberFormat = NumberFormat(cryptoNumberPattern) {
|
||||
reset();
|
||||
_setCryptoNumMaximumFractionDigits();
|
||||
key = UniqueKey();
|
||||
|
@ -52,8 +52,9 @@ abstract class OutputBase with Store {
|
|||
String extractedAddress;
|
||||
|
||||
@computed
|
||||
bool get isParsedAddress => parsedAddress.parseFrom != ParseFrom.notParsed
|
||||
&& parsedAddress.name.isNotEmpty;
|
||||
bool get isParsedAddress =>
|
||||
parsedAddress.parseFrom != ParseFrom.notParsed &&
|
||||
parsedAddress.name.isNotEmpty;
|
||||
|
||||
@computed
|
||||
int get formattedCryptoAmount {
|
||||
|
@ -68,10 +69,12 @@ abstract class OutputBase with Store {
|
|||
_amount = monero.formatterMoneroParseAmount(amount: _cryptoAmount);
|
||||
break;
|
||||
case WalletType.bitcoin:
|
||||
_amount = bitcoin.formatterStringDoubleToBitcoinAmount(_cryptoAmount);
|
||||
_amount =
|
||||
bitcoin.formatterStringDoubleToBitcoinAmount(_cryptoAmount);
|
||||
break;
|
||||
case WalletType.litecoin:
|
||||
_amount = bitcoin.formatterStringDoubleToBitcoinAmount(_cryptoAmount);
|
||||
_amount =
|
||||
bitcoin.formatterStringDoubleToBitcoinAmount(_cryptoAmount);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -81,7 +84,7 @@ abstract class OutputBase with Store {
|
|||
amount = _amount;
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
amount = 0;
|
||||
}
|
||||
|
||||
|
@ -94,8 +97,8 @@ abstract class OutputBase with Store {
|
|||
final fee = _wallet.calculateEstimatedFee(
|
||||
_settingsStore.priority[_wallet.type], formattedCryptoAmount);
|
||||
|
||||
if (_wallet.type == WalletType.bitcoin
|
||||
|| _wallet.type == WalletType.litecoin) {
|
||||
if (_wallet.type == WalletType.bitcoin ||
|
||||
_wallet.type == WalletType.litecoin) {
|
||||
return bitcoin.formatterBitcoinAmountToDouble(amount: fee);
|
||||
}
|
||||
|
||||
|
@ -215,5 +218,6 @@ abstract class OutputBase with Store {
|
|||
final ticker = _wallet.currency.title.toLowerCase();
|
||||
parsedAddress = await parseAddressFromDomain(domain, ticker);
|
||||
extractedAddress = await extractAddressFromParsed(context, parsedAddress);
|
||||
note = parsedAddress.description;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue