mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-26 00:41:23 +02:00
wallet, descspkm: Refactor wallet descriptor generation to standalone func
This commit is contained in:
parent
54e74f46ea
commit
73926f2d31
@ -2327,55 +2327,7 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(WalletBatch& batch, co
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t creation_time = GetTime();
|
m_wallet_descriptor = GenerateWalletDescriptor(master_key.Neuter(), addr_type, internal);
|
||||||
|
|
||||||
std::string xpub = EncodeExtPubKey(master_key.Neuter());
|
|
||||||
|
|
||||||
// Build descriptor string
|
|
||||||
std::string desc_prefix;
|
|
||||||
std::string desc_suffix = "/*)";
|
|
||||||
switch (addr_type) {
|
|
||||||
case OutputType::LEGACY: {
|
|
||||||
desc_prefix = "pkh(" + xpub + "/44h";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OutputType::P2SH_SEGWIT: {
|
|
||||||
desc_prefix = "sh(wpkh(" + xpub + "/49h";
|
|
||||||
desc_suffix += ")";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OutputType::BECH32: {
|
|
||||||
desc_prefix = "wpkh(" + xpub + "/84h";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OutputType::BECH32M: {
|
|
||||||
desc_prefix = "tr(" + xpub + "/86h";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OutputType::UNKNOWN: {
|
|
||||||
// We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
|
|
||||||
// so if we get to this point something is wrong
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
} // no default case, so the compiler can warn about missing cases
|
|
||||||
assert(!desc_prefix.empty());
|
|
||||||
|
|
||||||
// Mainnet derives at 0', testnet and regtest derive at 1'
|
|
||||||
if (Params().IsTestChain()) {
|
|
||||||
desc_prefix += "/1h";
|
|
||||||
} else {
|
|
||||||
desc_prefix += "/0h";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string internal_path = internal ? "/1" : "/0";
|
|
||||||
std::string desc_str = desc_prefix + "/0h" + 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
|
// Store the master private key, and descriptor
|
||||||
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
|
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
|
||||||
|
@ -4,7 +4,9 @@
|
|||||||
|
|
||||||
#include <wallet/walletutil.h>
|
#include <wallet/walletutil.h>
|
||||||
|
|
||||||
|
#include <chainparams.h>
|
||||||
#include <common/args.h>
|
#include <common/args.h>
|
||||||
|
#include <key_io.h>
|
||||||
#include <logging.h>
|
#include <logging.h>
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
@ -43,4 +45,58 @@ WalletFeature GetClosestWalletFeature(int version)
|
|||||||
}
|
}
|
||||||
return static_cast<WalletFeature>(0);
|
return static_cast<WalletFeature>(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& addr_type, bool internal)
|
||||||
|
{
|
||||||
|
int64_t creation_time = GetTime();
|
||||||
|
|
||||||
|
std::string xpub = EncodeExtPubKey(master_key);
|
||||||
|
|
||||||
|
// Build descriptor string
|
||||||
|
std::string desc_prefix;
|
||||||
|
std::string desc_suffix = "/*)";
|
||||||
|
switch (addr_type) {
|
||||||
|
case OutputType::LEGACY: {
|
||||||
|
desc_prefix = "pkh(" + xpub + "/44h";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OutputType::P2SH_SEGWIT: {
|
||||||
|
desc_prefix = "sh(wpkh(" + xpub + "/49h";
|
||||||
|
desc_suffix += ")";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OutputType::BECH32: {
|
||||||
|
desc_prefix = "wpkh(" + xpub + "/84h";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OutputType::BECH32M: {
|
||||||
|
desc_prefix = "tr(" + xpub + "/86h";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OutputType::UNKNOWN: {
|
||||||
|
// We should never have a DescriptorScriptPubKeyMan for an UNKNOWN OutputType,
|
||||||
|
// so if we get to this point something is wrong
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
} // no default case, so the compiler can warn about missing cases
|
||||||
|
assert(!desc_prefix.empty());
|
||||||
|
|
||||||
|
// Mainnet derives at 0', testnet and regtest derive at 1'
|
||||||
|
if (Params().IsTestChain()) {
|
||||||
|
desc_prefix += "/1h";
|
||||||
|
} else {
|
||||||
|
desc_prefix += "/0h";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string internal_path = internal ? "/1" : "/0";
|
||||||
|
std::string desc_str = desc_prefix + "/0h" + 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);
|
||||||
|
return w_desc;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace wallet
|
} // namespace wallet
|
||||||
|
@ -114,6 +114,8 @@ public:
|
|||||||
WalletDescriptor() {}
|
WalletDescriptor() {}
|
||||||
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) { }
|
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) { }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& output_type, bool internal);
|
||||||
} // namespace wallet
|
} // namespace wallet
|
||||||
|
|
||||||
#endif // BITCOIN_WALLET_WALLETUTIL_H
|
#endif // BITCOIN_WALLET_WALLETUTIL_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user