diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -640,6 +640,9 @@ //! keeps track of whether Unlock has run a thorough check before bool m_decryption_thoroughly_checked = false; + bool AddDescriptorKeyWithDB(WalletBatch &batch, const CKey &key, + const CPubKey &pubkey); + public: DescriptorScriptPubKeyMan(WalletStorage &storage, WalletDescriptor &descriptor) diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -1574,6 +1574,32 @@ } } +bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch &batch, + const CKey &key, + const CPubKey &pubkey) { + AssertLockHeld(cs_desc_man); + assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)); + + if (m_storage.HasEncryptionKeys()) { + if (m_storage.IsLocked()) { + return false; + } + + std::vector crypted_secret; + CKeyingMaterial secret(key.begin(), key.end()); + if (!EncryptSecret(m_storage.GetEncryptionKey(), secret, + pubkey.GetHash(), crypted_secret)) { + return false; + } + + m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret); + return batch.WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret); + } else { + m_map_keys[pubkey.GetID()] = key; + return batch.WriteDescriptorKey(GetID(), pubkey, key.GetPrivKey()); + } +} + bool DescriptorScriptPubKeyMan::IsHDEnabled() const { LOCK(cs_desc_man); return m_wallet_descriptor.descriptor->IsRange(); diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -12,6 +12,7 @@ #include