@@ -354,15 +354,22 @@ bool LegacyScriptPubKeyMan::TopUpInactiveHDChain(const CKeyID seed_id, int64_t i
354
354
return true ;
355
355
}
356
356
357
- void LegacyScriptPubKeyMan::MarkUnusedAddresses (const CScript& script)
357
+ std::vector<WalletDestination> LegacyScriptPubKeyMan::MarkUnusedAddresses (const CScript& script)
358
358
{
359
359
LOCK (cs_KeyStore);
360
+ std::vector<WalletDestination> result;
360
361
// extract addresses and check if they match with an unused keypool key
361
362
for (const auto & keyid : GetAffectedKeys (script, *this )) {
362
363
std::map<CKeyID, int64_t >::const_iterator mi = m_pool_key_to_index.find (keyid);
363
364
if (mi != m_pool_key_to_index.end ()) {
364
365
WalletLogPrintf (" %s: Detected a used keypool key, mark all keypool keys up to this key as used\n " , __func__);
365
- MarkReserveKeysAsUsed (mi->second );
366
+ for (const auto & keypool : MarkReserveKeysAsUsed (mi->second )) {
367
+ // derive all possible destinations as any of them could have been used
368
+ for (const auto & type : LEGACY_OUTPUT_TYPES) {
369
+ const auto & dest = GetDestinationForKey (keypool.vchPubKey , type);
370
+ result.push_back ({dest, keypool.fInternal });
371
+ }
372
+ }
366
373
367
374
if (!TopUp ()) {
368
375
WalletLogPrintf (" %s: Topping up keypool failed (locked wallet)\n " , __func__);
@@ -384,6 +391,8 @@ void LegacyScriptPubKeyMan::MarkUnusedAddresses(const CScript& script)
384
391
}
385
392
}
386
393
}
394
+
395
+ return result;
387
396
}
388
397
389
398
void LegacyScriptPubKeyMan::UpgradeKeyMetadata ()
@@ -1427,14 +1436,15 @@ void LegacyScriptPubKeyMan::LearnAllRelatedScripts(const CPubKey& key)
1427
1436
LearnRelatedScripts (key, OutputType::P2SH_SEGWIT);
1428
1437
}
1429
1438
1430
- void LegacyScriptPubKeyMan::MarkReserveKeysAsUsed (int64_t keypool_id)
1439
+ std::vector<CKeyPool> LegacyScriptPubKeyMan::MarkReserveKeysAsUsed (int64_t keypool_id)
1431
1440
{
1432
1441
AssertLockHeld (cs_KeyStore);
1433
1442
bool internal = setInternalKeyPool.count (keypool_id);
1434
1443
if (!internal) assert (setExternalKeyPool.count (keypool_id) || set_pre_split_keypool.count (keypool_id));
1435
1444
std::set<int64_t > *setKeyPool = internal ? &setInternalKeyPool : (set_pre_split_keypool.empty () ? &setExternalKeyPool : &set_pre_split_keypool);
1436
1445
auto it = setKeyPool->begin ();
1437
1446
1447
+ std::vector<CKeyPool> result;
1438
1448
WalletBatch batch (m_storage.GetDatabase ());
1439
1449
while (it != std::end (*setKeyPool)) {
1440
1450
const int64_t & index = *(it);
@@ -1448,7 +1458,10 @@ void LegacyScriptPubKeyMan::MarkReserveKeysAsUsed(int64_t keypool_id)
1448
1458
batch.ErasePool (index);
1449
1459
WalletLogPrintf (" keypool index %d removed\n " , index);
1450
1460
it = setKeyPool->erase (it);
1461
+ result.push_back (std::move (keypool));
1451
1462
}
1463
+
1464
+ return result;
1452
1465
}
1453
1466
1454
1467
std::vector<CKeyID> GetAffectedKeys (const CScript& spk, const SigningProvider& provider)
@@ -1820,19 +1833,32 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
1820
1833
return true ;
1821
1834
}
1822
1835
1823
- void DescriptorScriptPubKeyMan::MarkUnusedAddresses (const CScript& script)
1836
+ std::vector<WalletDestination> DescriptorScriptPubKeyMan::MarkUnusedAddresses (const CScript& script)
1824
1837
{
1825
1838
LOCK (cs_desc_man);
1839
+ std::vector<WalletDestination> result;
1826
1840
if (IsMine (script)) {
1827
1841
int32_t index = m_map_script_pub_keys[script];
1828
1842
if (index >= m_wallet_descriptor.next_index ) {
1829
1843
WalletLogPrintf (" %s: Detected a used keypool item at index %d, mark all keypool items up to this item as used\n " , __func__, index);
1830
- m_wallet_descriptor.next_index = index + 1 ;
1844
+ auto out_keys = std::make_unique<FlatSigningProvider>();
1845
+ std::vector<CScript> scripts_temp;
1846
+ while (index >= m_wallet_descriptor.next_index ) {
1847
+ if (!m_wallet_descriptor.descriptor ->ExpandFromCache (m_wallet_descriptor.next_index , m_wallet_descriptor.cache , scripts_temp, *out_keys)) {
1848
+ throw std::runtime_error (std::string (__func__) + " : Unable to expand descriptor from cache" );
1849
+ }
1850
+ CTxDestination dest;
1851
+ ExtractDestination (scripts_temp[0 ], dest);
1852
+ result.push_back ({dest, std::nullopt});
1853
+ m_wallet_descriptor.next_index ++;
1854
+ }
1831
1855
}
1832
1856
if (!TopUp ()) {
1833
1857
WalletLogPrintf (" %s: Topping up keypool failed (locked wallet)\n " , __func__);
1834
1858
}
1835
1859
}
1860
+
1861
+ return result;
1836
1862
}
1837
1863
1838
1864
void DescriptorScriptPubKeyMan::AddDescriptorKey (const CKey& key, const CPubKey &pubkey)
0 commit comments