Fixes for litecoin transactions. Change fixed rate mode for exchange.

M committed Jan 27, 2022 at 11:20 UTC 989ef442ac1ef4aba08288274a4253b9b9f19c60
5 files changed +45 -77
cw_bitcoin/lib/address_from_output.dart new
+23
@@ -0,0 +1,23 @@
1 +import 'dart:typed_data';
2 +import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
3 +import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData;
4 +
5 +String addressFromOutput(Uint8List script, bitcoin.NetworkType networkType) {
6 + try {
7 + return bitcoin.P2PKH(
8 + data: PaymentData(output: script),
9 + network: networkType)
10 + .data
11 + .address;
12 + } catch (_) {}
13 +
14 + try {
15 + return bitcoin.P2WPKH(
16 + data: PaymentData(output: script),
17 + network: networkType)
18 + .data
19 + .address;
20 + } catch(_) {}
21 +
22 + return null;
23 +}
\ No newline at end of file
cw_bitcoin/lib/electrum_transaction_info.dart
+8 -25
@@ -1,7 +1,7 @@
1 -import 'dart:typed_data';
1 import 'package:flutter/foundation.dart';
2 import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
3 import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData;
4 +import 'package:cw_bitcoin/address_from_output.dart';
5 import 'package:cw_bitcoin/bitcoin_address_record.dart';
6 import 'package:cw_bitcoin/bitcoin_amount_format.dart';
7 import 'package:cw_core/transaction_direction.dart';
@@ -9,26 +9,6 @@ import 'package:cw_core/transaction_info.dart';
9 import 'package:cw_core/format_amount.dart';
10 import 'package:cw_core/wallet_type.dart';
11
12 -String addressFromOutput(Uint8List script) {
13 - try {
14 - return bitcoin.P2PKH(
15 - data: PaymentData(output: script),
16 - network: bitcoin.bitcoin)
17 - .data
18 - .address;
19 - } catch (_) {}
20 -
21 - try {
22 - return bitcoin.P2WPKH(
23 - data: PaymentData(output: script),
24 - network: bitcoin.bitcoin)
25 - .data
26 - .address;
27 - } catch(_) {}
28 -
29 - return null;
30 -}
31 -
12 class ElectrumTransactionBundle {
13 ElectrumTransactionBundle(this.originalTransaction, {this.ins, this.time, this.confirmations});
14 final bitcoin.Transaction originalTransaction;
@@ -114,8 +94,11 @@ class ElectrumTransactionInfo extends TransactionInfo {
94 }
95
96 factory ElectrumTransactionInfo.fromElectrumBundle(
117 - ElectrumTransactionBundle bundle, WalletType type,
118 - {@required Set<String> addresses, int height}) {
97 + ElectrumTransactionBundle bundle,
98 + WalletType type,
99 + bitcoin.NetworkType networkType,
100 + {@required Set<String> addresses,
101 + int height}) {
102 final date = bundle.time != null
103 ? DateTime.fromMillisecondsSinceEpoch(bundle.time * 1000)
104 : DateTime.now();
@@ -129,7 +112,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
112 final inputTransaction = bundle.ins[i];
113 final vout = input.index;
114 final outTransaction = inputTransaction.outs[vout];
132 - final address = addressFromOutput(outTransaction.script);
115 + final address = addressFromOutput(outTransaction.script, networkType);
116 inputAmount += outTransaction.value;
117 if (addresses.contains(address)) {
118 direction = TransactionDirection.outgoing;
@@ -138,7 +121,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
121
122 for (final out in bundle.originalTransaction.outs) {
123 totalOutAmount += out.value;
141 - final address = addressFromOutput(out.script);
124 + final address = addressFromOutput(out.script, networkType);
125 final addressExists = addresses.contains(address);
126 if ((direction == TransactionDirection.incoming && addressExists) ||
127 (direction == TransactionDirection.outgoing && !addressExists)) {
cw_bitcoin/lib/electrum_wallet.dart
+5 -1
@@ -505,7 +505,11 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
505 final tx = await getTransactionExpanded(hash: hash, height: height);
506 final addresses = walletAddresses.addresses.map((addr) => addr.address).toSet();
507 return ElectrumTransactionInfo.fromElectrumBundle(
508 - tx,walletInfo.type, addresses: addresses, height: height);
508 + tx,
509 + walletInfo.type,
510 + networkType,
511 + addresses: addresses,
512 + height: height);
513 }
514
515 @override
lib/src/screens/exchange/exchange_page.dart
+7 -49
@@ -317,21 +317,6 @@ class ExchangePage extends BasePage {
317 ],
318 ),
319 ),
320 - Padding(
321 - padding: EdgeInsets.only(top: 12, left: 24),
322 - child: Row(
323 - mainAxisAlignment: MainAxisAlignment.start,
324 - children: [
325 - StandardCheckbox(
326 - key: checkBoxKey,
327 - value: exchangeViewModel.isFixedRateMode,
328 - caption: S.of(context).fixed_rate,
329 - onChanged: (value) =>
330 - exchangeViewModel.isFixedRateMode = value,
331 - ),
332 - ],
333 - )
334 - ),
320 Padding(
321 padding: EdgeInsets.only(top: 30, left: 24, bottom: 24),
322 child: Row(
@@ -743,42 +728,15 @@ class ExchangePage extends BasePage {
728 });
729
730 _receiveAmountFocus.addListener(() {
746 - if (_receiveAmountFocus.hasFocus && !exchangeViewModel.isFixedRateMode) {
747 - showPopUp<void>(
748 - context: context,
749 - builder: (BuildContext context) {
750 - return AlertWithTwoActions(
751 - alertTitle: S.of(context).exchange,
752 - alertContent: S.of(context).fixed_rate_alert,
753 - leftButtonText: S.of(context).cancel,
754 - rightButtonText: S.of(context).ok,
755 - actionLeftButton: () {
756 - FocusScope.of(context).unfocus();
757 - Navigator.of(context).pop();
758 - },
759 - actionRightButton: () {
760 - exchangeViewModel.isFixedRateMode = true;
761 - checkBoxKey.currentState
762 - .changeValue(exchangeViewModel.isFixedRateMode);
763 - Navigator.of(context).pop();
764 - });
765 - });
766 - }
731 + exchangeViewModel.isFixedRateMode = true;
732 + exchangeViewModel.changeReceiveAmount(
733 + amount: receiveAmountController.text);
734 });
735
769 - reaction((_) => exchangeViewModel.isFixedRateMode, (bool isFixedRateMode) {
770 - if ((_receiveAmountFocus.hasFocus ||
771 - exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) {
772 - FocusScope.of(context).unfocus();
773 - receiveAmountController.text = '';
774 - } else {
775 - exchangeViewModel.changeDepositAmount(
776 - amount: depositAmountController.text);
777 - }
778 -
779 - checkBoxKey.currentState
780 - .changeValue(exchangeViewModel.isFixedRateMode);
781 - exchangeViewModel.loadLimits();
736 + _depositAmountFocus.addListener(() {
737 + exchangeViewModel.isFixedRateMode = false;
738 + exchangeViewModel.changeDepositAmount(
739 + amount: depositAmountController.text);
740 });
741
742 _isReactionsSet = true;
lib/view_model/exchange/exchange_view_model.dart
+2 -2
@@ -64,7 +64,7 @@ abstract class ExchangeViewModelBase with Store {
64 loadLimits();
65 reaction(
66 (_) => isFixedRateMode,
67 - (Object _) => _defineIsReceiveAmountEditable());
67 + (Object _) => loadLimits());
68 }
69
70 final WalletBase wallet;
@@ -439,6 +439,6 @@ abstract class ExchangeViewModelBase with Store {
439 isReceiveAmountEditable = false;
440 }*/
441 //isReceiveAmountEditable = false;
442 - isReceiveAmountEditable = (isFixedRateMode ?? false) && provider is ChangeNowExchangeProvider;
442 + isReceiveAmountEditable = provider is ChangeNowExchangeProvider;
443 }
444 }