From f995342491d1517a06390038a7acc63090fa5a57 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Wed, 7 Jul 2021 16:50:55 +0300 Subject: [PATCH] CAKE-192 | parse_address_from_domain.dart fixed and split on 2 files (current and parse_address_from_domain_alert.dart), then moved to the entities directory; used one channel for native calls; removed unneeded spaces; fixed handle of unstoppable domain error in the MainActivity.java --- .../cakewallet/cake_wallet/MainActivity.java | 10 +--- ios/Runner/AppDelegate.swift | 57 +++++++------------ lib/entities/parse_address_from_domain.dart | 52 +++++++++++++++++ lib/entities/unstoppable_domain_address.dart | 4 +- lib/src/screens/exchange/exchange_page.dart | 2 +- .../send/parse_address_from_domain.dart | 53 ----------------- lib/src/screens/send/send_page.dart | 2 +- .../parse_address_from_domain_alert.dart | 17 ++++++ 8 files changed, 97 insertions(+), 100 deletions(-) create mode 100644 lib/entities/parse_address_from_domain.dart delete mode 100644 lib/src/screens/send/parse_address_from_domain.dart create mode 100644 lib/src/screens/send/widgets/parse_address_from_domain_alert.dart diff --git a/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java b/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java index fa6930a05..3afdc111e 100644 --- a/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java +++ b/android/app/src/main/java/com/cakewallet/cake_wallet/MainActivity.java @@ -21,7 +21,6 @@ import java.security.SecureRandom; public class MainActivity extends FlutterFragmentActivity { final String UTILS_CHANNEL = "com.cake_wallet/native_utils"; - final String UNSTOPPABLE_DOMAIN_CHANNEL = "com.cakewallet.cake_wallet/unstoppable-domain"; final int UNSTOPPABLE_DOMAIN_MIN_VERSION_SDK = 24; @Override @@ -33,12 +32,6 @@ public class MainActivity extends FlutterFragmentActivity { UTILS_CHANNEL); utilsChannel.setMethodCallHandler(this::handle); - - MethodChannel unstoppableDomainChannel = - new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), - UNSTOPPABLE_DOMAIN_CHANNEL); - - unstoppableDomainChannel.setMethodCallHandler(this::handle); } private void handle(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { @@ -80,7 +73,8 @@ public class MainActivity extends FlutterFragmentActivity { String address = resolution.getAddress(domain, ticker); handler.post(() -> result.success(address)); } catch (Exception e) { - handler.post(() -> result.error("INVALID DOMAIN", e.getMessage(), null)); + System.out.println("Expected Address, but got " + e.getMessage()); + handler.post(() -> result.success("")); } }); } diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 859a6e628..50b6e39dd 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -5,7 +5,7 @@ import UnstoppableDomainsResolution @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { lazy var resolution : Resolution? = { - return try? Resolution() + return try? Resolution() }() override func application( @@ -75,45 +75,32 @@ import UnstoppableDomainsResolution } result(secRandom(count: count)) + case "getUnstoppableDomainAddress": + guard let args = call.arguments as? Dictionary, + let domain = args["domain"], + let ticker = args["ticker"], + let resolution = self?.resolution else { + result(nil) + return + } + + resolution.addr(domain: domain, ticker: ticker) { addrResult in + var address : String = "" + + switch addrResult { + case .success(let returnValue): + address = returnValue + case .failure(let error): + print("Expected Address, but got \(error)") + } + + result(address) + } default: result(FlutterMethodNotImplemented) } }) - let unstoppableDomainChannel = FlutterMethodChannel(name: "com.cakewallet.cake_wallet/unstoppable-domain", binaryMessenger: controller.binaryMessenger) - unstoppableDomainChannel.setMethodCallHandler({ [weak self] - (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in - switch call.method { - case "getUnstoppableDomainAddress": - guard let args = call.arguments as? Dictionary, - let domain = args["domain"], - let ticker = args["ticker"] else { - result(nil) - return - } - - guard let resolution = self?.resolution else { - result(nil) - return - } - - resolution.addr(domain: domain, ticker: ticker) { addrResult in - var address : String = "" - - switch addrResult { - case .success(let returnValue): - address = returnValue - case .failure(let error): - print("Expected Address, but got \(error)") - } - - result(address) - } - default: - result(FlutterMethodNotImplemented) - } - }) - GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } diff --git a/lib/entities/parse_address_from_domain.dart b/lib/entities/parse_address_from_domain.dart new file mode 100644 index 000000000..b81120361 --- /dev/null +++ b/lib/entities/parse_address_from_domain.dart @@ -0,0 +1,52 @@ +import 'package:cake_wallet/entities/openalias_record.dart'; +import 'package:cake_wallet/entities/unstoppable_domain_address.dart'; +import 'package:cake_wallet/src/screens/send/widgets/parse_address_from_domain_alert.dart'; +import 'package:flutter/material.dart'; +import 'package:cake_wallet/generated/i18n.dart'; + +const topLevelDomain = 'crypto'; + +Future parseAddressFromDomain( + BuildContext context, String domain, String ticker) async { + try { + final domainParts = domain.split('.'); + final name = domainParts.last; + + if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) { + return domain; + } + + if (name.contains(topLevelDomain)) { + final address = await fetchUnstoppableDomainAddress(domain, ticker); + + if (address?.isEmpty ?? true) { + return domain; + } + + showAddressAlert( + context, + S.of(context).address_detected, + S.of(context).address_from_domain(domain)); + + return address; + } + + final record = await OpenaliasRecord.fetchAddressAndName( + OpenaliasRecord.formatDomainName(domain)); + + if (record == null || record.address.contains(domain)) { + return domain; + } + + showAddressAlert( + context, + S.of(context).openalias_alert_title, + S.of(context).openalias_alert_content(domain)); + + return record.address; + } catch (e) { + print(e.toString()); + } + + return domain; +} \ No newline at end of file diff --git a/lib/entities/unstoppable_domain_address.dart b/lib/entities/unstoppable_domain_address.dart index 494782d54..792f9a348 100644 --- a/lib/entities/unstoppable_domain_address.dart +++ b/lib/entities/unstoppable_domain_address.dart @@ -1,9 +1,9 @@ import 'package:flutter/services.dart'; -const channel = MethodChannel('com.cakewallet.cake_wallet/unstoppable-domain'); +const channel = MethodChannel('com.cake_wallet/native_utils'); Future fetchUnstoppableDomainAddress(String domain, String ticker) async { - String address; + var address = ''; try { address = await channel.invokeMethod( diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index fa69d5da2..2209fe01b 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -1,7 +1,7 @@ import 'dart:ui'; import 'package:cake_wallet/entities/sync_status.dart'; import 'package:cake_wallet/entities/wallet_type.dart'; -import 'package:cake_wallet/src/screens/send/parse_address_from_domain.dart'; +import 'package:cake_wallet/entities/parse_address_from_domain.dart'; import 'package:cake_wallet/src/widgets/standard_checkbox.dart'; import 'package:dotted_border/dotted_border.dart'; import 'package:flutter/cupertino.dart'; diff --git a/lib/src/screens/send/parse_address_from_domain.dart b/lib/src/screens/send/parse_address_from_domain.dart deleted file mode 100644 index b114ef88f..000000000 --- a/lib/src/screens/send/parse_address_from_domain.dart +++ /dev/null @@ -1,53 +0,0 @@ -import 'package:cake_wallet/entities/openalias_record.dart'; -import 'package:cake_wallet/entities/unstoppable_domain_address.dart'; -import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; -import 'package:cake_wallet/utils/show_pop_up.dart'; -import 'package:flutter/material.dart'; -import 'package:cake_wallet/generated/i18n.dart'; - -const topLevelDomain = 'crypto'; - -Future parseAddressFromDomain( - BuildContext context, String domain, String ticker) async { - try { - final name = domain.split('.').last; - - if (name.contains(topLevelDomain)) { - final address = await fetchUnstoppableDomainAddress(domain, ticker); - if (address.isNotEmpty) { - showAddressAlert( - context, - S.of(context).address_detected, - S.of(context).address_from_domain(domain)); - return address; - } - } else if (name.isNotEmpty) { - final record = await OpenaliasRecord.fetchAddressAndName( - OpenaliasRecord.formatDomainName(domain)); - if (record.name != null && record.name != domain) { - showAddressAlert( - context, - S.of(context).openalias_alert_title, - S.of(context).openalias_alert_content(domain)); - return record.address; - } - } - } catch (e) { - print(e.toString()); - } - - return domain; -} - -void showAddressAlert(BuildContext context, String title, String content) async { - await showPopUp( - context: context, - builder: (BuildContext context) { - - return AlertWithOneAction( - alertTitle: title, - alertContent: content, - buttonText: S.of(context).ok, - buttonAction: () => Navigator.of(context).pop()); - }); -} \ 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 92b6c54d1..3d7e011e1 100644 --- a/lib/src/screens/send/send_page.dart +++ b/lib/src/screens/send/send_page.dart @@ -1,6 +1,6 @@ import 'dart:ui'; import 'package:cake_wallet/entities/transaction_priority.dart'; -import 'package:cake_wallet/src/screens/send/parse_address_from_domain.dart'; +import 'package:cake_wallet/entities/parse_address_from_domain.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; import 'package:cake_wallet/src/widgets/keyboard_done_button.dart'; import 'package:cake_wallet/src/widgets/picker.dart'; diff --git a/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart b/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart new file mode 100644 index 000000000..760091611 --- /dev/null +++ b/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart @@ -0,0 +1,17 @@ +import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; +import 'package:cake_wallet/utils/show_pop_up.dart'; +import 'package:flutter/material.dart'; +import 'package:cake_wallet/generated/i18n.dart'; + +void showAddressAlert(BuildContext context, String title, String content) async { + await showPopUp( + context: context, + builder: (BuildContext context) { + + return AlertWithOneAction( + alertTitle: title, + alertContent: content, + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop()); + }); +} \ No newline at end of file