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

This commit is contained in:
M 2022-01-27 11:20:51 +02:00
parent 18a1f60433
commit 989ef442ac
5 changed files with 45 additions and 77 deletions

View file

@ -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;
}

View file

@ -1,7 +1,7 @@
import 'dart:typed_data';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin; import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData; 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_address_record.dart';
import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_bitcoin/bitcoin_amount_format.dart';
import 'package:cw_core/transaction_direction.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/format_amount.dart';
import 'package:cw_core/wallet_type.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 { class ElectrumTransactionBundle {
ElectrumTransactionBundle(this.originalTransaction, {this.ins, this.time, this.confirmations}); ElectrumTransactionBundle(this.originalTransaction, {this.ins, this.time, this.confirmations});
final bitcoin.Transaction originalTransaction; final bitcoin.Transaction originalTransaction;
@ -114,8 +94,11 @@ class ElectrumTransactionInfo extends TransactionInfo {
} }
factory ElectrumTransactionInfo.fromElectrumBundle( factory ElectrumTransactionInfo.fromElectrumBundle(
ElectrumTransactionBundle bundle, WalletType type, ElectrumTransactionBundle bundle,
{@required Set<String> addresses, int height}) { WalletType type,
bitcoin.NetworkType networkType,
{@required Set<String> addresses,
int height}) {
final date = bundle.time != null final date = bundle.time != null
? DateTime.fromMillisecondsSinceEpoch(bundle.time * 1000) ? DateTime.fromMillisecondsSinceEpoch(bundle.time * 1000)
: DateTime.now(); : DateTime.now();
@ -129,7 +112,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
final inputTransaction = bundle.ins[i]; final inputTransaction = bundle.ins[i];
final vout = input.index; final vout = input.index;
final outTransaction = inputTransaction.outs[vout]; final outTransaction = inputTransaction.outs[vout];
final address = addressFromOutput(outTransaction.script); final address = addressFromOutput(outTransaction.script, networkType);
inputAmount += outTransaction.value; inputAmount += outTransaction.value;
if (addresses.contains(address)) { if (addresses.contains(address)) {
direction = TransactionDirection.outgoing; direction = TransactionDirection.outgoing;
@ -138,7 +121,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
for (final out in bundle.originalTransaction.outs) { for (final out in bundle.originalTransaction.outs) {
totalOutAmount += out.value; totalOutAmount += out.value;
final address = addressFromOutput(out.script); final address = addressFromOutput(out.script, networkType);
final addressExists = addresses.contains(address); final addressExists = addresses.contains(address);
if ((direction == TransactionDirection.incoming && addressExists) || if ((direction == TransactionDirection.incoming && addressExists) ||
(direction == TransactionDirection.outgoing && !addressExists)) { (direction == TransactionDirection.outgoing && !addressExists)) {

View file

@ -505,7 +505,11 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
final tx = await getTransactionExpanded(hash: hash, height: height); final tx = await getTransactionExpanded(hash: hash, height: height);
final addresses = walletAddresses.addresses.map((addr) => addr.address).toSet(); final addresses = walletAddresses.addresses.map((addr) => addr.address).toSet();
return ElectrumTransactionInfo.fromElectrumBundle( return ElectrumTransactionInfo.fromElectrumBundle(
tx,walletInfo.type, addresses: addresses, height: height); tx,
walletInfo.type,
networkType,
addresses: addresses,
height: height);
} }
@override @override

View file

@ -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(
padding: EdgeInsets.only(top: 30, left: 24, bottom: 24), padding: EdgeInsets.only(top: 30, left: 24, bottom: 24),
child: Row( child: Row(
@ -743,42 +728,15 @@ class ExchangePage extends BasePage {
}); });
_receiveAmountFocus.addListener(() { _receiveAmountFocus.addListener(() {
if (_receiveAmountFocus.hasFocus && !exchangeViewModel.isFixedRateMode) { exchangeViewModel.isFixedRateMode = true;
showPopUp<void>( exchangeViewModel.changeReceiveAmount(
context: context, amount: receiveAmountController.text);
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();
});
});
}
}); });
reaction((_) => exchangeViewModel.isFixedRateMode, (bool isFixedRateMode) { _depositAmountFocus.addListener(() {
if ((_receiveAmountFocus.hasFocus || exchangeViewModel.isFixedRateMode = false;
exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) { exchangeViewModel.changeDepositAmount(
FocusScope.of(context).unfocus(); amount: depositAmountController.text);
receiveAmountController.text = '';
} else {
exchangeViewModel.changeDepositAmount(
amount: depositAmountController.text);
}
checkBoxKey.currentState
.changeValue(exchangeViewModel.isFixedRateMode);
exchangeViewModel.loadLimits();
}); });
_isReactionsSet = true; _isReactionsSet = true;

View file

@ -64,7 +64,7 @@ abstract class ExchangeViewModelBase with Store {
loadLimits(); loadLimits();
reaction( reaction(
(_) => isFixedRateMode, (_) => isFixedRateMode,
(Object _) => _defineIsReceiveAmountEditable()); (Object _) => loadLimits());
} }
final WalletBase wallet; final WalletBase wallet;
@ -439,6 +439,6 @@ abstract class ExchangeViewModelBase with Store {
isReceiveAmountEditable = false; isReceiveAmountEditable = false;
}*/ }*/
//isReceiveAmountEditable = false; //isReceiveAmountEditable = false;
isReceiveAmountEditable = (isFixedRateMode ?? false) && provider is ChangeNowExchangeProvider; isReceiveAmountEditable = provider is ChangeNowExchangeProvider;
} }
} }