Skip to content

Commit e014886

Browse files
committed
Implement SetupGeneration for DescriptorScriptPubKeyMan
1 parent 46dfb99 commit e014886

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,73 @@ bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch& batch, const
15771577
}
15781578
}
15791579

1580+
bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(const CExtKey& master_key)
1581+
{
1582+
LOCK(cs_desc_man);
1583+
assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
1584+
1585+
// Ignore when there is already a descriptor
1586+
if (m_wallet_descriptor.descriptor) {
1587+
return false;
1588+
}
1589+
1590+
int64_t creation_time = GetTime();
1591+
1592+
std::string xpub = EncodeExtPubKey(master_key.Neuter());
1593+
1594+
// Build descriptor string
1595+
std::string desc_prefix;
1596+
std::string desc_suffix = "/*)";
1597+
switch (m_address_type) {
1598+
case OutputType::LEGACY: {
1599+
desc_prefix = "pkh(" + xpub + "/44'";
1600+
break;
1601+
}
1602+
case OutputType::P2SH_SEGWIT: {
1603+
desc_prefix = "sh(wpkh(" + xpub + "/49'";
1604+
desc_suffix += ")";
1605+
break;
1606+
}
1607+
case OutputType::BECH32: {
1608+
desc_prefix = "wpkh(" + xpub + "/84'";
1609+
break;
1610+
}
1611+
default: assert(false);
1612+
}
1613+
1614+
// Mainnet derives at 0', testnet and regtest derive at 1'
1615+
if (Params().IsTestChain()) {
1616+
desc_prefix += "/1'";
1617+
} else {
1618+
desc_prefix += "/0'";
1619+
}
1620+
1621+
std::string internal_path = m_internal ? "/1" : "/0";
1622+
std::string desc_str = desc_prefix + "/0'" + internal_path + desc_suffix;
1623+
1624+
// Make the descriptor
1625+
FlatSigningProvider keys;
1626+
std::string error;
1627+
std::unique_ptr<Descriptor> desc = Parse(desc_str, keys, error, false);
1628+
WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
1629+
m_wallet_descriptor = w_desc;
1630+
1631+
// Store the master private key, and descriptor
1632+
WalletBatch batch(m_storage.GetDatabase());
1633+
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
1634+
throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
1635+
}
1636+
if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
1637+
throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
1638+
}
1639+
1640+
// TopUp
1641+
TopUp();
1642+
1643+
m_storage.UnsetBlankWalletFlag(batch);
1644+
return true;
1645+
}
1646+
15801647
bool DescriptorScriptPubKeyMan::IsHDEnabled() const
15811648
{
15821649
LOCK(cs_desc_man);

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
532532

533533
bool IsHDEnabled() const override;
534534

535+
//! Setup descriptors based on the given CExtkey
536+
bool SetupDescriptorGeneration(const CExtKey& master_key);
537+
535538
bool HavePrivateKeys() const override;
536539

537540
int64_t GetOldestKeyPoolTime() const override;

0 commit comments

Comments
 (0)