Skip to content

Commit 6bdbc5f

Browse files
committed
rpc: Allow users to specify wallet name for migratewallet
1 parent dbfa345 commit 6bdbc5f

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/wallet/rpc/wallet.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,9 @@ static RPCHelpMan migratewallet()
722722
"file will be named <wallet name>-<timestamp>.legacy.bak and can be found in the directory\n"
723723
"for this wallet. In the event of an incorrect migration, the backup can be restored using restorewallet." +
724724
HELP_REQUIRING_PASSPHRASE,
725-
{},
725+
{
726+
{"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to migrate. If provided both here and in the RPC endpoint, the two must be identical."},
727+
},
726728
RPCResult{
727729
RPCResult::Type::OBJ, "", "",
728730
{
@@ -739,17 +741,24 @@ static RPCHelpMan migratewallet()
739741
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
740742
{
741743
std::string wallet_name;
742-
{
743-
std::shared_ptr<CWallet> wallet = GetWalletForJSONRPCRequest(request);
744-
if (!wallet) return NullUniValue;
745-
746-
if (wallet->IsCrypted()) {
747-
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: migratewallet on encrypted wallets is currently unsupported.");
744+
if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
745+
if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
746+
throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
748747
}
749-
wallet_name = wallet->GetName();
748+
} else {
749+
if (request.params[0].isNull()) {
750+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Either RPC endpoint wallet or wallet_name parameter must be provided");
751+
}
752+
wallet_name = request.params[0].get_str();
750753
}
751754

752755
WalletContext& context = EnsureWalletContext(request.context);
756+
{
757+
std::shared_ptr<CWallet> wallet = GetWallet(context, wallet_name);
758+
if (wallet && wallet->IsCrypted()) {
759+
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: migratewallet on encrypted wallets is currently unsupported.");
760+
}
761+
}
753762

754763
util::Result<MigrationResult> res = MigrateLegacyToDescriptor(wallet_name, context);
755764
if (!res) {

0 commit comments

Comments
 (0)