Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions doc/managing-wallets.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,24 @@ $ dash-cli -rpcwallet="restored-wallet" getwalletinfo
```

The restored wallet can also be loaded in the GUI via `File` ->`Open wallet`.

## Migrating Legacy Wallets to Descriptor Wallets

Legacy wallets (traditional non-descriptor wallets) can be migrated to become Descriptor wallets
through the use of the `migratewallet` RPC. Migrated wallets will have all of their addresses and private keys added to
a newly created Descriptor wallet that has the same name as the original wallet. Because Descriptor
wallets do not support having private keys and watch-only scripts, there may be up to two
additional wallets created after migration. In addition to a descriptor wallet of the same name,
there may also be a wallet named `<name>_watchonly` and `<name>_solvables`. `<name>_watchonly`
contains all of the watchonly scripts. `<name>_solvables` contains any scripts which the wallet
knows but is not watching the corresponding P2SH scripts.

Given that there is an extremely large number of possible configurations for the scripts that
Legacy wallets can know about, be watching for, and be able to sign for, `migratewallet` only
makes a best effort attempt to capture all of these things into Descriptor wallets. There may be
unforeseen configurations which result in some scripts being excluded. If a migration fails
unexpectedly or otherwise misses any scripts, please create an issue on GitHub. A backup of the
original wallet can be found in the wallet directory with the name `<name>-<timestamp>.legacy.bak`.

The backup can be restored using the `restorewallet` command as discussed in the
[Restoring the Wallet From a Backup](#16-restoring-the-wallet-from-a-backup) section
9 changes: 9 additions & 0 deletions doc/release-notes-19602.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Wallet
======

Migrating Legacy Wallets to Descriptor Wallets
---------------------------------------------

An experimental RPC `migratewallet` has been added to migrate Legacy (non-descriptor) wallets to
Descriptor wallets. More information about the migration process is available in the
[documentation](https://github.com/dashpay/dash/blob/master/doc/managing-wallets.md#migrating-legacy-wallets-to-descriptor-wallets).
4 changes: 3 additions & 1 deletion src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ class DescriptorImpl : public Descriptor
return AddChecksum(ret);
}

bool ToPrivateString(const SigningProvider& arg, std::string& out) const override final
bool ToPrivateString(const SigningProvider& arg, std::string& out) const override
{
bool ret = ToStringHelper(&arg, out, StringType::PRIVATE);
out = AddChecksum(out);
Expand Down Expand Up @@ -677,6 +677,7 @@ class AddressDescriptor final : public DescriptorImpl
}
}
bool IsSingleType() const final { return true; }
bool ToPrivateString(const SigningProvider& arg, std::string& out) const final { return false; }
};

/** A parsed raw(H) descriptor. */
Expand All @@ -702,6 +703,7 @@ class RawDescriptor final : public DescriptorImpl
}
}
bool IsSingleType() const final { return true; }
bool ToPrivateString(const SigningProvider& arg, std::string& out) const final { return false; }
};

/** A parsed pk(P) descriptor. */
Expand Down
69 changes: 69 additions & 0 deletions src/wallet/rpc/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,74 @@ RPCHelpMan simulaterawtransaction()
};
}

static RPCHelpMan migratewallet()
{
return RPCHelpMan{"migratewallet",
"EXPERIMENTAL warning: This call may not work as expected and may be changed in future releases\n"
"\nMigrate the wallet to a descriptor wallet.\n"
"A new wallet backup will need to be made.\n"
"\nThe migration process will create a backup of the wallet before migrating. This backup\n"
"file will be named <wallet name>-<timestamp>.legacy.bak and can be found in the directory\n"
"for this wallet. In the event of an incorrect migration, the backup can be restored using restorewallet."
"\nEncrypted wallets must have the passphrase provided as an argument to this call.",
{
{"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, "", "",
{
{RPCResult::Type::STR, "wallet_name", "The name of the primary migrated wallet"},
{RPCResult::Type::STR, "watchonly_name", /*optional=*/true, "The name of the migrated wallet containing the watchonly scripts"},
{RPCResult::Type::STR, "solvables_name", /*optional=*/true, "The name of the migrated wallet containing solvable but not watched scripts"},
{RPCResult::Type::STR, "backup_path", "The location of the backup of the original wallet"},
}
},
RPCExamples{
HelpExampleCli("migratewallet", "")
+ HelpExampleRpc("migratewallet", "")
Comment on lines +1129 to +1131
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix the RPC examples; they currently show a call that errors.

These examples omit both the wallet endpoint and wallet_name, but the handler rejects that combination on Lines 1141-1142. help migratewallet should show a form that actually succeeds, e.g. by passing wallet_name explicitly.

💡 Suggested fix
         RPCExamples{
-            HelpExampleCli("migratewallet", "")
-            + HelpExampleRpc("migratewallet", "")
+            HelpExampleCli("migratewallet", "\"wallet_name\"")
+            + HelpExampleRpc("migratewallet", "\"wallet_name\"")
         },
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/wallet/rpc/wallet.cpp` around lines 1129 - 1131, The RPC help examples
currently show calling migratewallet with no wallet specified, which the
migratewallet handler rejects; update the RPCExamples block (the HelpExampleCli
and HelpExampleRpc calls near the migratewallet RPC definition) to pass an
explicit wallet name (and/or use the wallet endpoint) so the example actually
succeeds — e.g. change HelpExampleCli("migratewallet", "") and
HelpExampleRpc("migratewallet", "") to include a wallet parameter like
HelpExampleCli("migratewallet", "\"mywallet\"") and
HelpExampleRpc("migratewallet", "\"mywallet\"") or alternatively show using
-rpcwallet=mywallet; ensure you edit the RPCExamples surrounding the
migratewallet RPC to reflect this.

},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::string wallet_name;
if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
}
} else {
if (request.params[0].isNull()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Either RPC endpoint wallet or wallet_name parameter must be provided");
}
wallet_name = request.params[0].get_str();
}

SecureString wallet_pass;
wallet_pass.reserve(100);
if (!request.params[1].isNull()) {
wallet_pass = std::string_view{request.params[1].get_str()};
}

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);
}

UniValue r{UniValue::VOBJ};
r.pushKV("wallet_name", res->wallet_name);
if (res->watchonly_wallet) {
r.pushKV("watchonly_name", res->watchonly_wallet->GetName());
}
if (res->solvables_wallet) {
r.pushKV("solvables_name", res->solvables_wallet->GetName());
}
r.pushKV("backup_path", fs::PathToString(res->backup_path));

return r;
},
};
}

// addresses
RPCHelpMan getaddressinfo();
RPCHelpMan getnewaddress();
Expand Down Expand Up @@ -1223,6 +1291,7 @@ Span<const CRPCCommand> GetWalletRPCCommands()
{"wallet", &listwallets},
{"wallet", &loadwallet},
{"wallet", &lockunspent},
{"wallet", &migratewallet},
{"wallet", &newkeypool},
{"wallet", &removeprunedfunds},
{"wallet", &rescanblockchain},
Expand Down
Loading
Loading