feat: add option -usehd to wallettool to let create non-hd wallets

This commit is contained in:
Konstantin Akimov 2024-04-09 23:10:58 +07:00
parent 456e34c991
commit 31ffb78ced
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
3 changed files with 22 additions and 2 deletions

View File

@ -25,6 +25,7 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
SetupChainParamsBaseOptions(argsman); SetupChainParamsBaseOptions(argsman);
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-usehd", strprintf("Create HD (hierarchical deterministic) wallet (default: %d)", true), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file. When used with 'createfromdump', loads the records into a new wallet.", ArgsManager::ALLOW_STRING, OptionsCategory::OPTIONS); argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file. When used with 'createfromdump', loads the records into a new wallet.", ArgsManager::ALLOW_STRING, OptionsCategory::OPTIONS);

View File

@ -22,10 +22,16 @@ static void WalletToolReleaseWallet(CWallet* wallet)
delete wallet; delete wallet;
} }
static const bool DEFAULT_USE_HD_WALLET{true};
static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flags) static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flags)
{ {
LOCK(wallet_instance->cs_wallet); LOCK(wallet_instance->cs_wallet);
if (gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET)) {
wallet_instance->SetMinVersion(FEATURE_LATEST); wallet_instance->SetMinVersion(FEATURE_LATEST);
} else {
wallet_instance->SetMinVersion(FEATURE_COMPRPUBKEY);
}
wallet_instance->SetWalletFlag(wallet_creation_flags); wallet_instance->SetWalletFlag(wallet_creation_flags);
if (!wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) { if (!wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
@ -33,7 +39,9 @@ static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flag
// SetupGeneration is not backported yet // SetupGeneration is not backported yet
wallet_instance->SetupLegacyScriptPubKeyMan(); wallet_instance->SetupLegacyScriptPubKeyMan();
auto spk_man = wallet_instance->GetOrCreateLegacyScriptPubKeyMan(); auto spk_man = wallet_instance->GetOrCreateLegacyScriptPubKeyMan();
if (gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET)) {
spk_man->GenerateNewHDChain(/*secureMnemonic=*/"", /*secureMnemonicPassphrase=*/""); spk_man->GenerateNewHDChain(/*secureMnemonic=*/"", /*secureMnemonicPassphrase=*/"");
}
} else { } else {
wallet_instance->SetupDescriptorScriptPubKeyMans(); wallet_instance->SetupDescriptorScriptPubKeyMans();
} }

View File

@ -400,6 +400,16 @@ class ToolWalletTest(BitcoinTestFramework):
assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload")) assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
def test_nonhd(self):
self.log.info('Check non-hd wallet')
self.start_node(0, ['-usehd=0', '-nowallet'])
self.nodes[0].createwallet("nohd")
assert_equal(False, 'hdchainid' in self.nodes[0].get_wallet_rpc('nohd').getwalletinfo())
self.restart_node(0, ['-usehd=1', '-nowallet'])
self.nodes[0].createwallet("hd")
assert_equal(True, 'hdchainid' in self.nodes[0].get_wallet_rpc('hd').getwalletinfo())
self.stop_node(0)
def run_test(self): def run_test(self):
self.wallet_path = os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename) self.wallet_path = os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename)
self.test_invalid_tool_commands_and_args() self.test_invalid_tool_commands_and_args()
@ -412,6 +422,7 @@ class ToolWalletTest(BitcoinTestFramework):
# Salvage is a legacy wallet only thing # Salvage is a legacy wallet only thing
self.test_salvage() self.test_salvage()
self.test_wipe() self.test_wipe()
self.test_nonhd()
self.test_dump_createfromdump() self.test_dump_createfromdump()
if __name__ == '__main__': if __name__ == '__main__':