fix transaction priority type

This commit is contained in:
Serhii 2023-08-18 11:48:30 +03:00
parent c1faead0ab
commit 5f59bd939e
8 changed files with 106 additions and 45 deletions

View file

@ -1,6 +1,6 @@
import 'package:cw_core/transaction_priority.dart'; import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
class BitcoinCashTransactionPriority extends TransactionPriority { class BitcoinCashTransactionPriority extends BitcoinTransactionPriority {
const BitcoinCashTransactionPriority({required String title, required int raw}) const BitcoinCashTransactionPriority({required String title, required int raw})
: super(title: title, raw: raw); : super(title: title, raw: raw);

View file

@ -3,6 +3,7 @@ import 'package:cw_bitcoin/bitcoin_address_record.dart';
import 'package:cw_bitcoin/electrum_balance.dart'; import 'package:cw_bitcoin/electrum_balance.dart';
import 'package:cw_bitcoin/electrum_wallet.dart'; import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_bitcoin/electrum_wallet_snapshot.dart'; import 'package:cw_bitcoin/electrum_wallet_snapshot.dart';
import 'package:cw_bitcoin/pending_bitcoin_transaction.dart';
import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/unspent_coins_info.dart'; import 'package:cw_core/unspent_coins_info.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
@ -44,7 +45,7 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
initialChangeAddressIndex: initialChangeAddressIndex, initialChangeAddressIndex: initialChangeAddressIndex,
mainHd: hd, mainHd: hd,
sideHd: hd, sideHd: hd,
//TODO: BCH check if this is correct //TODO: BCH: check if this is correct
networkType: networkType); networkType: networkType);
} }
@ -87,4 +88,71 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
initialRegularAddressIndex: snp.regularAddressIndex, initialRegularAddressIndex: snp.regularAddressIndex,
initialChangeAddressIndex: snp.changeAddressIndex); initialChangeAddressIndex: snp.changeAddressIndex);
} }
}
@override
Future<PendingBitcoinTransaction> createTransaction(Object credentials,
[List<Object>? unspents, Object? wallet]) async {
// final utxoSigningData = await fetchBuildTxData(unspents as List<UnspentCash>, wallet as BitcoinCashWalletBase);
// final builder = bitbox.Bitbox.transactionBuilder(testnet: false);
// final utxosToUse = unspents as List<UnspentCash>;
// final _wallet = wallet as BitcoinCashWallet;
// print('unspents: ${unspents.first.address}');
//
// List<bitbox.Utxo> _utxos = [];
// for (var element in utxosToUse) {
// _utxos.add(bitbox.Utxo(element.hash, element.vout,
// bitbox.BitcoinCash.fromSatoshi(element.value), element.value, 0, 1));
// }
//
// final signatures = <Map>[];
// int totalBalance = 0;
//
// _utxos.forEach((bitbox.Utxo utxo) {
// // add the utxo as an input for the transaction
// builder.addInput(utxo.txid, utxo.vout);
//
// final ec = utxoSigningData.firstWhere((e) => e.utxo.hash == utxo.txid).keyPair!;
//
// final bitboxEC = bitbox.ECPair.fromWIF(ec.toWIF());
//
// // add a signature to the list to be used later
// signatures
// .add({"vin": signatures.length, "key_pair": bitboxEC, "original_amount": utxo.satoshis});
//
// totalBalance += utxo.satoshis;
// });
//
// // set an address to send the remaining balance to
// final outputAddress = "13Hvge9HRduGiXMfcJHFn6sggequmaKqsZ";
//
// // if there is an unspent balance, create a spending transaction
// if (totalBalance > 0 && outputAddress != "") {
// // calculate the fee based on number of inputs and one expected output
// final fee = bitbox.BitcoinCash.getByteCount(signatures.length, 1);
//
// // calculate how much balance will be left over to spend after the fee
// final sendAmount = totalBalance - fee;
//
// // add the output based on the address provided in the testing data
// builder.addOutput(outputAddress, sendAmount);
//
// // sign all inputs
// signatures.forEach((signature) {
// builder.sign(signature["vin"], signature["key_pair"], signature["original_amount"]);
// });
//
// // build the transaction
// final tx = builder.build();
//
// // broadcast the transaction
// final result = await electrumClient.broadcastTransaction(transactionRaw: tx.toHex());
//
// // Yatta!
// print("Transaction broadcasted: $result");
// }
return PendingBitcoinTransaction(bitcoin.Transaction(), type,
electrumClient: electrumClient, amount: 1, fee: 1);
}
}

View file

@ -44,9 +44,14 @@ class CWBitcoin extends Bitcoin {
List<TransactionPriority> getTransactionPriorities() List<TransactionPriority> getTransactionPriorities()
=> BitcoinTransactionPriority.all; => BitcoinTransactionPriority.all;
@override
List<TransactionPriority> getLitecoinTransactionPriorities() List<TransactionPriority> getLitecoinTransactionPriorities()
=> LitecoinTransactionPriority.all; => LitecoinTransactionPriority.all;
@override
List<TransactionPriority> getBitcoinCashTransactionPriorities()
=> BitcoinCashTransactionPriority.all;
@override @override
TransactionPriority deserializeBitcoinTransactionPriority(int raw) TransactionPriority deserializeBitcoinTransactionPriority(int raw)
=> BitcoinTransactionPriority.deserialize(raw: raw); => BitcoinTransactionPriority.deserialize(raw: raw);
@ -55,6 +60,10 @@ class CWBitcoin extends Bitcoin {
TransactionPriority deserializeLitecoinTransactionPriority(int raw) TransactionPriority deserializeLitecoinTransactionPriority(int raw)
=> LitecoinTransactionPriority.deserialize(raw: raw); => LitecoinTransactionPriority.deserialize(raw: raw);
@override
TransactionPriority deserializeBitcoinCashTransactionPriority(int raw)
=> BitcoinCashTransactionPriority.deserialize(raw: raw);
@override @override
int getFeeRate(Object wallet, TransactionPriority priority) { int getFeeRate(Object wallet, TransactionPriority priority) {
final bitcoinWallet = wallet as ElectrumWallet; final bitcoinWallet = wallet as ElectrumWallet;

View file

@ -171,6 +171,7 @@ class AddressValidator extends TextValidator {
case CryptoCurrency.shib: case CryptoCurrency.shib:
case CryptoCurrency.avaxc: case CryptoCurrency.avaxc:
case CryptoCurrency.bch: case CryptoCurrency.bch:
return [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]; //TODO: BCH: replace with correct length
case CryptoCurrency.bnb: case CryptoCurrency.bnb:
return [42]; return [42];
case CryptoCurrency.ltc: case CryptoCurrency.ltc:

View file

@ -34,6 +34,7 @@ class PreferencesKey {
static const havenTransactionPriority = 'current_fee_priority_haven'; static const havenTransactionPriority = 'current_fee_priority_haven';
static const litecoinTransactionPriority = 'current_fee_priority_litecoin'; static const litecoinTransactionPriority = 'current_fee_priority_litecoin';
static const ethereumTransactionPriority = 'current_fee_priority_ethereum'; static const ethereumTransactionPriority = 'current_fee_priority_ethereum';
static const bitcoinCashTransactionPriority = 'current_fee_priority_bitcoin_cash';
static const shouldShowReceiveWarning = 'should_show_receive_warning'; static const shouldShowReceiveWarning = 'should_show_receive_warning';
static const shouldShowYatPopup = 'should_show_yat_popup'; static const shouldShowYatPopup = 'should_show_yat_popup';
static const moneroWalletPasswordUpdateV1Base = 'monero_wallet_update_v1'; static const moneroWalletPasswordUpdateV1Base = 'monero_wallet_update_v1';

View file

@ -140,8 +140,8 @@ abstract class SettingsStoreBase with Store {
priority[WalletType.ethereum] = initialEthereumTransactionPriority; priority[WalletType.ethereum] = initialEthereumTransactionPriority;
} }
if (initialBitcoinTransactionPriority != null) { if (initialBitcoinCashTransactionPriority != null) {
priority[WalletType.bitcoinCash] = initialBitcoinTransactionPriority; priority[WalletType.bitcoinCash] = initialBitcoinCashTransactionPriority;
} }
reaction( reaction(
@ -173,7 +173,7 @@ abstract class SettingsStoreBase with Store {
key = PreferencesKey.ethereumTransactionPriority; key = PreferencesKey.ethereumTransactionPriority;
break; break;
case WalletType.bitcoinCash: case WalletType.bitcoinCash:
key = PreferencesKey.bitcoinTransactionPriority; key = PreferencesKey.bitcoinCashTransactionPriority;
break; break;
default: default:
key = null; key = null;
@ -518,9 +518,9 @@ abstract class SettingsStoreBase with Store {
ethereumTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority( ethereumTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority(
sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!); sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!);
} }
if (sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority) != null) { if (sharedPreferences.getInt(PreferencesKey.bitcoinCashTransactionPriority) != null) {
bitcoinCashTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority( bitcoinCashTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority(
sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority)!); sharedPreferences.getInt(PreferencesKey.bitcoinCashTransactionPriority)!);
} }
moneroTransactionPriority ??= monero?.getDefaultTransactionPriority(); moneroTransactionPriority ??= monero?.getDefaultTransactionPriority();
@ -730,9 +730,9 @@ abstract class SettingsStoreBase with Store {
sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!) ?? sharedPreferences.getInt(PreferencesKey.ethereumTransactionPriority)!) ??
priority[WalletType.ethereum]!; priority[WalletType.ethereum]!;
} }
if (sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority) != null) { if (sharedPreferences.getInt(PreferencesKey.bitcoinCashTransactionPriority) != null) {
priority[WalletType.bitcoinCash] = bitcoinCash?.deserializeBitcoinCashTransactionPriority( priority[WalletType.bitcoinCash] = bitcoin?.deserializeBitcoinCashTransactionPriority(
sharedPreferences.getInt(PreferencesKey.bitcoinTransactionPriority)!) ?? sharedPreferences.getInt(PreferencesKey.bitcoinCashTransactionPriority)!) ??
priority[WalletType.bitcoinCash]!; priority[WalletType.bitcoinCash]!;
} }

View file

@ -323,50 +323,30 @@ abstract class SendViewModelBase with Store {
_settingsStore.priority[_wallet.type] = priority; _settingsStore.priority[_wallet.type] = priority;
Object _credentials() { Object _credentials() {
final priority = _settingsStore.priority[_wallet.type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${_wallet.type}');
}
switch (_wallet.type) { switch (_wallet.type) {
case WalletType.bitcoin: case WalletType.bitcoin:
final priority = _settingsStore.priority[_wallet.type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${_wallet.type}');
}
return bitcoin!.createBitcoinTransactionCredentials(outputs, priority: priority);
case WalletType.litecoin: case WalletType.litecoin:
final priority = _settingsStore.priority[_wallet.type]; case WalletType.bitcoinCash:
if (priority == null) {
throw Exception('Priority is null for wallet type: ${_wallet.type}');
}
return bitcoin!.createBitcoinTransactionCredentials(outputs, priority: priority); return bitcoin!.createBitcoinTransactionCredentials(outputs, priority: priority);
case WalletType.monero: case WalletType.monero:
final priority = _settingsStore.priority[_wallet.type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${_wallet.type}');
}
return monero! return monero!
.createMoneroTransactionCreationCredentials(outputs: outputs, priority: priority); .createMoneroTransactionCreationCredentials(outputs: outputs, priority: priority);
case WalletType.haven: case WalletType.haven:
final priority = _settingsStore.priority[_wallet.type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${_wallet.type}');
}
return haven!.createHavenTransactionCreationCredentials( return haven!.createHavenTransactionCreationCredentials(
outputs: outputs, priority: priority, assetType: selectedCryptoCurrency.title); outputs: outputs, priority: priority, assetType: selectedCryptoCurrency.title);
case WalletType.ethereum: case WalletType.ethereum:
final priority = _settingsStore.priority[_wallet.type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${_wallet.type}');
}
return ethereum!.createEthereumTransactionCredentials( return ethereum!.createEthereumTransactionCredentials(
outputs, priority: priority, currency: selectedCryptoCurrency); outputs, priority: priority, currency: selectedCryptoCurrency);
default: default:
throw Exception('Unexpected wallet type: ${_wallet.type}'); throw Exception('Unexpected wallet type: ${_wallet.type}');
} }

View file

@ -28,6 +28,7 @@ Future<void> main(List<String> args) async {
Future<void> generateBitcoin(bool hasImplementation) async { Future<void> generateBitcoin(bool hasImplementation) async {
final outputFile = File(bitcoinOutputPath); final outputFile = File(bitcoinOutputPath);
const bitcoinCommonHeaders = """ const bitcoinCommonHeaders = """
import 'package:cw_bitcoin_cash/cw_bitcoin_cash.dart';
import 'package:cw_core/wallet_credentials.dart'; import 'package:cw_core/wallet_credentials.dart';
import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/transaction_priority.dart'; import 'package:cw_core/transaction_priority.dart';
@ -41,7 +42,6 @@ import 'package:cw_bitcoin/electrum_wallet.dart';
import 'package:cw_bitcoin/bitcoin_unspent.dart'; import 'package:cw_bitcoin/bitcoin_unspent.dart';
import 'package:cw_bitcoin/bitcoin_mnemonic.dart'; import 'package:cw_bitcoin/bitcoin_mnemonic.dart';
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart'; import 'package:cw_bitcoin/bitcoin_transaction_priority.dart';
import 'package:cw_bitcoin/bitcoin_wallet.dart';
import 'package:cw_bitcoin/bitcoin_wallet_service.dart'; import 'package:cw_bitcoin/bitcoin_wallet_service.dart';
import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart'; import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart';
import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_bitcoin/bitcoin_amount_format.dart';
@ -79,8 +79,10 @@ abstract class Bitcoin {
Map<String, String> getWalletKeys(Object wallet); Map<String, String> getWalletKeys(Object wallet);
List<TransactionPriority> getTransactionPriorities(); List<TransactionPriority> getTransactionPriorities();
List<TransactionPriority> getLitecoinTransactionPriorities(); List<TransactionPriority> getLitecoinTransactionPriorities();
TransactionPriority deserializeBitcoinTransactionPriority(int raw); List<TransactionPriority> getBitcoinCashTransactionPriorities();
TransactionPriority deserializeBitcoinTransactionPriority(int raw);
TransactionPriority deserializeLitecoinTransactionPriority(int raw); TransactionPriority deserializeLitecoinTransactionPriority(int raw);
TransactionPriority deserializeBitcoinCashTransactionPriority(int raw);
int getFeeRate(Object wallet, TransactionPriority priority); int getFeeRate(Object wallet, TransactionPriority priority);
Future<void> generateNewAddress(Object wallet); Future<void> generateNewAddress(Object wallet);
Object createBitcoinTransactionCredentials(List<Output> outputs, {required TransactionPriority priority, int? feeRate}); Object createBitcoinTransactionCredentials(List<Output> outputs, {required TransactionPriority priority, int? feeRate});