From c14d021f072f2c378aa6ac860b22895a36b36348 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Thu, 22 Jul 2021 19:48:26 +0300 Subject: [PATCH] CAKE-345 | merged release-4.2.4 branch into current and resolved problems --- lib/bitcoin/electrum_wallet.dart | 71 +++++++------------ lib/src/screens/exchange/exchange_page.dart | 17 +---- lib/src/screens/send/send_page.dart | 19 ++--- lib/src/screens/send/send_template_page.dart | 1 - .../parse_address_from_domain_alert.dart | 19 ++++- lib/src/screens/send/widgets/send_card.dart | 47 +++++++++--- lib/view_model/send/send_item.dart | 16 ++--- lib/view_model/send/send_view_model.dart | 4 ++ 8 files changed, 98 insertions(+), 96 deletions(-) diff --git a/lib/bitcoin/electrum_wallet.dart b/lib/bitcoin/electrum_wallet.dart index 24438feca..cf9fedd06 100644 --- a/lib/bitcoin/electrum_wallet.dart +++ b/lib/bitcoin/electrum_wallet.dart @@ -176,9 +176,10 @@ abstract class ElectrumWalletBase extends WalletBase item.sendAll).toList(); if (sendAllItems?.isNotEmpty ?? false) { - throw BitcoinTransactionWrongBalanceException(); + throw BitcoinTransactionWrongBalanceException(currency); } final nullAmountItems = sendItemList.where((item) => @@ -195,29 +196,37 @@ abstract class ElectrumWalletBase extends WalletBase - previousValue + stringDoubleToBitcoinAmount( - element.cryptoAmount.replaceAll(',', '.'))); + previousValue + stringDoubleToBitcoinAmount( + element.cryptoAmount.replaceAll(',', '.'))); + + if (credentialsAmount > allAmount) { + throw BitcoinTransactionWrongBalanceException(currency); + } amount = allAmount - credentialsAmount < minAmount - ? allAmount - : credentialsAmount; + ? allAmount + : credentialsAmount; fee = amount == allAmount - ? allAmountFee - : calculateEstimatedFee(transactionCredentials.priority, amount, - outputsCount: sendItemList.length + 1); + ? allAmountFee + : calculateEstimatedFee(transactionCredentials.priority, amount, + outputsCount: sendItemList.length + 1); } else { final sendItem = sendItemList.first; credentialsAmount = !sendItem.sendAll ? stringDoubleToBitcoinAmount( - sendItem.cryptoAmount.replaceAll(',', '.')) + sendItem.cryptoAmount.replaceAll(',', '.')) : 0; + if (credentialsAmount > allAmount) { + throw BitcoinTransactionWrongBalanceException(currency); + } + amount = sendItem.sendAll || allAmount - credentialsAmount < minAmount ? allAmount : credentialsAmount; @@ -227,35 +236,11 @@ abstract class ElectrumWalletBase extends WalletBase balance.confirmed) { - throw BitcoinTransactionWrongBalanceException(); - } - - feeAmountForPriority(transactionCredentials.priority, inputs.length, 1); - final allAmount = allInputsAmount - allAmountFee; - - final credentialsAmount = transactionCredentials.amount != null - ? stringDoubleToBitcoinAmount(transactionCredentials.amount) - : 0; - final amount = transactionCredentials.amount == null || - allAmount - credentialsAmount < minAmount - ? allAmount - : credentialsAmount; - final fee = transactionCredentials.amount == null || amount == allAmount - ? allAmountFee - : calculateEstimatedFee(transactionCredentials.priority, amount); - if (fee == 0) { throw BitcoinTransactionWrongBalanceException(currency); } - final totalAmount = amount + fee; + final totalAmount = amount + fee; if (totalAmount > balance.confirmed || totalAmount > allInputsAmount) { throw BitcoinTransactionWrongBalanceException(currency); @@ -264,8 +249,6 @@ abstract class ElectrumWalletBase extends WalletBase[ Container( - height: 445, + height: 470, child: Observer( builder: (_) { return PageView.builder( @@ -219,16 +215,9 @@ class SendPage extends BasePage { item.address = template.address; item.setCryptoAmount(template.amount); - final record = - await item.getOpenaliasRecord(); - - if (record != null) { - showAddressAlert( - context, - S.current.openalias_alert_title, - S.current - .openalias_alert_content(record.name)); - } + final parsedAddress = await item + .applyOpenaliasOrUnstoppableDomains(); + showAddressAlert(context, parsedAddress); }, onRemove: () { showPopUp( diff --git a/lib/src/screens/send/send_template_page.dart b/lib/src/screens/send/send_template_page.dart index baebe435a..27ee71d23 100644 --- a/lib/src/screens/send/send_template_page.dart +++ b/lib/src/screens/send/send_template_page.dart @@ -146,7 +146,6 @@ class SendTemplatePage extends BasePage { .primaryTextTheme .headline .decorationColor), - validator: sendTemplateViewModel.addressValidator, ), ), Padding( 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 index 760091611..526393109 100644 --- a/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart +++ b/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart @@ -1,9 +1,26 @@ +import 'package:cake_wallet/entities/parsed_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'; -void showAddressAlert(BuildContext context, String title, String content) async { +void showAddressAlert(BuildContext context, ParsedAddress parsedAddress) async { + var title = ''; + var content = ''; + + switch (parsedAddress.parseFrom) { + case ParseFrom.unstoppableDomains: + title = S.of(context).address_detected; + content = S.of(context).address_from_domain(parsedAddress.name); + break; + case ParseFrom.openAlias: + title = S.of(context).openalias_alert_title; + content = S.of(context).openalias_alert_content(parsedAddress.name); + break; + case ParseFrom.notParsed: + return; + } + await showPopUp( context: context, builder: (BuildContext context) { diff --git a/lib/src/screens/send/widgets/send_card.dart b/lib/src/screens/send/widgets/send_card.dart index 28212412e..67884ce29 100644 --- a/lib/src/screens/send/widgets/send_card.dart +++ b/lib/src/screens/send/widgets/send_card.dart @@ -1,5 +1,6 @@ import 'dart:ui'; import 'package:cake_wallet/entities/transaction_priority.dart'; +import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/src/screens/send/widgets/parse_address_from_domain_alert.dart'; import 'package:cake_wallet/src/widgets/keyboard_done_button.dart'; import 'package:cake_wallet/src/widgets/picker.dart'; @@ -79,7 +80,7 @@ class SendCardState extends State ) ]), child: Container( - height: 445, + height: 470, decoration: BoxDecoration( borderRadius: BorderRadius.only( bottomLeft: Radius.circular(24), @@ -140,6 +141,11 @@ class SendCardState extends State .primaryTextTheme .headline .decorationColor), + onPushPasteButton: (context) async { + final parsedAddress = + await item.applyOpenaliasOrUnstoppableDomains(); + showAddressAlert(context, parsedAddress); + }, validator: sendViewModel.addressValidator, ), Observer( @@ -393,7 +399,34 @@ class SendCardState extends State ], ), ), - )) + )), + if (sendViewModel.isElectrumWallet) Padding( + padding: EdgeInsets.only(top: 6), + child: GestureDetector( + onTap: () => Navigator.of(context) + .pushNamed(Routes.unspentCoinsList), + child: Container( + color: Colors.transparent, + child: Row( + mainAxisAlignment: + MainAxisAlignment.spaceBetween, + children: [ + Text( + S.of(context).coin_control, + style: TextStyle( + fontSize: 12, + fontWeight: FontWeight.w600, + color: Colors.white)), + Icon( + Icons.arrow_forward_ios, + size: 12, + color: Colors.white, + ) + ], + ) + ) + ) + ) ], ) ), @@ -485,14 +518,8 @@ class SendCardState extends State addressFocusNode.addListener(() async { if (!addressFocusNode.hasFocus && addressController.text.isNotEmpty) { - final record = await item.getOpenaliasRecord(); - - if (record != null) { - showAddressAlert( - context, - S.current.openalias_alert_title, - S.current.openalias_alert_content(record.name)); - } + final parsedAddress = await item.applyOpenaliasOrUnstoppableDomains(); + showAddressAlert(context, parsedAddress); } }); diff --git a/lib/view_model/send/send_item.dart b/lib/view_model/send/send_item.dart index aa5d93b02..983646d47 100644 --- a/lib/view_model/send/send_item.dart +++ b/lib/view_model/send/send_item.dart @@ -2,6 +2,8 @@ import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart'; import 'package:cake_wallet/bitcoin/electrum_wallet.dart'; import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart'; import 'package:cake_wallet/entities/openalias_record.dart'; +import 'package:cake_wallet/entities/parse_address_from_domain.dart'; +import 'package:cake_wallet/entities/parsed_address.dart'; import 'package:cake_wallet/monero/monero_amount_format.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; @@ -184,15 +186,13 @@ abstract class SendItemBase with Store { _cryptoNumberFormat.maximumFractionDigits = maximumFractionDigits; } - Future getOpenaliasRecord() async { - final formattedName = OpenaliasRecord.formatDomainName(address); - final record = await OpenaliasRecord.fetchAddressAndName(formattedName); + Future applyOpenaliasOrUnstoppableDomains() async { + final domain = address; + final ticker = _wallet.currency.title.toLowerCase(); + final parsedAddress = await parseAddressFromDomain(domain, ticker); - if (record == null || record.address.contains(formattedName)) { - return null; - } + address = parsedAddress.address; - address = record.address; - return record; + return parsedAddress; } } \ No newline at end of file diff --git a/lib/view_model/send/send_view_model.dart b/lib/view_model/send/send_view_model.dart index 6c04980ae..a4dda362d 100644 --- a/lib/view_model/send/send_view_model.dart +++ b/lib/view_model/send/send_view_model.dart @@ -44,6 +44,8 @@ abstract class SendViewModelBase with Store { sendItemList = ObservableList() ..add(SendItem(_wallet, _settingsStore, _fiatConversationStore)); + + isElectrumWallet = _wallet is ElectrumWallet; } @observable @@ -51,6 +53,8 @@ abstract class SendViewModelBase with Store { ObservableList sendItemList; + bool isElectrumWallet; + @action void addSendItem() { sendItemList.add(SendItem(_wallet, _settingsStore, _fiatConversationStore));