From cce4336f432976585be64cc6fc30487d67e37581 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 26 Apr 2023 17:05:45 -0600 Subject: [PATCH] ensure all addresses with history have been generated before scanning for tx history on restore --- .../coins/bitcoin/bitcoin_wallet.dart | 70 +++++++++++++++++-- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/lib/services/coins/bitcoin/bitcoin_wallet.dart b/lib/services/coins/bitcoin/bitcoin_wallet.dart index f29d2f55a..ba49eefc0 100644 --- a/lib/services/coins/bitcoin/bitcoin_wallet.dart +++ b/lib/services/coins/bitcoin/bitcoin_wallet.dart @@ -682,12 +682,8 @@ class BitcoinWallet extends CoinServiceAPI // isSegwit does not matter here at all final myCode = await getPaymentCode(isSegwit: false); - // this will add the notification address to the db if it isn't - // already there so it can be watched - await getMyNotificationAddress(); - // refresh transactions to pick up any received notification transactions - await _refreshTransactions(); + await _refreshNotificationAddressTransactions(); try { final Set codesToCheck = {}; @@ -716,7 +712,10 @@ class BitcoinWallet extends CoinServiceAPI ); } - await _updateUTXOs(); + await Future.wait([ + _refreshTransactions(), + _updateUTXOs(), + ]); await Future.wait([ updateCachedId(walletId), @@ -1247,6 +1246,11 @@ class BitcoinWallet extends CoinServiceAPI } await _prefs.init(); + + // this will add the notification address to the db if it isn't + // already there for older wallets + await getMyNotificationAddress(); + // await _checkCurrentChangeAddressesForTransactions(); // await _checkCurrentReceivingAddressesForTransactions(); } @@ -2016,6 +2020,60 @@ class BitcoinWallet extends CoinServiceAPI return false; } + Future _refreshNotificationAddressTransactions() async { + final address = await getMyNotificationAddress(); + final hashes = await _fetchHistory([address.value]); + + List> allTransactions = []; + + final currentHeight = await chainHeight; + + for (final txHash in hashes) { + final storedTx = await db + .getTransactions(walletId) + .filter() + .txidEqualTo(txHash["tx_hash"] as String) + .findFirst(); + + // TODO: remove bip47Notification type check sometime after Q2 2023 + if (storedTx == null || + storedTx.subType == + isar_models.TransactionSubType.bip47Notification || + !storedTx.isConfirmed(currentHeight, MINIMUM_CONFIRMATIONS)) { + final tx = await cachedElectrumXClient.getTransaction( + txHash: txHash["tx_hash"] as String, + verbose: true, + coin: coin, + ); + + tx["address"] = await db + .getAddresses(walletId) + .filter() + .valueEqualTo(txHash["address"] as String) + .findFirst(); + tx["height"] = txHash["height"]; + allTransactions.add(tx); + } + } + + final List> txnsData = + []; + + for (final txObject in allTransactions) { + final data = await parseTransaction( + txObject, + cachedElectrumXClient, + [address], + coin, + MINIMUM_CONFIRMATIONS, + walletId, + ); + + txnsData.add(data); + } + await db.addNewTransactionData(txnsData, walletId); + } + Future _refreshTransactions() async { final List allAddresses = await _fetchAllOwnAddresses();