Skip to content

Commit

Permalink
Implement SetupGeneration for DescriptorScriptPubKeyMan
Browse files Browse the repository at this point in the history
Summary:
Backport of Core [[bitcoin/bitcoin#16528 | PR16528]] [20/43] : bitcoin/bitcoin@e014886

Depends on D8417

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, majcosta

Reviewed By: #bitcoin_abc, PiRK, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D8418
  • Loading branch information
achow101 authored and deadalnix committed Nov 16, 2020
1 parent 85b7c1a commit 2093160
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <chainparams.h>
#include <config.h>
#include <key_io.h>
#include <outputtype.h>
Expand Down Expand Up @@ -1600,6 +1601,69 @@ bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch &batch,
}
}

bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(
const CExtKey &master_key) {
LOCK(cs_desc_man);
assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));

// Ignore when there is already a descriptor
if (m_wallet_descriptor.descriptor) {
return false;
}

int64_t creation_time = GetTime();

std::string xpub = EncodeExtPubKey(master_key.Neuter());

// Build descriptor string
std::string desc_prefix;
std::string desc_suffix = "/*)";
switch (m_address_type) {
case OutputType::LEGACY: {
desc_prefix = "pkh(" + xpub + "/44'";
break;
}
default:
assert(false);
}

// Mainnet derives at 0', testnet and regtest derive at 1'
if (m_storage.GetChainParams().IsTestChain()) {
desc_prefix += "/1'";
} else {
desc_prefix += "/0'";
}

std::string internal_path = m_internal ? "/1" : "/0";
std::string desc_str = desc_prefix + "/0'" + internal_path + desc_suffix;

// Make the descriptor
FlatSigningProvider keys;
std::string error;
std::unique_ptr<Descriptor> desc = Parse(desc_str, keys, error, false);
WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
m_wallet_descriptor = w_desc;

// Store the master private key, and descriptor
WalletBatch batch(m_storage.GetDatabase());
if (!AddDescriptorKeyWithDB(batch, master_key.key,
master_key.key.GetPubKey())) {
throw std::runtime_error(
std::string(__func__) +
": writing descriptor master private key failed");
}
if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
throw std::runtime_error(std::string(__func__) +
": writing descriptor failed");
}

// TopUp
TopUp();

m_storage.UnsetBlankWalletFlag(batch);
return true;
}

bool DescriptorScriptPubKeyMan::IsHDEnabled() const {
LOCK(cs_desc_man);
return m_wallet_descriptor.descriptor->IsRange();
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/scriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <boost/signals2/signal.hpp>

enum class OutputType;
class CChainParams;
struct bilingual_str;

// Wallet storage things that ScriptPubKeyMans need in order to be able to store
Expand All @@ -33,6 +34,7 @@ class WalletStorage {
virtual ~WalletStorage() = default;
virtual const std::string GetDisplayName() const = 0;
virtual WalletDatabase &GetDatabase() = 0;
virtual const CChainParams &GetChainParams() const = 0;
virtual bool IsWalletFlagSet(uint64_t) const = 0;
virtual void UnsetBlankWalletFlag(WalletBatch &) = 0;
virtual bool CanSupportFeature(enum WalletFeature) const = 0;
Expand Down Expand Up @@ -675,6 +677,9 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan {

bool IsHDEnabled() const override;

//! Setup descriptors based on the given CExtkey
bool SetupDescriptorGeneration(const CExtKey &master_key);

bool HavePrivateKeys() const override;

int64_t GetOldestKeyPoolTime() const override;
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,9 @@ class CWallet final : public WalletStorage,
assert(NotifyUnload.empty());
}

/* Returns the chain params used by this wallet. */
const CChainParams &GetChainParams() const override { return chainParams; }

bool IsCrypted() const;
bool IsLocked() const override;
bool Lock();
Expand Down

0 comments on commit 2093160

Please sign in to comment.