Skip to content

Commit

Permalink
wallet: Setup new autogenerated descriptors on construction
Browse files Browse the repository at this point in the history
Instead of having a caller use SetupDescriptorGeneration, just have a
constructor that takes those arguments and sets up the descriptor with
the autogenerated key.
  • Loading branch information
achow101 committed Dec 8, 2023
1 parent f2eb054 commit a5f1781
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,13 @@ DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, con
SetCache(m_wallet_descriptor.cache);
}

DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal)
: ScriptPubKeyMan(storage),
m_keypool_size(keypool_size)
{
SetupDescriptorGeneration(batch, master_key, addr_type, internal);
}

util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
{
// Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
Expand Down
17 changes: 10 additions & 7 deletions src/wallet/scriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,15 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
void AddDescriptorKey(const CKey& key, const CPubKey &pubkey);
void UpdateWithSigningProvider(const FlatSigningProvider& signing_provider);

//! Setup descriptors based on the given CExtkey
bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);

protected:
DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
: ScriptPubKeyMan(storage),
m_keypool_size(keypool_size)
{}

WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);

//! Same as 'TopUp' but designed for use within a batch transaction context
Expand All @@ -595,10 +603,8 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider);
//! Create a DescriptorScriptPubKeyMan from existing data (i.e. during loading)
DescriptorScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys);
DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
: ScriptPubKeyMan(storage),
m_keypool_size(keypool_size)
{}
//! Create an automatically generated DescriptorScriptPubKeyMan
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal);

mutable RecursiveMutex cs_desc_man;

Expand All @@ -621,9 +627,6 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan

bool IsHDEnabled() const override;

//! Setup descriptors based on the given CExtkey
bool SetupDescriptorGeneration(WalletBatch& batch, const CExtKey& master_key, OutputType addr_type, bool internal);

bool HavePrivateKeys() const override;

std::optional<int64_t> GetOldestKeyPoolTime() const override;
Expand Down
3 changes: 1 addition & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3496,7 +3496,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)

for (bool internal : {false, true}) {
for (OutputType t : OUTPUT_TYPES) {
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, m_keypool_size));
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, batch, m_keypool_size, master_key, t, internal));
if (IsCrypted()) {
if (IsLocked()) {
throw std::runtime_error(std::string(__func__) + ": Wallet is locked, cannot setup new descriptors");
Expand All @@ -3505,7 +3505,6 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
throw std::runtime_error(std::string(__func__) + ": Could not encrypt new descriptors");
}
}
spk_manager->SetupDescriptorGeneration(batch, master_key, t, internal);
uint256 id = spk_manager->GetID();
AddScriptPubKeyMan(id, std::move(spk_manager));
AddActiveScriptPubKeyManWithDb(batch, id, t, internal);
Expand Down

0 comments on commit a5f1781

Please sign in to comment.