Skip to content

Commit 73926f2

Browse files
committed
wallet, descspkm: Refactor wallet descriptor generation to standalone func
1 parent 54e74f4 commit 73926f2

File tree

3 files changed

+59
-49
lines changed

3 files changed

+59
-49
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,55 +2327,7 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(WalletBatch& batch, co
23272327
return false;
23282328
}
23292329

2330-
int64_t creation_time = GetTime();
2331-
2332-
std::string xpub = EncodeExtPubKey(master_key.Neuter());
2333-
2334-
// Build descriptor string
2335-
std::string desc_prefix;
2336-
std::string desc_suffix = "/*)";
2337-
switch (addr_type) {
2338-
case OutputType::LEGACY: {
2339-
desc_prefix = "pkh(" + xpub + "/44h";
2340-
break;
2341-
}
2342-
case OutputType::P2SH_SEGWIT: {
2343-
desc_prefix = "sh(wpkh(" + xpub + "/49h";
2344-
desc_suffix += ")";
2345-
break;
2346-
}
2347-
case OutputType::BECH32: {
2348-
desc_prefix = "wpkh(" + xpub + "/84h";
2349-
break;
2350-
}
2351-
case OutputType::BECH32M: {
2352-
desc_prefix = "tr(" + xpub + "/86h";
2353-
break;
2354-
}
2355-
case OutputType::UNKNOWN: {
2356-
// We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
2357-
// so if we get to this point something is wrong
2358-
assert(false);
2359-
}
2360-
} // no default case, so the compiler can warn about missing cases
2361-
assert(!desc_prefix.empty());
2362-
2363-
// Mainnet derives at 0', testnet and regtest derive at 1'
2364-
if (Params().IsTestChain()) {
2365-
desc_prefix += "/1h";
2366-
} else {
2367-
desc_prefix += "/0h";
2368-
}
2369-
2370-
std::string internal_path = internal ? "/1" : "/0";
2371-
std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
2372-
2373-
// Make the descriptor
2374-
FlatSigningProvider keys;
2375-
std::string error;
2376-
std::unique_ptr<Descriptor> desc = Parse(desc_str, keys, error, false);
2377-
WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
2378-
m_wallet_descriptor = w_desc;
2330+
m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal);
23792331

23802332
// Store the master private key, and descriptor
23812333
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {

src/wallet/walletutil.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include <wallet/walletutil.h>
66

7+
#include <chainparams.h>
78
#include <common/args.h>
9+
#include <key_io.h>
810
#include <logging.h>
911

1012
namespace wallet {
@@ -43,4 +45,58 @@ WalletFeature GetClosestWalletFeature(int version)
4345
}
4446
return static_cast<WalletFeature>(0);
4547
}
48+
49+
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
50+
{
51+
int64_t creation_time = GetTime();
52+
53+
std::string xpub = EncodeExtPubKey(master_key);
54+
55+
// Build descriptor string
56+
std::string desc_prefix;
57+
std::string desc_suffix = "/*)";
58+
switch (addr_type) {
59+
case OutputType::LEGACY: {
60+
desc_prefix = "pkh(" + xpub + "/44h";
61+
break;
62+
}
63+
case OutputType::P2SH_SEGWIT: {
64+
desc_prefix = "sh(wpkh(" + xpub + "/49h";
65+
desc_suffix += ")";
66+
break;
67+
}
68+
case OutputType::BECH32: {
69+
desc_prefix = "wpkh(" + xpub + "/84h";
70+
break;
71+
}
72+
case OutputType::BECH32M: {
73+
desc_prefix = "tr(" + xpub + "/86h";
74+
break;
75+
}
76+
case OutputType::UNKNOWN: {
77+
// We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
78+
// so if we get to this point something is wrong
79+
assert(false);
80+
}
81+
} // no default case, so the compiler can warn about missing cases
82+
assert(!desc_prefix.empty());
83+
84+
// Mainnet derives at 0', testnet and regtest derive at 1'
85+
if (Params().IsTestChain()) {
86+
desc_prefix += "/1h";
87+
} else {
88+
desc_prefix += "/0h";
89+
}
90+
91+
std::string internal_path = internal ? "/1" : "/0";
92+
std::string desc_str = desc_prefix + "/0h" + internal_path + desc_suffix;
93+
94+
// Make the descriptor
95+
FlatSigningProvider keys;
96+
std::string error;
97+
std::unique_ptr<Descriptor> desc = Parse(desc_str, keys, error, false);
98+
WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
99+
return w_desc;
100+
}
101+
46102
} // namespace wallet

src/wallet/walletutil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ class WalletDescriptor
114114
WalletDescriptor() {}
115115
WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index) : descriptor(descriptor), id(DescriptorID(*descriptor)), creation_time(creation_time), range_start(range_start), range_end(range_end), next_index(next_index) { }
116116
};
117+
118+
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& output_type, bool internal);
117119
} // namespace wallet
118120

119121
#endif // BITCOIN_WALLET_WALLETUTIL_H

0 commit comments

Comments
 (0)