check a proof that does not reconstruct against the server chain before reporting it, as a server can report a transaction on a branch before announcing its tip
What changed, and why it matters
This commit fixes a bug in how Sparrow Wallet checks Bitcoin transaction proofs from an Electrum server. Previously, if a server had switched to a different blockchain branch (for example due to a reorganization) but had not yet announced the new tip, Sparrow could wrongly reject valid proofs as false. The fix makes Sparrow reconcile its stored block headers with the server's chain before marking a proof as failed. This reduces the chance of a server tricking the wallet into thinking a real transaction is unconfirmed or on the wrong chain, which could affect balance display or trust decisions.
Review the reconciliation logic to ensure it cannot be abused to accept invalid proofs on a malicious server-controlled chain. Confirm that getReconciledHeader() properly validates the server's chain against proof-of-work and that the fallback to failedProofs remains safe when reconciliation fails. Consider whether additional logging or user notification is warranted when the wallet follows a server onto a previously unknown branch.
Security signals we found
Merkle proof verification bypass/relaxation under reorganization window
Server-side blockchain state synchronization before proof validation
Risk of false-negative proof rejection leading to incorrect transaction confirmation status
Electrum server chain reconciliation added to header store
Evidence from the diff
The change adds getReconciledHeader() in ElectrumServer.java, which attempts to bring the local HeaderStore onto the server’s chain before judging a Merkle proof. In verifyMerkleProofs(), when a proof does not verify against the currently stored header, the code now fetches the server-reconciled header at the claimed height and re-verifies the proof against that. If reconciliation succeeds and the proof matches the server’s branch, the transaction is accepted as proven on that branch. If the server cannot be reached, the failure is treated as a failed RPC call (ServerException) rather than proof fraud. Tests cover a branch-not-yet-announced scenario and a server-unreachable scenario.
Changed components
src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.javasrc/test/java/com/sparrowwallet/sparrow/net/TransactionProofTest.javaElectrum server RPC clientHeaderStore block header cacheMerkle proof verification logicInspect captured patch +91 / −2
### src/main/java/com/sparrowwallet/sparrow/net/ElectrumServer.java
@@ -1390,6 +1390,33 @@ private static boolean isProvenAgainstOrphanedHeader(BlockTransaction existing)
}
}
+ /**
+ * The verified header at a height once the store has been brought onto the server's chain, or null where that could not be done. A server can report
+ * a transaction on a branch it has moved to before announcing that branch's tip, so a proof is only taken as false against the chain the server is on.
+ */
+ private BlockHeader getReconciledHeader(int height) throws ServerException {
+ synchronized(headerSyncLock) {
+ HeaderStore store = getHeaderStore();
+ try {
+ if(height < store.getStartHeight() || height > store.getTipHeight()) {
+ return null;
+ }
+ reconcile(store, store.getTipHeight());
+ } catch(VerificationException e) {
+ log.debug("Could not reconcile the block header store with the server's chain", e);
+ return null; //the server cannot substantiate a chain that replaces the stored one, so the proof stands as false
+ } catch(UnsupportedMethodException e) {
+ throw e;
+ } catch(ElectrumServerRpcException e) {
+ throw new ServerException(e.getMessage(), e.getCause()); //a failed call rather than a finding about the proof
+ } catch(IOException e) {
+ throw new ServerException("Could not write to the block header store", e);
+ }
+ }
+
+ return getVerifiedHeader(height);
+ }
+
/**
* Verifies an inclusion proof for each given (transaction, height) pair, returning those proven with the header they were proven against. What is
* missing was refused or shown false, told apart by what the server did rather than said: one unable to keep up fails whole batches and recovers
@@ -1445,7 +1472,12 @@ private Map<BlockTransactionHash, BlockHeader> verifyMerkleProofs(Wallet wallet,
} else if(verifyProof(reference.getHash(), proof, header)) {
proven.put(reference, header);
} else {
- failedProofs.add(reference); //the branch does not reconstruct against a verified header
+ BlockHeader serverHeader = getReconciledHeader(reference.getHeight());
+ if(serverHeader != null && !serverHeader.getHash().equals(header.getHash()) && verifyProof(reference.getHash(), proof, serverHeader)) {
+ proven.put(reference, serverHeader);
+ } else {
+ failedProofs.add(reference); //the branch does not reconstruct against a verified header
+ }
}
}
}
### src/test/java/com/sparrowwallet/sparrow/net/TransactionProofTest.java
@@ -419,6 +419,53 @@ public void demotesATamperedProof() throws Exception {
assertTrue(listener.getRefused().isEmpty());
}
+ /**
+ * A server can report a transaction on a branch it has moved to before the tip of that branch is announced, leaving the store on the branch it
+ * replaced. The proof is checked against the server's chain before it is taken as false, and the store is brought onto that chain.
+ */
+ @Test
+ public void provesATransactionOnABranchNotYetAnnounced() throws Exception {
+ Wallet wallet = testWallet();
+ Transaction transaction = blockTransactions.getFirst();
+ server.serveTransaction(transaction);
+ server.serveProof(transaction, PROVEN_HEIGHT, 0);
+ reorganiseStoreAbove(PROVEN_HEIGHT - 1);
+ server.serveChain(chain); //the server has moved back to the original branch, and has not yet announced its tip
+ Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap = references(wallet, reference(transaction, PROVEN_HEIGHT));
+
+ ElectrumServer electrumServer = new ElectrumServer();
+ electrumServer.getReferencedTransactions(wallet, nodeTransactionMap);
+ electrumServer.postProofEvents(wallet);
+
+ BlockTransaction written = wallet.getWalletTransaction(transaction.getTxId());
+ assertEquals(PROVEN_HEIGHT, written.getHeight());
+ assertEquals(chain.get(PROVEN_HEIGHT - 1).getHash(), written.getBlockHash());
+ assertEquals(chain.get(PROVEN_HEIGHT - 1).getHash(), ElectrumServer.getHeaderStore().getHash(PROVEN_HEIGHT));
+ assertTrue(listener.getFailed().isEmpty());
+ }
+
+ /**
+ * A proof that does not match the stored header is checked against the server's chain, and a server that cannot be reached for that check has
+ * failed a call rather than shown the proof false: the pass fails, and nothing is reported against the server.
+ */
+ @Test
+ public void failsThePassWhereTheServersChainCannotBeFetchedForAProof() throws Exception {
+ Wallet wallet = testWallet();
+ Transaction transaction = blockTransactions.getFirst();
+ server.serveTransaction(transaction);
+ server.serveProof(transaction, PROVEN_HEIGHT, 0);
+ reorganiseStoreAbove(PROVEN_HEIGHT - 1);
+ server.serveChain(chain);
+ server.setHeadersFailure(new ElectrumServerRpcException("Connection lost"));
+ Map<WalletNode, Set<BlockTransactionHash>> nodeTransactionMap = references(wallet, reference(transaction, PROVEN_HEIGHT));
+
+ ElectrumServer electrumServer = new ElectrumServer();
+ assertThrows(ServerException.class, () -> electrumServer.getReferencedTransactions(wallet, nodeTransactionMap));
+ electrumServer.postProofEvents(wallet);
+
+ assertTrue(listener.getFailed().isEmpty());
+ }
+
/**
* The CVE-2017-12842 forgery: a fake transaction whose txid is one half of a genuine 64 byte transaction mined in the block, with the other half
* presented as its sibling. The verifier rejects it because that concatenation deserializes as a transaction, which no inner node of a real tree
@@ -1324,6 +1371,11 @@ private List<BlockHeader> reorganiseStoreAbove(int forkHeight) throws Exception
replacement.add(previous);
}
+ //The store follows the server, so the server serves the chain the store has been reorganised onto
+ List<BlockHeader> servedChain = new ArrayList<>(chain.subList(0, forkHeight));
+ servedChain.addAll(replacement);
+ server.serveChain(servedChain);
+
return replacement;
}
@@ -1506,7 +1558,7 @@ private static Wallet testWallet() {
* proof for is answered with the error sentinel every attempt, which is a server that will not substantiate what it reported.
*/
private class FakeProofServer extends SimpleElectrumServerRpc {
- private final List<BlockHeader> chain;
+ private volatile List<BlockHeader> chain;
private final Map<String, TransactionMerkleProof> proofs = new HashMap<>();
private final Map<String, Integer> refusals = new HashMap<>();
private final Map<String, String> rawTransactions = new HashMap<>();
@@ -1692,6 +1744,11 @@ public void setServedChainLength(int servedChainLength) {
this.servedChainLength = servedChainLength;
}
+ public void serveChain(List<BlockHeader> chain) {
+ this.chain = List.copyOf(chain);
+ this.servedChainLength = chain.size();
+ }
+
public int getProofRequests() {
return proofRequests.get();
}Why this scored 61/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.