Skip to content

Commit e00f1ca

Browse files
Merge dashpay#5991: fix: various fixes for HD wallets implementation
e5129e6 fix: if hdseed is wrong - do not setup random seed, user can lost his fund (Konstantin Akimov) e52498b chore: add todoes to UpgradeToHD function (Konstantin Akimov) 0d12ea9 fix: wrong if/else branches for key refill in wallet creation (Konstantin Akimov) d2c3dcb refactor: move list of function in rpcwallet to the end (Konstantin Akimov) Pull request description: ## Issue being fixed or feature implemented Reviewed our implementation of HD wallets and compare it to bitcoin's for aiming to backport or re-implement `sethdseed` rpc. Noticed some strange things in our implementation, which this PR is aim to fix. ## What was done? See each commit for detailed changes. ## How Has This Been Tested? Run unit/functional tests ## Breaking Changes `-hdseed` doesn't assign a random seed anymore if an user provided an invalid hex string. ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone ACKs for top commit: PastaPastaPasta: utACK e5129e6 Tree-SHA512: b404dbc0762777abf421a847f58cd243e0aa00151ba3f036835c9ff54c1109b6159921ec24e29455e975797f49d54832c55f7876188da90f37dd1e4a811a21e0
2 parents 27b2e5c + e5129e6 commit e00f1ca

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4272,21 +4272,6 @@ static RPCHelpMan send()
42724272
};
42734273
}
42744274

4275-
RPCHelpMan abortrescan();
4276-
RPCHelpMan dumpprivkey();
4277-
RPCHelpMan importprivkey();
4278-
RPCHelpMan importaddress();
4279-
RPCHelpMan importpubkey();
4280-
RPCHelpMan dumpwallet();
4281-
RPCHelpMan importwallet();
4282-
RPCHelpMan importprunedfunds();
4283-
RPCHelpMan removeprunedfunds();
4284-
RPCHelpMan importmulti();
4285-
RPCHelpMan importdescriptors();
4286-
RPCHelpMan listdescriptors();
4287-
RPCHelpMan dumphdinfo();
4288-
RPCHelpMan importelectrumwallet();
4289-
42904275
RPCHelpMan walletprocesspsbt()
42914276
{
42924277
return RPCHelpMan{"walletprocesspsbt",
@@ -4520,6 +4505,21 @@ static RPCHelpMan upgradewallet()
45204505
};
45214506
}
45224507

4508+
RPCHelpMan abortrescan();
4509+
RPCHelpMan dumpprivkey();
4510+
RPCHelpMan importprivkey();
4511+
RPCHelpMan importaddress();
4512+
RPCHelpMan importpubkey();
4513+
RPCHelpMan dumpwallet();
4514+
RPCHelpMan importwallet();
4515+
RPCHelpMan importprunedfunds();
4516+
RPCHelpMan removeprunedfunds();
4517+
RPCHelpMan importmulti();
4518+
RPCHelpMan importdescriptors();
4519+
RPCHelpMan listdescriptors();
4520+
RPCHelpMan dumphdinfo();
4521+
RPCHelpMan importelectrumwallet();
4522+
45234523
Span<const CRPCCommand> GetWalletRPCCommands()
45244524
{
45254525
// clang-format off

src/wallet/wallet.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4609,7 +4609,8 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, interfaces::C
46094609
newHdChain.AddAccount();
46104610
} else {
46114611
if (gArgs.IsArgSet("-hdseed") && !IsHex(strSeed)) {
4612-
walletInstance->WalletLogPrintf("%s -- Incorrect seed, generating a random mnemonic instead\n", __func__);
4612+
error = strprintf(_("%s -- Incorrect seed, it should be a hex string"), __func__);
4613+
return nullptr;
46134614
}
46144615
SecureString secureMnemonic = gArgs.GetArg("-mnemonic", "").c_str();
46154616
SecureString secureMnemonicPassphrase = gArgs.GetArg("-mnemonicpassphrase", "").c_str();
@@ -4623,18 +4624,13 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, interfaces::C
46234624
gArgs.ForceRemoveArg("hdseed");
46244625
gArgs.ForceRemoveArg("mnemonic");
46254626
gArgs.ForceRemoveArg("mnemonicpassphrase");
4626-
}
4627+
} // Otherwise, do not create a new HD chain
4628+
46274629
LOCK(walletInstance->cs_wallet);
46284630
if (walletInstance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
46294631
walletInstance->SetupDescriptorScriptPubKeyMans();
46304632
// SetupDescriptorScriptPubKeyMans already calls SetupGeneration for us so we don't need to call SetupGeneration separately
4631-
}
4632-
} // Otherwise, do not create a new HD chain
4633-
4634-
// Top up the keypool
4635-
{
4636-
LOCK(walletInstance->cs_wallet);
4637-
if (!walletInstance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
4633+
} else { // Top up the keypool
46384634
// Legacy wallets need SetupGeneration here.
46394635
if (auto spk_man = walletInstance->GetLegacyScriptPubKeyMan()) {
46404636
if (spk_man->CanGenerateKeys() && !spk_man->TopUp()) {
@@ -4935,8 +4931,10 @@ bool CWallet::UpgradeToHD(const SecureString& secureMnemonic, const SecureString
49354931
WalletLogPrintf("Upgrading wallet to HD\n");
49364932
SetMinVersion(FEATURE_HD);
49374933

4934+
// TODO: replace to GetLegacyScriptPubKeyMan() when `sethdseed` is backported
49384935
auto spk_man = GetOrCreateLegacyScriptPubKeyMan();
49394936
bool prev_encrypted = IsCrypted();
4937+
// TODO: unify encrypted and plain chains usages here
49404938
if (prev_encrypted) {
49414939
if (!GenerateNewHDChainEncrypted(secureMnemonic, secureMnemonicPassphrase, secureWalletPassphrase)) {
49424940
error = Untranslated("Failed to generate encrypted HD wallet");

0 commit comments

Comments
 (0)