Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electrum utxo fixes #1819

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions cw_bitcoin/lib/bitcoin_address_record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abstract class BaseBitcoinAddressRecord {
BaseBitcoinAddressRecord(
this.address, {
required this.index,
this.isHidden = false,
required this.isHidden,
int txCount = 0,
int balance = 0,
String name = '',
Expand Down Expand Up @@ -56,7 +56,7 @@ class BitcoinAddressRecord extends BaseBitcoinAddressRecord {
BitcoinAddressRecord(
super.address, {
required super.index,
super.isHidden = false,
required super.isHidden,
super.txCount = 0,
super.balance = 0,
super.name = '',
Expand Down
25 changes: 12 additions & 13 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -925,13 +925,13 @@ abstract class ElectrumWalletBase
);
} else {
// Here, lastOutput already is change, return the amount left without the fee to the user's address.
updatedOutputs[updatedOutputs.length - 1] = BitcoinOutput(
updatedOutputs.last = BitcoinOutput(
address: lastOutput.address,
value: BigInt.from(amountLeftForChange),
isSilentPayment: lastOutput.isSilentPayment,
isChange: true,
);
outputs[outputs.length - 1] = BitcoinOutput(
outputs.last = BitcoinOutput(
address: lastOutput.address,
value: BigInt.from(amountLeftForChange),
isSilentPayment: lastOutput.isSilentPayment,
Expand Down Expand Up @@ -1378,20 +1378,16 @@ abstract class ElectrumWalletBase
updatedUnspentCoins.addAll(await fetchUnspent(address));
}));

unspentCoins = updatedUnspentCoins;

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

await updateCoins(unspentCoins);
unspentCoins = await updateCoinsWithInfoFromBox(updatedUnspentCoins);
await _refreshUnspentCoinsInfo();
}

Future<void> updateCoins(List<BitcoinUnspent> newUnspentCoins) async {
Future<List<BitcoinUnspent>> updateCoinsWithInfoFromBox(List<BitcoinUnspent> newUnspentCoins) async {
// this function updates and returns unspent coins list (freshly fetched from the server)
// with info from the box, if the box doesn't have info for some of the unspents, it adds them to the box

if (newUnspentCoins.isEmpty) {
return;
return newUnspentCoins;
}

newUnspentCoins.forEach((coin) {
Expand All @@ -1414,12 +1410,14 @@ abstract class ElectrumWalletBase
addCoinInfo(coin);
}
});

return newUnspentCoins;
}

@action
Future<void> updateUnspentsForAddress(BitcoinAddressRecord address) async {
final newUnspentCoins = await fetchUnspent(address);
await updateCoins(newUnspentCoins);
unspentCoins = await updateCoinsWithInfoFromBox(newUnspentCoins);
}

@action
Expand Down Expand Up @@ -1461,6 +1459,7 @@ abstract class ElectrumWalletBase
await unspentCoinsInfo.add(newInfo);
}

// delete unspent coins info entries that don't have a corresponding unspent coin:
Future<void> _refreshUnspentCoinsInfo() async {
try {
final List<dynamic> keys = <dynamic>[];
Expand Down
6 changes: 3 additions & 3 deletions cw_bitcoin/lib/litecoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,11 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
mwebUnspentCoins.add(unspent);
});

// copy coin control attributes to mwebCoins:
await updateCoins(mwebUnspentCoins);
// copy coin control attributes to coinsInfo:
mwebUnspentCoins = await updateCoinsWithInfoFromBox(mwebUnspentCoins);
// get regular ltc unspents (this resets unspentCoins):
await super.updateAllUnspents();
// add the mwebCoins:
// add back the mwebCoins:
unspentCoins.addAll(mwebUnspentCoins);
}

Expand Down
2 changes: 2 additions & 0 deletions cw_bitcoin/lib/litecoin_wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
index: e.key,
type: SegwitAddresType.mweb,
network: network,
isHidden: false,
))
.toList();
addMwebAddresses(addressRecords);
Expand Down Expand Up @@ -195,6 +196,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
index: 0,
type: SegwitAddresType.mweb,
network: network,
isHidden: true,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ abstract class UnspentCoinsListViewModelBase with Store {
}
});

// sort by hash so that even if the addresses are the same, they don't jump around when selected (because that calls updateBalance)
unspents.sort((a, b) => a.hash.compareTo(b.hash));

// sort unspents by address so that if something changes it's easier to see:
unspents.sort((a, b) => a.address.compareTo(b.address));

// sort change addresses to the end:
unspents.sort((a, b) => a.isChange ? 1 : -1);

_items.addAll(unspents);
}
}