fix: unify ScriptPubKeyMan implementation with bitcoin's

This commit is contained in:
Konstantin Akimov 2024-03-08 00:49:06 +07:00
parent 953b6706f4
commit d04c1a703e
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
2 changed files with 9 additions and 7 deletions

View File

@ -299,6 +299,7 @@ bool LegacyScriptPubKeyMan::GetReservedDestination(bool internal, CTxDestination
if (!ReserveKeyFromKeyPool(index, keypool, internal)) { if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
return false; return false;
} }
// TODO: unify with bitcoin and use here GetDestinationForKey even if we have no type
address = PKHash(keypool.vchPubKey); address = PKHash(keypool.vchPubKey);
return true; return true;
} }
@ -1214,13 +1215,13 @@ bool LegacyScriptPubKeyMan::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& inf
return true; return true;
} }
bool LegacyScriptPubKeyMan::AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info) bool LegacyScriptPubKeyMan::AddKeyOriginWithDB(WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info)
{ {
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
std::copy(info.fingerprint, info.fingerprint + 4, mapKeyMetadata[pubkey.GetID()].key_origin.fingerprint); std::copy(info.fingerprint, info.fingerprint + 4, mapKeyMetadata[pubkey.GetID()].key_origin.fingerprint);
mapKeyMetadata[pubkey.GetID()].key_origin.path = info.path; mapKeyMetadata[pubkey.GetID()].key_origin.path = info.path;
mapKeyMetadata[pubkey.GetID()].has_key_origin = true; mapKeyMetadata[pubkey.GetID()].has_key_origin = true;
return WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true); return batch.WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true);
} }
bool LegacyScriptPubKeyMan::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const bool LegacyScriptPubKeyMan::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
@ -1367,6 +1368,9 @@ void LegacyScriptPubKeyMan::LoadKeyPool(int64_t nIndex, const CKeyPool &keypool)
bool LegacyScriptPubKeyMan::CanGenerateKeys() const bool LegacyScriptPubKeyMan::CanGenerateKeys() const
{ {
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
// TODO : unify with bitcoin after backporting SetupGeneration
// return IsHDEnabled() || !m_storage.CanSupportFeature(FEATURE_HD);
if (m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) || m_storage.IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET)) { if (m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) || m_storage.IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET)) {
return false; return false;
} }
@ -1713,6 +1717,9 @@ bool LegacyScriptPubKeyMan::ImportPrivKeys(const std::map<CKeyID, CKey>& privkey
bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp)
{ {
WalletBatch batch(m_storage.GetDatabase()); WalletBatch batch(m_storage.GetDatabase());
for (const auto& entry : key_origins) {
AddKeyOriginWithDB(batch, entry.second.first, entry.second.second);
}
for (const CKeyID& id : ordered_pubkeys) { for (const CKeyID& id : ordered_pubkeys) {
auto entry = pubkey_map.find(id); auto entry = pubkey_map.find(id);
if (entry == pubkey_map.end()) { if (entry == pubkey_map.end()) {
@ -1735,9 +1742,6 @@ bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector<CKeyID>& ordered_pub
NotifyCanGetAddressesChanged(); NotifyCanGetAddressesChanged();
} }
} }
for (const auto& entry : key_origins) {
AddKeyOrigin(entry.second.first, entry.second.second);
}
return true; return true;
} }

View File

@ -433,8 +433,6 @@ public:
bool AddCScript(const CScript& redeemScript) override; bool AddCScript(const CScript& redeemScript) override;
/** Implement lookup of key origin information through wallet key metadata. */ /** Implement lookup of key origin information through wallet key metadata. */
bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override; bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
/** Add a KeyOriginInfo to the wallet */
bool AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info);
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool); void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool);
bool NewKeyPool(); bool NewKeyPool();