wallet: Be able to unlock the wallet for migration

Since migration reloads the wallet, the wallet will always be locked
unless the passphrase is given. migratewallet can now take the
passphrase in order to unlock the wallet for migration.

Github-Pull: #26595
Rebased-From: 7fd125b27d
This commit is contained in:
Andrew Chow
2022-11-28 17:10:44 -05:00
committed by fanquake
parent 50dd8b13df
commit ccc72fecd7
4 changed files with 23 additions and 11 deletions

View File

@@ -713,6 +713,7 @@ static RPCHelpMan migratewallet()
HELP_REQUIRING_PASSPHRASE,
{
{"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."},
{"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The wallet passphrase"},
},
RPCResult{
RPCResult::Type::OBJ, "", "",
@@ -741,15 +742,14 @@ static RPCHelpMan migratewallet()
wallet_name = request.params[0].get_str();
}
WalletContext& context = EnsureWalletContext(request.context);
{
std::shared_ptr<CWallet> wallet = GetWallet(context, wallet_name);
if (wallet && wallet->IsCrypted()) {
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: migratewallet on encrypted wallets is currently unsupported.");
}
SecureString wallet_pass;
wallet_pass.reserve(100);
if (!request.params[1].isNull()) {
wallet_pass = std::string_view{request.params[1].get_str()};
}
util::Result<MigrationResult> res = MigrateLegacyToDescriptor(wallet_name, context);
WalletContext& context = EnsureWalletContext(request.context);
util::Result<MigrationResult> res = MigrateLegacyToDescriptor(wallet_name, wallet_pass, context);
if (!res) {
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(res).original);
}