| 532 | } |
| 533 | |
| 534 | std::optional<MigrationData> LegacyDataSPKM::MigrateToDescriptor() |
| 535 | { |
| 536 | LOCK(cs_KeyStore); |
| 537 | if (m_storage.IsLocked()) { |
| 538 | return std::nullopt; |
| 539 | } |
| 540 | |
| 541 | MigrationData out; |
| 542 | |
| 543 | std::unordered_set<CScript, SaltedSipHasher> spks{GetScriptPubKeys()}; |
| 544 | |
| 545 | // Get all key ids |
| 546 | std::set<CKeyID> keyids; |
| 547 | for (const auto& key_pair : mapKeys) { |
| 548 | keyids.insert(key_pair.first); |
| 549 | } |
| 550 | for (const auto& key_pair : mapCryptedKeys) { |
| 551 | keyids.insert(key_pair.first); |
| 552 | } |
| 553 | |
| 554 | // Get key metadata and figure out which keys don't have a seed |
| 555 | // Note that we do not ignore the seeds themselves because they are considered IsMine! |
| 556 | for (auto keyid_it = keyids.begin(); keyid_it != keyids.end();) { |
| 557 | const CKeyID& keyid = *keyid_it; |
| 558 | const auto& it = mapKeyMetadata.find(keyid); |
| 559 | if (it != mapKeyMetadata.end()) { |
| 560 | const CKeyMetadata& meta = it->second; |
| 561 | if (meta.hdKeypath == "s" || meta.hdKeypath == "m") { |
| 562 | keyid_it++; |
| 563 | continue; |
| 564 | } |
| 565 | if (!meta.hd_seed_id.IsNull() && (m_hd_chain.seed_id == meta.hd_seed_id || m_inactive_hd_chains.contains(meta.hd_seed_id))) { |
| 566 | keyid_it = keyids.erase(keyid_it); |
| 567 | continue; |
| 568 | } |
| 569 | } |
| 570 | keyid_it++; |
| 571 | } |
| 572 | |
| 573 | WalletBatch batch(m_storage.GetDatabase()); |
| 574 | if (!batch.TxnBegin()) { |
| 575 | LogWarning("Error generating descriptors for migration, cannot initialize db transaction"); |
| 576 | return std::nullopt; |
| 577 | } |
| 578 | |
| 579 | // keyids is now all non-HD keys. Each key will have its own combo descriptor |
| 580 | for (const CKeyID& keyid : keyids) { |
| 581 | CKey key; |
| 582 | if (!GetKey(keyid, key)) { |
| 583 | assert(false); |
| 584 | } |
| 585 | |
| 586 | // Get birthdate from key meta |
| 587 | uint64_t creation_time = 0; |
| 588 | const auto& it = mapKeyMetadata.find(keyid); |
| 589 | if (it != mapKeyMetadata.end()) { |
| 590 | creation_time = it->second.nCreateTime; |
| 591 | } |
no test coverage detected