Skip to content

Commit

Permalink
Cw 830 coin control getting cleared (#1825)
Browse files Browse the repository at this point in the history
* init commit

* add select all button

* localisation all coins

* fix isSending and isFrozen state updates

* fix: clean up electrum UTXOs

* ui fixes

* address the review comments[skip ci]

* remove onPopInvoked[skip ci]

---------

Co-authored-by: Omar Hatem <[email protected]>
  • Loading branch information
Serhii-Borodenko and OmarHatem28 authored Nov 28, 2024
1 parent 4ca50b5 commit 9cd69c4
Show file tree
Hide file tree
Showing 40 changed files with 405 additions and 128 deletions.
9 changes: 9 additions & 0 deletions cw_bitcoin/lib/bitcoin_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ class BitcoinWalletService extends WalletService<
final walletInfo = walletInfoSource.values
.firstWhereOrNull((info) => info.id == WalletBase.idFor(wallet, getType()))!;
await walletInfoSource.delete(walletInfo.key);

final unspentCoinsToDelete = unspentCoinsInfoSource.values.where(
(unspentCoin) => unspentCoin.walletId == walletInfo.id).toList();

final keysToDelete = unspentCoinsToDelete.map((unspentCoin) => unspentCoin.key).toList();

if (keysToDelete.isNotEmpty) {
await unspentCoinsInfoSource.deleteAll(keysToDelete);
}
}

@override
Expand Down
57 changes: 42 additions & 15 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ abstract class ElectrumWalletBase
Future<void> init() async {
await walletAddresses.init();
await transactionHistory.init();
await cleanUpDuplicateUnspentCoins();
await save();

_autoSaveTimer =
Expand Down Expand Up @@ -1379,10 +1380,11 @@ abstract class ElectrumWalletBase
}));

unspentCoins = updatedUnspentCoins;

final currentWalletUnspentCoins = unspentCoinsInfo.values.where((element) => element.walletId == id);

if (unspentCoinsInfo.length != updatedUnspentCoins.length) {
if (currentWalletUnspentCoins.length != updatedUnspentCoins.length) {
unspentCoins.forEach((coin) => addCoinInfo(coin));
return;
}

await updateCoins(unspentCoins);
Expand All @@ -1408,6 +1410,7 @@ abstract class ElectrumWalletBase
coin.isFrozen = coinInfo.isFrozen;
coin.isSending = coinInfo.isSending;
coin.note = coinInfo.note;

if (coin.bitcoinAddressRecord is! BitcoinSilentPaymentAddressRecord)
coin.bitcoinAddressRecord.balance += coinInfo.value;
} else {
Expand Down Expand Up @@ -1445,20 +1448,27 @@ abstract class ElectrumWalletBase

@action
Future<void> addCoinInfo(BitcoinUnspent coin) async {
final newInfo = UnspentCoinsInfo(
walletId: id,
hash: coin.hash,
isFrozen: coin.isFrozen,
isSending: coin.isSending,
noteRaw: coin.note,
address: coin.bitcoinAddressRecord.address,
value: coin.value,
vout: coin.vout,
isChange: coin.isChange,
isSilentPayment: coin is BitcoinSilentPaymentsUnspent,
);

await unspentCoinsInfo.add(newInfo);
// Check if the coin is already in the unspentCoinsInfo for the wallet
final existingCoinInfo = unspentCoinsInfo.values.firstWhereOrNull(
(element) => element.walletId == walletInfo.id && element == coin);

if (existingCoinInfo == null) {
final newInfo = UnspentCoinsInfo(
walletId: id,
hash: coin.hash,
isFrozen: coin.isFrozen,
isSending: coin.isSending,
noteRaw: coin.note,
address: coin.bitcoinAddressRecord.address,
value: coin.value,
vout: coin.vout,
isChange: coin.isChange,
isSilentPayment: coin is BitcoinSilentPaymentsUnspent,
);

await unspentCoinsInfo.add(newInfo);
}
}

Future<void> _refreshUnspentCoinsInfo() async {
Expand Down Expand Up @@ -1486,6 +1496,23 @@ abstract class ElectrumWalletBase
}
}

Future<void> cleanUpDuplicateUnspentCoins() async {
final currentWalletUnspentCoins = unspentCoinsInfo.values.where((element) => element.walletId == id);
final Map<String, UnspentCoinsInfo> uniqueUnspentCoins = {};
final List<dynamic> duplicateKeys = [];

for (final unspentCoin in currentWalletUnspentCoins) {
final key = '${unspentCoin.hash}:${unspentCoin.vout}';
if (!uniqueUnspentCoins.containsKey(key)) {
uniqueUnspentCoins[key] = unspentCoin;
} else {
duplicateKeys.add(unspentCoin.key);
}
}

if (duplicateKeys.isNotEmpty) await unspentCoinsInfo.deleteAll(duplicateKeys);
}

int transactionVSize(String transactionHex) => BtcTransaction.fromRaw(transactionHex).getVSize();

Future<String?> canReplaceByFee(ElectrumTransactionInfo tx) async {
Expand Down
9 changes: 9 additions & 0 deletions cw_bitcoin/lib/litecoin_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ class LitecoinWalletService extends WalletService<
mwebdLogs.deleteSync();
}
}

final unspentCoinsToDelete = unspentCoinsInfoSource.values.where(
(unspentCoin) => unspentCoin.walletId == walletInfo.id).toList();

final keysToDelete = unspentCoinsToDelete.map((unspentCoin) => unspentCoin.key).toList();

if (keysToDelete.isNotEmpty) {
await unspentCoinsInfoSource.deleteAll(keysToDelete);
}
}

@override
Expand Down
9 changes: 9 additions & 0 deletions cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class BitcoinCashWalletService extends WalletService<
final walletInfo = walletInfoSource.values
.firstWhereOrNull((info) => info.id == WalletBase.idFor(wallet, getType()))!;
await walletInfoSource.delete(walletInfo.key);

final unspentCoinsToDelete = unspentCoinsInfoSource.values.where(
(unspentCoin) => unspentCoin.walletId == walletInfo.id).toList();

final keysToDelete = unspentCoinsToDelete.map((unspentCoin) => unspentCoin.key).toList();

if (keysToDelete.isNotEmpty) {
await unspentCoinsInfoSource.deleteAll(keysToDelete);
}
}

@override
Expand Down
3 changes: 2 additions & 1 deletion cw_core/lib/unspent_coins_info.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:cw_core/hive_type_ids.dart';
import 'package:cw_core/unspent_comparable_mixin.dart';
import 'package:hive/hive.dart';

part 'unspent_coins_info.g.dart';

@HiveType(typeId: UnspentCoinsInfo.typeId)
class UnspentCoinsInfo extends HiveObject {
class UnspentCoinsInfo extends HiveObject with UnspentComparable {
UnspentCoinsInfo({
required this.walletId,
required this.hash,
Expand Down
27 changes: 27 additions & 0 deletions cw_core/lib/unspent_comparable_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mixin UnspentComparable {
String get address;

String get hash;

int get value;

int get vout;

String? get keyImage;

bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is UnspentComparable &&
other.hash == hash &&
other.address == address &&
other.value == value &&
other.vout == vout &&
other.keyImage == keyImage;
}

@override
int get hashCode {
return Object.hash(address, hash, value, vout, keyImage);
}
}
4 changes: 3 additions & 1 deletion cw_core/lib/unspent_transaction_output.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Unspent {
import 'package:cw_core/unspent_comparable_mixin.dart';

class Unspent with UnspentComparable {
Unspent(this.address, this.hash, this.value, this.vout, this.keyImage)
: isSending = true,
isFrozen = false,
Expand Down
Loading

0 comments on commit 9cd69c4

Please sign in to comment.