replace Unspend with BitcoinUnspent

This commit is contained in:
Serhii 2023-08-18 12:32:59 +03:00
parent 5f59bd939e
commit c563e300f8
4 changed files with 65 additions and 82 deletions

View file

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

View file

@ -130,11 +130,11 @@ class CWBitcoin extends Bitcoin {
=> (priority as BitcoinTransactionPriority).labelWithRate(rate); => (priority as BitcoinTransactionPriority).labelWithRate(rate);
@override @override
List<Unspent> getUnspents(Object wallet) { List<BitcoinUnspent> getUnspents(Object wallet) {
final bitcoinWallet = wallet as ElectrumWallet; final bitcoinWallet = wallet as ElectrumWallet;
return bitcoinWallet.unspentCoins return bitcoinWallet.unspentCoins
.map((BitcoinUnspent bitcoinUnspent) => Unspent( .map((BitcoinUnspent bitcoinUnspent) => BitcoinUnspent(
bitcoinUnspent.address.address, bitcoinUnspent.address,
bitcoinUnspent.hash, bitcoinUnspent.hash,
bitcoinUnspent.value, bitcoinUnspent.value,
bitcoinUnspent.vout)) bitcoinUnspent.vout))

View file

@ -26,10 +26,10 @@ abstract class UnspentCoinsListViewModelBase with Store {
final amount = bitcoin!.formatterBitcoinAmountToString(amount: elem.value) + final amount = bitcoin!.formatterBitcoinAmountToString(amount: elem.value) +
' ${wallet.currency.title}'; ' ${wallet.currency.title}';
final info = getUnspentCoinInfo(elem.hash, elem.address, elem.value, elem.vout); final info = getUnspentCoinInfo(elem.hash, elem.address.address, elem.value, elem.vout);
return UnspentCoinsItem( return UnspentCoinsItem(
address: elem.address, address: elem.address.address,
amount: amount, amount: amount,
hash: elem.hash, hash: elem.hash,
isFrozen: info?.isFrozen ?? false, isFrozen: info?.isFrozen ?? false,

View file

@ -51,24 +51,6 @@ import 'package:cw_bitcoin/litecoin_wallet_service.dart';
"""; """;
const bitcoinCwPart = "part 'cw_bitcoin.dart';"; const bitcoinCwPart = "part 'cw_bitcoin.dart';";
const bitcoinContent = """ const bitcoinContent = """
class Unspent {
Unspent(this.address, this.hash, this.value, this.vout)
: isSending = true,
isFrozen = false,
note = '';
final String address;
final String hash;
final int value;
final int vout;
bool isSending;
bool isFrozen;
String note;
bool get isP2wpkh => address.startsWith('bc') || address.startsWith('ltc');
}
abstract class Bitcoin { abstract class Bitcoin {
TransactionPriority getMediumTransactionPriority(); TransactionPriority getMediumTransactionPriority();
@ -96,7 +78,7 @@ abstract class Bitcoin {
int formatterStringDoubleToBitcoinAmount(String amount); int formatterStringDoubleToBitcoinAmount(String amount);
String bitcoinTransactionPriorityWithLabel(TransactionPriority priority, int rate); String bitcoinTransactionPriorityWithLabel(TransactionPriority priority, int rate);
List<Unspent> getUnspents(Object wallet); List<BitcoinUnspent> getUnspents(Object wallet);
void updateUnspents(Object wallet); void updateUnspents(Object wallet);
WalletService createBitcoinWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource); WalletService createBitcoinWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource);
WalletService createLitecoinWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource); WalletService createLitecoinWalletService(Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource);