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 Aug 24, 2023
1 parent 2ca1fca commit d91026a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/wallet/external_signer_scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace wallet {
ExternalSignerScriptPubKeyMan::ExternalSignerScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size, std::unique_ptr<Descriptor> desc)
: DescriptorScriptPubKeyMan(storage, keypool_size)
: DescriptorScriptPubKeyMan(storage, keypool_size)
{
LOCK(cs_desc_man);
assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
Expand Down
7 changes: 7 additions & 0 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,13 @@ DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, con
SetCache(m_wallet_descriptor.cache);
}

DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal)
: ScriptPubKeyMan(storage),
m_keypool_size(keypool_size)
{
SetupDescriptorGeneration(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
19 changes: 11 additions & 8 deletions src/wallet/scriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,18 +584,24 @@ 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(const CExtKey& master_key, OutputType addr_type, bool internal);

protected:
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);

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

public:
//! Create a new DescriptorScriptPubKeyMan from an existing descriptor (i.e. from an import)
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, int64_t keypool_size, const CExtKey& master_key, OutputType addr_type, bool internal);

mutable RecursiveMutex cs_desc_man;

Expand All @@ -618,9 +624,6 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan

bool IsHDEnabled() const override;

//! Setup descriptors based on the given CExtkey
bool SetupDescriptorGeneration(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 @@ -3560,7 +3560,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, 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 @@ -3569,7 +3569,6 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
throw std::runtime_error(std::string(__func__) + ": Could not encrypt new descriptors");
}
}
spk_manager->SetupDescriptorGeneration(master_key, t, internal);
uint256 id = spk_manager->GetID();
AddScriptPubKeyMan(id, std::move(spk_manager));
AddActiveScriptPubKeyMan(id, t, internal);
Expand Down

0 comments on commit d91026a

Please sign in to comment.