leave incomplete transaction entries out of the wallet balance when the transactions model is first built, and check entry completeness with set lookups
What changed, and why it matters
This commit fixes a bug in how Sparrow Wallet calculates your balance when the transactions list is first loaded. Previously, if a transaction record was incomplete (for example, because the wallet hadn't yet fully linked a spending reference), it could still be included in the displayed balance, making the balance look wrong. Now, incomplete entries are filtered out when the model is first built, and the code checks completeness more efficiently using set lookups. There is no direct evidence this is a security vulnerability, but incorrect balance display could mislead users.
Treat as a bug fix rather than a security vulnerability. Users should upgrade to a version containing this commit to ensure wallet balances are displayed accurately. No immediate incident response is warranted unless incorrect balances caused user error.
Security signals we found
Balance calculation could include incomplete transaction entries, leading to incorrect displayed wallet balance
Previous completeness check compared HashIndex string representations rather than object equality, which is less reliable
Fix ensures incomplete entries are excluded from initial model construction, not just during updates
No cryptographic, networking, or privilege-related changes observed
Evidence from the diff
The patch changes TransactionEntry.isComplete() to precompute sets of child input/output HashIndexes instead of doing expensive and potentially fragile stream string comparisons. It also changes WalletTransactionsEntry’s constructor to remove incomplete transaction entries before calculating balances, and extracts a shared getWalletTxos() helper used both on initial model construction and during updates. A new unit test verifies that an incomplete transfer is excluded from a freshly built model until the spending reference is recorded.
Changed components
src/main/java/com/sparrowwallet/sparrow/wallet/TransactionEntry.javasrc/main/java/com/sparrowwallet/sparrow/wallet/WalletTransactionsEntry.javaWallet transaction model / balance calculationTransactions tab UI balance displayInspect captured patch +57 / −6
### src/main/java/com/sparrowwallet/sparrow/wallet/TransactionEntry.java
@@ -96,14 +96,16 @@ public boolean isCoinbase() {
}
public boolean isComplete(Map<HashIndex, BlockTransactionHashIndex> walletTxos) {
+ Set<HashIndex> inputChildren = getChildHashIndexes(HashIndexEntry.Type.INPUT);
+ Set<HashIndex> outputChildren = getChildHashIndexes(HashIndexEntry.Type.OUTPUT);
int validEntries = 0;
for(TransactionInput txInput : blockTransaction.getTransaction().getInputs()) {
BlockTransactionHashIndex ref = walletTxos.get(new HashIndex(txInput.getOutpoint().getHash(), txInput.getOutpoint().getIndex()));
if(ref != null) {
validEntries++;
if(!ref.isSpent()) {
log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getFullName() + " missing spending reference on output " + ref);
- } else if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().toString().equals(ref.getSpentBy().toString()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.INPUT))) {
+ } else if(!inputChildren.contains(new HashIndex(ref.getSpentBy().getHash(), ref.getSpentBy().getIndex()))) {
log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getFullName() + " missing child for input " + ref.getSpentBy() + " on output " + ref);
return false;
}
@@ -113,7 +115,7 @@ public boolean isComplete(Map<HashIndex, BlockTransactionHashIndex> walletTxos)
BlockTransactionHashIndex ref = walletTxos.get(new HashIndex(txOutput.getHash(), txOutput.getIndex()));
if(ref != null) {
validEntries++;
- if(getChildren().stream().noneMatch(entry -> ((HashIndexEntry)entry).getHashIndex().toString().equals(ref.toString()) && ((HashIndexEntry)entry).getType().equals(HashIndexEntry.Type.OUTPUT))) {
+ if(!outputChildren.contains(new HashIndex(ref.getHash(), ref.getIndex()))) {
log.warn("TransactionEntry " + blockTransaction.getHash() + " for wallet " + getWallet().getFullName() + " missing child for output " + ref);
return false;
}
@@ -128,6 +130,11 @@ public boolean isComplete(Map<HashIndex, BlockTransactionHashIndex> walletTxos)
return true;
}
+ private Set<HashIndex> getChildHashIndexes(HashIndexEntry.Type type) {
+ return getChildren().stream().map(HashIndexEntry.class::cast).filter(entry -> entry.getType() == type)
+ .map(entry -> new HashIndex(entry.getHashIndex().getHash(), entry.getHashIndex().getIndex())).collect(Collectors.toSet());
+ }
+
private static List<Entry> createChildEntries(Wallet wallet, Map<BlockTransactionHashIndex, KeyPurpose> incoming, Map<BlockTransactionHashIndex, KeyPurpose> outgoing) {
List<Entry> incomingOutputEntries = incoming.entrySet().stream().map(input -> new TransactionHashIndexEntry(wallet, input.getKey(), HashIndexEntry.Type.OUTPUT, input.getValue())).collect(Collectors.toList());
List<Entry> outgoingInputEntries = outgoing.entrySet().stream().map(output -> new TransactionHashIndexEntry(wallet, output.getKey(), HashIndexEntry.Type.INPUT, output.getValue())).collect(Collectors.toList());
### src/main/java/com/sparrowwallet/sparrow/wallet/WalletTransactionsEntry.java
@@ -28,6 +28,8 @@ public WalletTransactionsEntry(Wallet wallet) {
public WalletTransactionsEntry(Wallet wallet, boolean includeAllChildWallets) {
super(wallet, wallet.getDisplayName(), getWalletTransactions(wallet, includeAllChildWallets).stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toList()));
+ Map<HashIndex, BlockTransactionHashIndex> walletTxos = getWalletTxos(wallet, includeAllChildWallets);
+ getChildren().removeIf(entry -> !((TransactionEntry)entry).isComplete(walletTxos));
calculateBalances(false); //No need to resort
}
@@ -73,10 +75,7 @@ private void calculateBalances(boolean resort) {
}
public void updateTransactions() {
- Map<HashIndex, BlockTransactionHashIndex> walletTxos = getWallet().getWalletTxos().entrySet().stream()
- .collect(Collectors.toUnmodifiableMap(entry -> new HashIndex(entry.getKey().getHash(), entry.getKey().getIndex()), Map.Entry::getKey,
- BinaryOperator.maxBy(BlockTransactionHashIndex::compareTo)));
-
+ Map<HashIndex, BlockTransactionHashIndex> walletTxos = getWalletTxos(getWallet(), false);
Collection<WalletTransactionsEntry.WalletTransaction> entries = getWalletTransactions(getWallet(), false);
Set<Entry> current = entries.stream().map(WalletTransaction::getTransactionEntry).collect(Collectors.toCollection(LinkedHashSet::new));
Set<Entry> previous = new LinkedHashSet<>(getChildren());
@@ -119,6 +118,16 @@ public void unregisterForConfirmations() {
getChildren().forEach(entry -> ((TransactionEntry)entry).unregisterForConfirmations());
}
+ private static Map<HashIndex, BlockTransactionHashIndex> getWalletTxos(Wallet wallet, boolean includeAllChildWallets) {
+ List<Wallet> wallets = new ArrayList<>(List.of(wallet));
+ if(includeAllChildWallets) {
+ wallets.addAll(wallet.getChildWallets());
+ }
+
+ return wallets.stream().flatMap(txoWallet -> txoWallet.getWalletTxos().keySet().stream())
+ .collect(Collectors.toUnmodifiableMap(txo -> new HashIndex(txo.getHash(), txo.getIndex()), txo -> txo, BinaryOperator.maxBy(BlockTransactionHashIndex::compareTo)));
+ }
+
private static Collection<WalletTransaction> getWalletTransactions(Wallet wallet, boolean includeAllChildWallets) {
Map<BlockTransaction, WalletTransaction> walletTransactionMap = new HashMap<>(wallet.getTransactions().size());
### src/test/java/com/sparrowwallet/sparrow/wallet/WalletTransactionsEntryTest.java
@@ -185,6 +185,41 @@ public void incompleteEntryIsLeftOutOfTheBalanceAndTheNotification() {
}
}
+ /**
+ * A model built from a wallet whose history already holds an incomplete transfer, as when the transactions tab is first opened, leaves it out the same way.
+ */
+ @Test
+ public void incompleteEntryIsLeftOutOfANewModel() {
+ Wallet wallet = testWallet();
+ BlockTransaction funding = receive(wallet, 0, 100000L);
+ BlockTransaction transfer = transfer(wallet, funding, 1, 90000L);
+ WalletTransactionsEntry walletTransactionsEntry = new WalletTransactionsEntry(wallet);
+
+ NotificationListener listener = new NotificationListener();
+ EventManager.get().register(listener);
+ try {
+ assertEquals(1, walletTransactionsEntry.getChildren().size());
+ assertEquals(100000L, walletTransactionsEntry.getBalance());
+
+ //Once the funding node records the spend, a refresh adds the entry at what it moved, and a new model includes it
+ BlockTransactionHashIndex fundingOutput = receiveNode(wallet, 0).getTransactionOutputs().iterator().next();
+ fundingOutput.setSpentBy(new BlockTransactionHashIndex(transfer.getHash(), HEIGHT, transfer.getDate(), null, 0, 100000L));
+ walletTransactionsEntry.updateTransactions();
+
+ assertEquals(2, walletTransactionsEntry.getChildren().size());
+ assertEquals(90000L, walletTransactionsEntry.getBalance());
+ assertEquals(1, listener.events.size());
+ assertEquals(List.of(transfer), listener.events.getFirst().getBlockTransactions());
+
+ WalletTransactionsEntry rebuilt = new WalletTransactionsEntry(wallet);
+ assertEquals(2, rebuilt.getChildren().size());
+ assertEquals(90000L, rebuilt.getBalance());
+ } finally {
+ EventManager.get().unregister(listener);
+ walletTransactionsEntry.unregisterForConfirmations();
+ }
+ }
+
private static BlockTransaction transfer(Wallet wallet, BlockTransaction funding, int index, long value) {
WalletNode node = receiveNode(wallet, index);
Transaction transaction = new Transaction();Why this scored 46/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.