From c927fb60baa87bf596545caadb8cbf3b10996f87 Mon Sep 17 00:00:00 2001 From: Steven Barclay Date: Sun, 8 Sep 2024 08:09:08 +0800 Subject: [PATCH] Use watched scripts to pick up broadcast of staged txs Since the multisig escrow outputs of the deposit & warning txs do not belong to the user, bitcoinj won't pick up any txs spending them unless a corresponding watched script (the ScriptPubKey) is added to the wallet. To this end, provide a trade task to add watched scripts for those three outputs, which runs just before the client or the peer might broadcast the deposit tx. Also remove them upon withdrawal of funds at the end of the trade (closed normally or through a dispute). We need to add watched scripts for the deposit tx output and both the user's and the peer's warning tx outputs, so that the peer's warning, redirect and claim txs are all picked up, regardless of any message sent to the client. TODO: Possibly find a way to clear out old watched scripts from failed trades, as they will otherwise remain in the user's wallet permanently, creating a growing burden for the wallet. Also, we should possibly re- add all the watched scripts if the wallet is restored from seed. --- .../java/bisq/core/trade/TradeManager.java | 6 ++ .../bisq/core/trade/model/bisq_v1/Trade.java | 26 ++++++++ .../bisq_v5/BaseSellerProtocol_v5.java | 2 + .../bisq_v5/BuyerAsMakerProtocol_v5.java | 2 + .../bisq_v5/BuyerAsTakerProtocol_v5.java | 2 + .../bisq_v5/SellerAsMakerProtocol_v5.java | 2 + .../tasks/AddWatchedScriptsToWallet.java | 59 +++++++++++++++++++ 7 files changed, 99 insertions(+) create mode 100644 core/src/main/java/bisq/core/trade/protocol/bisq_v5/tasks/AddWatchedScriptsToWallet.java diff --git a/core/src/main/java/bisq/core/trade/TradeManager.java b/core/src/main/java/bisq/core/trade/TradeManager.java index 30c609f6f2f..3e3fa88069d 100644 --- a/core/src/main/java/bisq/core/trade/TradeManager.java +++ b/core/src/main/java/bisq/core/trade/TradeManager.java @@ -92,6 +92,7 @@ import org.bitcoinj.core.InsufficientMoneyException; import org.bitcoinj.core.Transaction; import org.bitcoinj.core.TransactionConfidence; +import org.bitcoinj.script.Script; import javax.inject.Inject; import javax.inject.Named; @@ -723,6 +724,11 @@ public void onTradeCompleted(Trade trade) { // TODO The address entry should have been removed already. Check and if its the case remove that. btcWalletService.resetAddressEntriesForPendingTrade(trade.getId()); + // FIXME: If the trade fails, any watched scripts will remain in the wallet permanently, which is not ideal. + List