Skip to content

Commit 46dfb99

Browse files
committed
Implement writing descriptorkeys, descriptorckeys, and descriptors to wallet file
1 parent 4cb9b69 commit 46dfb99

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,30 @@ void DescriptorScriptPubKeyMan::MarkUnusedAddresses(const CScript& script)
15531553
}
15541554
}
15551555

1556+
bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey)
1557+
{
1558+
AssertLockHeld(cs_desc_man);
1559+
assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
1560+
1561+
if (m_storage.HasEncryptionKeys()) {
1562+
if (m_storage.IsLocked()) {
1563+
return false;
1564+
}
1565+
1566+
std::vector<unsigned char> crypted_secret;
1567+
CKeyingMaterial secret(key.begin(), key.end());
1568+
if (!EncryptSecret(m_storage.GetEncryptionKey(), secret, pubkey.GetHash(), crypted_secret)) {
1569+
return false;
1570+
}
1571+
1572+
m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
1573+
return batch.WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
1574+
} else {
1575+
m_map_keys[pubkey.GetID()] = key;
1576+
return batch.WriteDescriptorKey(GetID(), pubkey, key.GetPrivKey());
1577+
}
1578+
}
1579+
15561580
bool DescriptorScriptPubKeyMan::IsHDEnabled() const
15571581
{
15581582
LOCK(cs_desc_man);

src/wallet/scriptpubkeyman.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
503503

504504
//! keeps track of whether Unlock has run a thorough check before
505505
bool m_decryption_thoroughly_checked = false;
506+
507+
bool AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey);
506508
public:
507509
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor)
508510
: ScriptPubKeyMan(storage),

src/wallet/walletdb.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,31 @@ bool WalletBatch::WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bo
191191
return WriteIC(make_pair(key, type), id);
192192
}
193193

194+
bool WalletBatch::WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey)
195+
{
196+
// hash pubkey/privkey to accelerate wallet load
197+
std::vector<unsigned char> key;
198+
key.reserve(pubkey.size() + privkey.size());
199+
key.insert(key.end(), pubkey.begin(), pubkey.end());
200+
key.insert(key.end(), privkey.begin(), privkey.end());
201+
202+
return WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)), std::make_pair(privkey, Hash(key.begin(), key.end())), false);
203+
}
204+
205+
bool WalletBatch::WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret)
206+
{
207+
if (!WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORCKEY, std::make_pair(desc_id, pubkey)), secret, false)) {
208+
return false;
209+
}
210+
EraseIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)));
211+
return true;
212+
}
213+
214+
bool WalletBatch::WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor)
215+
{
216+
return WriteIC(make_pair(DBKeys::WALLETDESCRIPTOR, desc_id), descriptor);
217+
}
218+
194219
class CWalletScanState {
195220
public:
196221
unsigned int nKeys{0};

src/wallet/walletdb.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <amount.h>
1010
#include <script/sign.h>
1111
#include <wallet/db.h>
12+
#include <wallet/walletutil.h>
1213
#include <key.h>
1314

1415
#include <stdint.h>
@@ -245,6 +246,10 @@ class WalletBatch
245246

246247
bool WriteMinVersion(int nVersion);
247248

249+
bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
250+
bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
251+
bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
252+
248253
/// Write destination data key,value tuple to database
249254
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
250255
/// Erase destination data tuple from wallet database

0 commit comments

Comments
 (0)