Skip to content

Commit 31ffb78

Browse files
committed
feat: add option -usehd to wallettool to let create non-hd wallets
1 parent 456e34c commit 31ffb78

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/bitcoin-wallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
2525
SetupChainParamsBaseOptions(argsman);
2626

2727
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
28+
argsman.AddArg("-usehd", strprintf("Create HD (hierarchical deterministic) wallet (default: %d)", true), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2829
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
2930
argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
3031
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);

src/wallet/wallettool.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,26 @@ static void WalletToolReleaseWallet(CWallet* wallet)
2222
delete wallet;
2323
}
2424

25+
static const bool DEFAULT_USE_HD_WALLET{true};
26+
2527
static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flags)
2628
{
2729
LOCK(wallet_instance->cs_wallet);
28-
wallet_instance->SetMinVersion(FEATURE_LATEST);
30+
if (gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET)) {
31+
wallet_instance->SetMinVersion(FEATURE_LATEST);
32+
} else {
33+
wallet_instance->SetMinVersion(FEATURE_COMPRPUBKEY);
34+
}
2935
wallet_instance->SetWalletFlag(wallet_creation_flags);
3036

3137
if (!wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
3238
// TODO: use here SetupGeneration instead, such as: spk_man->SetupGeneration(false);
3339
// SetupGeneration is not backported yet
3440
wallet_instance->SetupLegacyScriptPubKeyMan();
3541
auto spk_man = wallet_instance->GetOrCreateLegacyScriptPubKeyMan();
36-
spk_man->GenerateNewHDChain(/*secureMnemonic=*/"", /*secureMnemonicPassphrase=*/"");
42+
if (gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET)) {
43+
spk_man->GenerateNewHDChain(/*secureMnemonic=*/"", /*secureMnemonicPassphrase=*/"");
44+
}
3745
} else {
3846
wallet_instance->SetupDescriptorScriptPubKeyMans();
3947
}

test/functional/tool_wallet.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,16 @@ def test_dump_createfromdump(self):
400400
assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest/wallets", "badload"))
401401

402402

403+
def test_nonhd(self):
404+
self.log.info('Check non-hd wallet')
405+
self.start_node(0, ['-usehd=0', '-nowallet'])
406+
self.nodes[0].createwallet("nohd")
407+
assert_equal(False, 'hdchainid' in self.nodes[0].get_wallet_rpc('nohd').getwalletinfo())
408+
self.restart_node(0, ['-usehd=1', '-nowallet'])
409+
self.nodes[0].createwallet("hd")
410+
assert_equal(True, 'hdchainid' in self.nodes[0].get_wallet_rpc('hd').getwalletinfo())
411+
self.stop_node(0)
412+
403413
def run_test(self):
404414
self.wallet_path = os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename)
405415
self.test_invalid_tool_commands_and_args()
@@ -412,6 +422,7 @@ def run_test(self):
412422
# Salvage is a legacy wallet only thing
413423
self.test_salvage()
414424
self.test_wipe()
425+
self.test_nonhd()
415426
self.test_dump_createfromdump()
416427

417428
if __name__ == '__main__':

0 commit comments

Comments
 (0)