| 450 | } |
| 451 | |
| 452 | static RPCHelpMan sethdseed() |
| 453 | { |
| 454 | return RPCHelpMan{"sethdseed", |
| 455 | "\nSet or generate a new HD wallet seed. Non-HD wallets will not be upgraded to being a HD wallet. Wallets that are already\n" |
| 456 | "HD will have a new HD seed set so that new keys added to the keypool will be derived from this new seed.\n" |
| 457 | "\nNote that you will need to MAKE A NEW BACKUP of your wallet after setting the HD wallet seed." + |
| 458 | HELP_REQUIRING_PASSPHRASE, |
| 459 | { |
| 460 | {"newkeypool", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether to flush old unused addresses, including change addresses, from the keypool and regenerate it.\n" |
| 461 | "If true, the next address from getnewaddress and change address from getrawchangeaddress will be from this new seed.\n" |
| 462 | "If false, addresses (including change addresses if the wallet already had HD Chain Split enabled) from the existing\n" |
| 463 | "keypool will be used until it has been depleted."}, |
| 464 | {"seed", RPCArg::Type::STR, RPCArg::DefaultHint{"random seed"}, "The WIF private key to use as the new HD seed.\n" |
| 465 | "The seed value can be retrieved using the dumpwallet command. It is the private key marked hdseed=1"}, |
| 466 | }, |
| 467 | RPCResult{RPCResult::Type::NONE, "", ""}, |
| 468 | RPCExamples{ |
| 469 | HelpExampleCli("sethdseed", "") |
| 470 | + HelpExampleCli("sethdseed", "false") |
| 471 | + HelpExampleCli("sethdseed", "true \"wifkey\"") |
| 472 | + HelpExampleRpc("sethdseed", "true, \"wifkey\"") |
| 473 | }, |
| 474 | [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 475 | { |
| 476 | std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request); |
| 477 | if (!pwallet) return NullUniValue; |
| 478 | |
| 479 | LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet, true); |
| 480 | |
| 481 | if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { |
| 482 | throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set a HD seed to a wallet with private keys disabled"); |
| 483 | } |
| 484 | |
| 485 | LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore); |
| 486 | |
| 487 | // Do not do anything to non-HD wallets |
| 488 | if (!pwallet->CanSupportFeature(FEATURE_HD)) { |
| 489 | throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set an HD seed on a non-HD wallet. Use the upgradewallet RPC in order to upgrade a non-HD wallet to HD"); |
| 490 | } |
| 491 | |
| 492 | EnsureWalletIsUnlocked(*pwallet); |
| 493 | |
| 494 | bool flush_key_pool = true; |
| 495 | if (!request.params[0].isNull()) { |
| 496 | flush_key_pool = request.params[0].get_bool(); |
| 497 | } |
| 498 | |
| 499 | CPubKey master_pub_key; |
| 500 | if (request.params[1].isNull()) { |
| 501 | master_pub_key = spk_man.GenerateNewSeed(); |
| 502 | } else { |
| 503 | CKey key = DecodeSecret(request.params[1].get_str()); |
| 504 | if (!key.IsValid()) { |
| 505 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key"); |
| 506 | } |
| 507 | |
| 508 | if (HaveKey(spk_man, key)) { |
| 509 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Already have this key (either as an HD seed or as a loose private key)"); |
nothing calls this directly
no test coverage detected