From 989ef442ac1ef4aba08288274a4253b9b9f19c60 Mon Sep 17 00:00:00 2001 From: M Date: Thu, 27 Jan 2022 11:20:51 +0200 Subject: [PATCH] Fixes for litecoin transactions. Change fixed rate mode for exchange. --- cw_bitcoin/lib/address_from_output.dart | 23 ++++++++ cw_bitcoin/lib/electrum_transaction_info.dart | 33 +++-------- cw_bitcoin/lib/electrum_wallet.dart | 6 +- lib/src/screens/exchange/exchange_page.dart | 56 +++---------------- .../exchange/exchange_view_model.dart | 4 +- 5 files changed, 45 insertions(+), 77 deletions(-) create mode 100644 cw_bitcoin/lib/address_from_output.dart diff --git a/cw_bitcoin/lib/address_from_output.dart b/cw_bitcoin/lib/address_from_output.dart new file mode 100644 index 000000000..6aa90e883 --- /dev/null +++ b/cw_bitcoin/lib/address_from_output.dart @@ -0,0 +1,23 @@ +import 'dart:typed_data'; +import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin; +import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData; + +String addressFromOutput(Uint8List script, bitcoin.NetworkType networkType) { + try { + return bitcoin.P2PKH( + data: PaymentData(output: script), + network: networkType) + .data + .address; + } catch (_) {} + + try { + return bitcoin.P2WPKH( + data: PaymentData(output: script), + network: networkType) + .data + .address; + } catch(_) {} + + return null; +} \ No newline at end of file diff --git a/cw_bitcoin/lib/electrum_transaction_info.dart b/cw_bitcoin/lib/electrum_transaction_info.dart index 98ad60f70..491ebddfb 100644 --- a/cw_bitcoin/lib/electrum_transaction_info.dart +++ b/cw_bitcoin/lib/electrum_transaction_info.dart @@ -1,7 +1,7 @@ -import 'dart:typed_data'; import 'package:flutter/foundation.dart'; import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin; import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData; +import 'package:cw_bitcoin/address_from_output.dart'; import 'package:cw_bitcoin/bitcoin_address_record.dart'; import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_core/transaction_direction.dart'; @@ -9,26 +9,6 @@ import 'package:cw_core/transaction_info.dart'; import 'package:cw_core/format_amount.dart'; import 'package:cw_core/wallet_type.dart'; -String addressFromOutput(Uint8List script) { - try { - return bitcoin.P2PKH( - data: PaymentData(output: script), - network: bitcoin.bitcoin) - .data - .address; - } catch (_) {} - - try { - return bitcoin.P2WPKH( - data: PaymentData(output: script), - network: bitcoin.bitcoin) - .data - .address; - } catch(_) {} - - return null; -} - class ElectrumTransactionBundle { ElectrumTransactionBundle(this.originalTransaction, {this.ins, this.time, this.confirmations}); final bitcoin.Transaction originalTransaction; @@ -114,8 +94,11 @@ class ElectrumTransactionInfo extends TransactionInfo { } factory ElectrumTransactionInfo.fromElectrumBundle( - ElectrumTransactionBundle bundle, WalletType type, - {@required Set addresses, int height}) { + ElectrumTransactionBundle bundle, + WalletType type, + bitcoin.NetworkType networkType, + {@required Set addresses, + int height}) { final date = bundle.time != null ? DateTime.fromMillisecondsSinceEpoch(bundle.time * 1000) : DateTime.now(); @@ -129,7 +112,7 @@ class ElectrumTransactionInfo extends TransactionInfo { final inputTransaction = bundle.ins[i]; final vout = input.index; final outTransaction = inputTransaction.outs[vout]; - final address = addressFromOutput(outTransaction.script); + final address = addressFromOutput(outTransaction.script, networkType); inputAmount += outTransaction.value; if (addresses.contains(address)) { direction = TransactionDirection.outgoing; @@ -138,7 +121,7 @@ class ElectrumTransactionInfo extends TransactionInfo { for (final out in bundle.originalTransaction.outs) { totalOutAmount += out.value; - final address = addressFromOutput(out.script); + final address = addressFromOutput(out.script, networkType); final addressExists = addresses.contains(address); if ((direction == TransactionDirection.incoming && addressExists) || (direction == TransactionDirection.outgoing && !addressExists)) { diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 03884d4b5..ebfb09392 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -505,7 +505,11 @@ abstract class ElectrumWalletBase extends WalletBase addr.address).toSet(); return ElectrumTransactionInfo.fromElectrumBundle( - tx,walletInfo.type, addresses: addresses, height: height); + tx, + walletInfo.type, + networkType, + addresses: addresses, + height: height); } @override diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index d7c44cee3..176d238db 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -317,21 +317,6 @@ class ExchangePage extends BasePage { ], ), ), - Padding( - padding: EdgeInsets.only(top: 12, left: 24), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - StandardCheckbox( - key: checkBoxKey, - value: exchangeViewModel.isFixedRateMode, - caption: S.of(context).fixed_rate, - onChanged: (value) => - exchangeViewModel.isFixedRateMode = value, - ), - ], - ) - ), Padding( padding: EdgeInsets.only(top: 30, left: 24, bottom: 24), child: Row( @@ -743,42 +728,15 @@ class ExchangePage extends BasePage { }); _receiveAmountFocus.addListener(() { - if (_receiveAmountFocus.hasFocus && !exchangeViewModel.isFixedRateMode) { - showPopUp( - context: context, - builder: (BuildContext context) { - return AlertWithTwoActions( - alertTitle: S.of(context).exchange, - alertContent: S.of(context).fixed_rate_alert, - leftButtonText: S.of(context).cancel, - rightButtonText: S.of(context).ok, - actionLeftButton: () { - FocusScope.of(context).unfocus(); - Navigator.of(context).pop(); - }, - actionRightButton: () { - exchangeViewModel.isFixedRateMode = true; - checkBoxKey.currentState - .changeValue(exchangeViewModel.isFixedRateMode); - Navigator.of(context).pop(); - }); - }); - } + exchangeViewModel.isFixedRateMode = true; + exchangeViewModel.changeReceiveAmount( + amount: receiveAmountController.text); }); - reaction((_) => exchangeViewModel.isFixedRateMode, (bool isFixedRateMode) { - if ((_receiveAmountFocus.hasFocus || - exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) { - FocusScope.of(context).unfocus(); - receiveAmountController.text = ''; - } else { - exchangeViewModel.changeDepositAmount( - amount: depositAmountController.text); - } - - checkBoxKey.currentState - .changeValue(exchangeViewModel.isFixedRateMode); - exchangeViewModel.loadLimits(); + _depositAmountFocus.addListener(() { + exchangeViewModel.isFixedRateMode = false; + exchangeViewModel.changeDepositAmount( + amount: depositAmountController.text); }); _isReactionsSet = true; diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index ce36e875e..9e4e8d861 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -64,7 +64,7 @@ abstract class ExchangeViewModelBase with Store { loadLimits(); reaction( (_) => isFixedRateMode, - (Object _) => _defineIsReceiveAmountEditable()); + (Object _) => loadLimits()); } final WalletBase wallet; @@ -439,6 +439,6 @@ abstract class ExchangeViewModelBase with Store { isReceiveAmountEditable = false; }*/ //isReceiveAmountEditable = false; - isReceiveAmountEditable = (isFixedRateMode ?? false) && provider is ChangeNowExchangeProvider; + isReceiveAmountEditable = provider is ChangeNowExchangeProvider; } }