Skip to content

wallet: Add importdescriptors interface - #34861

Merged
achow101 merged 12 commits into
bitcoin:masterfrom
polespinasa:2026-03-19-importdescriptors
Sep 17, 2026
Merged

achow101 merged 12 commits into
bitcoin:masterfrom
polespinasa:2026-03-19-importdescriptors

Conversation

@polespinasa

@polespinasa polespinasa commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

This PR adds an interface for importing descriptors.

The motivation behind this is that currently, importing descriptors is only possible via RPC. Bitcoin Core GUI doesn't use the RPC interface so it cannot offer descriptor import functionality, which is needed to support more complex wallet setups such as multisig.

This PR also adds a refactor by moving the importdescriptors logic from the RPC layer into CWallet::ImportDescriptor, making it reusable by both the RPC and this new interface.

The main changes are:

  • Introduces CWallet::ImportDescriptor() containing the core import logic, previously embedded in the RPC ProcessDescriptorImport function.
  • Introduces wallet::ImportDescriptorResult, a new result struct that carries success status, error message, warnings, and a FailureReason enum. The RPC layer uses FailureReason to map results back to the appropriate JSON-RPC error codes, keeping RPC concerns out of CWallet.
  • Updates ProcessDescriptorImport in rpc/backup.cpp to delegate to CWallet::ImportDescriptor.
  • Adds interfaces::Wallet::importDescriptors() as a new interface method, allowing the GUI to import descriptors without going through RPC.

I have a GUI menu here: polespinasa#7 so it can be tested. I will open a PR against the main GUI repo, once this gets merged.

@DrahtBot

DrahtBot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34861.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK achow101, w0xlt, arejula27
Concept ACK sedited, stickies-v, rkrux, davidgumberg
Stale ACK xyzconstant, Sjors

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36236 (wallet, rpc: add verify_balance option to importdescriptors by musaHaruna)
  • #36143 (descriptor: add CreateMultisigDescriptor() by rxbryan)
  • #36133 (wallet: store multipath descriptor by Sjors)
  • #35998 (wallet: Handle or explicitly ignore WalletBatch write failures by achow101)
  • #35989 (wallet: fix crash on importdescriptors with a range ending at 2^31-1 by shuv-amp)
  • #35752 (wallet: make encryption state updates atomic by l0rinc)
  • #31668 (Added rescan option for import descriptors by saikiran57)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • // ExpandPrivate to whether the descriptor can be derived at the first index. -> // Expand the descriptor to check whether it can be derived at the first index. [broken phrasing; the original is not grammatical]

2026-09-14 18:21:43

@polespinasa polespinasa changed the title wallet, refactor, ipc: Add importdescriptors IPC interface wallet, ipc: Add importdescriptors IPC interface Mar 19, 2026
@polespinasa
polespinasa force-pushed the 2026-03-19-importdescriptors branch from a3582a2 to 5e9dab4 Compare March 19, 2026 09:36
@stickies-v

Copy link
Copy Markdown
Contributor

currently, importing descriptors is only possible via RPC. This makes it impossible for the GUI to offer descriptor import functionality,

Why can a GUI not use the RPC interface?

@polespinasa

Copy link
Copy Markdown
Member Author

Why can a GUI not use the RPC interface?

Sorry, a GUI can. I ment Bitcoin Core GUI needs it because it doesn't use RPC.
Will rephrase to make it more clear.

@Sjors

Sjors commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

Concept ACK

We'll eventually want a way to import descriptors in the GUI, and moving this code out of src/wallet/rpc is a sensible refactor in any case.

Let's update the PR description to make it clear this isn't an IPC change. Until #10102
lands it's just an interface change, used internally.

@sedited

sedited commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

Makes sense to move this into the wallet interface, but this does indeed have nothing to do with ipc for now.

@polespinasa polespinasa changed the title wallet, ipc: Add importdescriptors IPC interface wallet, ipc: Add importdescriptors interface Mar 19, 2026
@polespinasa

polespinasa commented Mar 19, 2026 •

Copy link
Copy Markdown
Member Author

Updated the title and description to avoid confusion

@polespinasa polespinasa changed the title wallet, ipc: Add importdescriptors interface wallet: Add importdescriptors interface Mar 19, 2026
@stickies-v

Copy link
Copy Markdown
Contributor

Thanks for the clarification, Concept ACK.

@w0xlt

w0xlt commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Approach ACK

Comment thread src/wallet/interfaces.cpp Resolved
@polespinasa
polespinasa force-pushed the 2026-03-19-importdescriptors branch from 5e9dab4 to b88462d Compare March 22, 2026 22:01

@rkrux rkrux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK b88462d

I have not reviewed the PR fully but only looked at one portion that was conspicuous in the diff.

Comment thread src/wallet/wallet.cpp Resolved
@davidgumberg

Copy link
Copy Markdown
Contributor

Concept ACK, this refactor is a nice win for maintainability, and makes adding importdescriptors to the GUI and/or to an IPC interface or any other interface trivial.

I could be mistaken as I haven't tried yet to make this change myself, but I think that the RPC code should consume the interface you are adding, because it deduplicates code and increases confidence that the interface added in this PR (which in the current branch is untested) works correctly since we reuse all of our functional tests on it.

If that's not feasible, the interface commit should probably be held off until the PR that introduces its consumer in the GUI.

Comment thread src/wallet/interfaces.cpp Resolved
@achow101

Copy link
Copy Markdown
Member

but I think that the RPC code should consume the interface you are adding

That is not how RPCs access the wallet, and making RPCs access the wallet through this interface is a pretty significant change that is massively outside of the scope of this PR. It would also mean that a bunch of things use the interface, while other things access CWallet directly, which is very weird.


I think the layout of the commits could be changed to make this easier to review. In general, making a new function that is mostly a copy of another function that will end up replacing the other function makes review more difficult as it does not allow git to show reviewers where things are moved. It also runs the risk of things that are added to the original function not making it into the copy when the PR is rebased, although this is less likely to be an issue for this PR. Since the difference between the 2 functions is primarily in the data types, I would suggest having a first commit that separates ImportDescriptor from ProcessDescriptorImport so that the type changes can be reviewed in that commit. Then having a second commit that moves ImportDescriptor to its new location so that the move diff is minimal.

Comment thread src/wallet/wallet.cpp Resolved
@polespinasa

Copy link
Copy Markdown
Member Author

Added a helper function to minimize the size of the main functions:
https://github.com/bitcoin/bitcoin/compare/8724afc70ea93e29dabf1957b8f30fe406d212bf..b65792c32486acef25801fac1efdc3b989a8d1db

This function cal also be used by other RPCs who rely on WalletErrorCodes. It avoid having duplicated switch cases around the codebase.

@polespinasa

Copy link
Copy Markdown
Member Author

Force pushed to apply some comments by Sjors and David.

  • The two lambda functions are now extinct.
  • Negative timestamps are now treated as invalid. (small breaking change to the rpc interface)
  • Added some more test coverage for the label change behavior.
  • Cleaned a bit how the response was created taking into consideration request order and errors.

Giving all this a prettier and cleaner RPC interface :)

@Sjors Sjors left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

utACK cd36035

Comment thread src/wallet/imports.h Resolved
Comment thread src/wallet/rpc/backup.cpp Resolved
@polespinasa

Copy link
Copy Markdown
Member Author

Force pushed to address some nits :)

@Sjors

Sjors commented Sep 4, 2026

Copy link
Copy Markdown
Member

re-ACK d1036b8

@achow101

achow101 commented Sep 8, 2026

Copy link
Copy Markdown
Member

ACK d1036b8

@arejula27 arejula27 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK d1036b8

A few things I noticed, none of them blocking:

In 89becfe the range checks were also added to ImportDescriptor(), so all callers get them. Two checks are still only in the RPC: negative timestamps and the "*" label, so importDescriptors() accepts both. Could they also be checked in imports.cpp? The RPC check for the timestamp can stay, so the RPC still returns a top-level error.

nit: the title and message of f50d56b say ProcessDescriptorImport returns ImportDescriptorResult, but that type does not exist in that commit (or anywhere in the PR). The returned type is ImportResult. However, I prefer ImportDescriptorResult.

Comment thread src/wallet/rpc/backup.cpp Resolved
Comment thread doc/release-notes-34861.md Resolved
Comment thread src/wallet/rpc/backup.cpp Resolved
Comment thread src/wallet/imports.h Resolved
Comment thread src/interfaces/wallet.h Resolved
Comment thread src/wallet/imports.cpp Resolved
@sedited

sedited commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Can you rebase and take the nits?

Timestamp 0 is allowed and it does not seem a reason to have a minimum timestamp > 0
other than some carried value from legacy wallet.

Lowering simplifies importdescriptors because, before this change, if a timestamp 0 was set
by the user it would be overwritten by the minimum timestamp, but error messages should still
report timestamp 0.

Negative timestamps are now invalid and throw.
ImportDescriptorRquest will replace the UniValue-based interface of ProcessDescriptorImport(),
which will allow to extract the functionality to other interfaces such as the GUI, etc,
in a subsequent commit.
…ptorImport

Adds a new function ProcessUniValueDescriptor that translates from UniValue to ImportDescriptorRequest.
Refactors ProcessDescriptorImport to work with ImportDescriptorRequest objects instead of UniValue.
Refactors RPCMethod importdescriptors() to use ProcessUniValueDescriptor.

The commit introduces a small behavior change to make label handling more consistent.
Previously, an unset label on a descriptor that cannot have a label would eventually be converted
to an empty string. However, if the user explicitly set the label to an empty string,
the importdescriptors RPC call would fail because the descriptor does not support labels.
This inconsistency does not make sense, since an empty string (or null) effectively means that
no label was set. With this change, null labels and empty strings are treated as valid and are
considered equivalent to having no label at all.
ImportError struct will substitute the UniValue based response of ProcessDescriptorImport
in a subsequent commit. This will allow other interfaces such as the GUI to consume those function.

Added an extra constructors that will be useful in following commits.

Also add new error codes to the WalletError codes enum, this is necessary in order to propagate
error codes to the new interfaces and to translate errors to the rpc wallet error codes.
Helper function to translate from WalletErrorCode to RPC error codes.
…orResult

Make ProcessDescriptorImport return a ImportDescriptorResult object instead of a UniValue.

This commit gets rid of a try-catch that generates a big diff of identation changes,
it is recommended to review it with --ignore-space-change
…ocessDescriptorsImport

Rename ProcessDescriptorImport() to ImportDescriptor().
ImportDescriptor() takes a single descriptor request and imports it into the wallet.

Add ProcessDescriptorsImport() (descriptors in plural) to handle wallet locking and rescanning
over a vector of ImportDescriptorRequest items. It is also in charge of collecting all responses
of imports from ImportDescriptor.

This allows a next commit to extract ImportDescriptor() and
ProcessDescriptorsImport() out of the RPC code so they can be used by
other future interfaces.
….cpp

Extract ImportDescriptor() and ProcessDescriptorsImport() out of backup.cpp and move them into imports.cpp so other future interfaces
can use them without needing to know about RPC code.

The commit can be reviewed with the --color-moved=dimmed-zebra option for an easier review.
@polespinasa

Copy link
Copy Markdown
Member Author

Rebased on top of master and addressed the pending nits :)

Co-authored-by: w0xlt <[email protected]>

ProcessUniValueDescriptor() checks for the descriptor range correctness, but since a
future commit adds an interface to import descriptors, those checks must be extracted
from RPC code. This commit adds a helper function to check ranges in descriptors and
checks for the range inside ImportDescriptor().
ProcessUniValueDescriptor keeps also the check as other RPC calls other than importdescriptors
also needs to check for ranges. This introduces a double check on the RPC path,
but the cost of it is minimal and can be accepted.
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/34879117509/job/104093653722
LLM reason (✨ experimental): CI failed because IWYU reported a missing/incorrect #include (e.g., added util/expected.h in src/rpc/util.cpp) and forced the job to fail.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@achow101

Copy link
Copy Markdown
Member

re-ACK 2c249db

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reACK 2c249db

@arejula27 arejula27 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reACK 2c249db

Comment thread src/script/descriptor.cpp
if ((high >> 31) != 0) {
return util::Unexpected<std::string>("End of range is too high");
}
if (high >= low + 1000000) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

low + 1000000 can overflow before the low > high check below.

For example, a range of [INT64_MAX, 0] reaches this expression and invokes signed-integer overflow, which is undefined behavior. Previously, ParseRange() rejected low > high before performing this arithmetic.

Could the reversed-range check be moved above this one, followed by high - low >= 1000000? The subtraction is safe after establishing 0 <= low <= high.

It would also be useful to add {std::numeric_limits<int64_t>::max(), 0} to reject_invalid_descriptor_ranges.

A possible implementation would be:

if (low > high) {
return util::Unexpectedstd::string(
"Range specified as [begin,end] must not have begin after end");
}
if (high - low >= 1000000) {
return util::Unexpectedstd::string("Range is too large");
}

Wouldn't call this a blocker, just wanted to call it out.

@polespinasa polespinasa Sep 16, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks Rob 🔺

Unless maintainers ask me to fix this I will leave at is, because there is a PR already fixing overflows in descriptors ranges. See #35989.
And also because the user must intentionally trigger this.

If that PR gets closed I am happy to open a follow-up PR fixing all this overflows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.