Skip to content

wallet: Allow importing of descriptors without private keys when the wallet has the private keys - #35377

Open
achow101 wants to merge 4 commits into
bitcoin:masterfrom
achow101:importdescriptors-without-priv
Open

achow101 wants to merge 4 commits into
bitcoin:masterfrom
achow101:importdescriptors-without-priv

Conversation

@achow101

@achow101 achow101 commented May 25, 2026 •

Copy link
Copy Markdown
Member

Currently importing a descriptor to a wallet that has private keys requires the descriptor to include the private keys. This is not ideal as it means exposing private key material. This PR makes it so that the wallet will lookup and substitute the private keys for their respective public keys in such descriptors, thus enabling importing of public descriptors into wallets with private keys.

The underlying mechanism is that the wallet retrieves all of the pubkeys from the descriptor and checks to see if any of them have private keys in any ScriptPubKeyMan. Additionally, if the descriptor has a xpub with key origin info, we will check if any xprvs known to the wallet have a matching fingerprint and derive to the specified xpub. If so, the origin + xpub are replaced with the single xprv with the origin derivation path prepended to the key expression's derivation path.

Possible future work is to allow the xpub substitution to work when the wallet has some child in the key origin, i.e. the wallet xprv has a key origin that is a prefix of the key origin specified for an xpub in the descriptor. Currently this kind of substitution is not being done, only root master xprvs will be substituted.

If a wallet does not have the private keys for a public descriptor, the import is still disallowed.

This PR is based on #34861 to avoid an annoying rebase.

Closes #27336

@DrahtBot

DrahtBot commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Type Reviewers
Concept ACK rkrux, polespinasa, 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:

  • #36885 (wallet, rpc: Add analyzedescriptor for multisig setups by pablomartin4btc)
  • #36257 (qa: assert_equal -> assert_true/assert_false by hodlinator)
  • #36070 (wallet: Add deriveHDKey interface by PraneethGunas)
  • #34520 (refactor: Add [[nodiscard]] to functions returning bool+mutable ref by maflcko)

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:

  • // Whether ' or h is used in harded derivation -> // Whether ' or h is used in hardened derivation [“harded” is misspelled]
  • self.log.info("Test import of descriptors without private keys that the wallet has the privkeys for") -> self.log.info("Test import of descriptors without private keys that the wallet has the private keys for") [“privkeys” is unclear/abbreviated in a way that may hinder comprehension]
  • # xpub substition -> # xpub substitution [“substition” is misspelled]

2026-09-30 18:06:49

@rkrux

rkrux commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Definite Concept ACK 2e6d8d0 because it allows the users to not deal with private keys manually.

@polespinasa

Copy link
Copy Markdown
Member

seems like a good idea, concept ACK

Will review after #34861 is merged

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/27453755862/job/81154189319
LLM reason (✨ experimental): CI failed because IWYU reported a header include issue (it modified src/script/descriptor.h and deliberately exited with “Failure generated from IWYU”).

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
achow101 marked this pull request as ready for review September 22, 2026 20:33
rxbryan added a commit to rxbryan/bitcoin that referenced this pull request Sep 24, 2026
During the import process, the participant swaps their own key
expression in the descriptor for the private one since
importdescriptors reject a descriptor with no private keys when
the wallet has private keys enabled. See bitcoin#35377
rxbryan added a commit to rxbryan/bitcoin that referenced this pull request Sep 24, 2026
During the import process, the participant swaps their own key
expression in the descriptor for the private one since
importdescriptors reject a descriptor with no private keys when
the wallet has private keys enabled. See bitcoin#35377
rxbryan added a commit to rxbryan/bitcoin that referenced this pull request Sep 25, 2026
During the import process, the participant swaps their own key
expression in the descriptor for the private one since
importdescriptors reject a descriptor with no private keys when
the wallet has private keys enabled. See bitcoin#35377
Sjors added a commit to Sjors/bitcoin that referenced this pull request Sep 28, 2026
…criptors

# Conflicts:
#	src/wallet/imports.cpp

@polespinasa polespinasa 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.

did a first review, looks pretty good, I have a concept question/suggestion, left a comment below :)

Comment thread src/script/descriptor.cpp Resolved
Comment thread src/script/descriptor.cpp
break;
}
}
if (derived.Neuter() != xpub_prov->m_root_extkey) continue;

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.

in e59a2f2 descriptor: Implement SubstituteMasterExtPubs

Idk if what I am going to say makes sense but, shouldn't we only compare the pubkey? What if some metadata such as depth, fingerprint, etc. is missing (or is 0)? They key still a valid key but we would silently omit it.
Probably this with Core descriptors will not happen, but should we assume other wallets might miss this data?

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.

I think we want to be as specific as possible here to avoid any unexpected surprises. Given that changing any of the other fields in an xpub can drastically change how the xpub is encoded, I think it would be surprising to users if something very visually different to their private key magically gets a private key.

@achow101
achow101 force-pushed the importdescriptors-without-priv branch from 9f13f80 to b58383a Compare September 30, 2026 18:06
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/36756207544/job/110026972644
LLM reason (✨ experimental): CI failed because the IWYU lint step (Fixing #includes) detected missing/incorrect includes and returned a failure (“Failure generated from IWYU”).

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.

BenWestgate pushed a commit to BenWestgate/bitcoin that referenced this pull request Oct 3, 2026
The multisig tutorial runs every participant wallet on one online node,
and the offline signing tutorial covers a single key. People who want
both, an M-of-N multisig whose keys never touch a networked machine,
have had to piece the procedure together from the two, or follow
third-party guides that do.

Add an "Offline Multisig" section to the offline signing tutorial that
walks through the whole procedure with Bitcoin Core alone:
- create the key wallets on the offline host (`addhdkey`), derive BIP 87
  xpubs with `derivehdkey`, and build one `wsh(sortedmulti())`
  multipath descriptor,
- import it into every key wallet so each can sign, and into a
  watch-only wallet for the online host,
- back up each key with the watch-only wallet and the descriptor, and
  remove the keys from the offline host,
- receive and spend, with the address and PSBT checks from the
  single-key part,
- check every backup with one test deposit, by signing with each key
  and running `testmempoolaccept` on groups of M signatures,
- recover from a lost online host or a lost backup, and handle key
  wallets that were asked to sign past their imported range.

Two details are easy to get wrong offline, so they are called out:
`"timestamp": "now"` means the best block's time, which is the genesis
block on the offline node, and a pruned online node cannot restore the
watch-only wallet once it has pruned blocks from after the wallet's
timestamp.

Importing an xpub-only descriptor into a key wallet that holds one of
its keys depends on bitcoin#35377; on master without it the
import fails with "Cannot import descriptor without private keys to a
wallet with private keys enabled".

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_019R9ppFkUwc5mTfpuBADAkX
@DrahtBot

DrahtBot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Could turn into draft while CI is red?

Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 20, 2026
@fametrano

fametrano commented Oct 22, 2026 •

Copy link
Copy Markdown

Tested b58383a: with the master key in the wallet, re-importing a descriptor the wallet already holds adds a duplicate.

I computed 20 vectors with a separate BIP32 implementation and imported them on regtest. They cover hardened, unhardened and mixed origins, the master as origin, a wrong fingerprint, the right fingerprint with a wrong path, <0;1>, hardened steps after the xpub, tr() script paths and wsh(multi) with a foreign key. 19 succeed or fail as expected, and their substituted descriptors derive the addresses I computed. The 20th led to the case below.

A wallet imports wpkh([fpM/84h/1h/0h]xprvA/0/*), then pkh(xprvM/0/*). Importing that wpkh again, public or private, stores wpkh(xprvM/84h/1h/0h/0/*). Its descriptor ID is new, so listdescriptors lists the same public descriptor twice. With the master imported first, the first private wpkh import is already stored that way. Either way the private export carries the master key where the user gave an account key. Matching the wallet's exact xpub before the master would avoid the duplicate. Keeping the origin with the derived xprv would also keep the descriptor as given. The IWYU failure is this PR's: the new KeyFingerprint comparison needs #include <array> in descriptor.cpp. The vectors and script are available.

Comment thread src/script/descriptor.cpp Resolved
@achow101

achow101 commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

A wallet imports wpkh([fpM/84h/1h/0h]xprvA/0/), then pkh(xprvM/0/). Importing that wpkh again, public or private, stores wpkh(xprvM/84h/1h/0h/0/*)

While we do our best to avoid importing duplicates, importing a duplicate is not world ending and this sort of duplication issue is one that we are not interested in solving. There are a variety of ways that a single descriptor can be textually represented and we cannot cover all cases where there may be duplication.

@achow101
achow101 force-pushed the importdescriptors-without-priv branch from b58383a to 7400986 Compare November 5, 2026 00:33
SubstituteMasterExtPubs replaces Origin + BIP32 inside of a descriptor
when a master xprv is provided that matches the origin and derives the
BIP32 xpub.
When an imported descriptor contains pubkeys for which the wallet knows
the private keys, substitute those pubkeys for the privkeys so that the
descriptor can be imported. This allows such descriptors without private keys
to be imported into wallets with private keys enabled.
Test that importdescriptors can import descriptors that don't have
private keys, but the wallet already has the private key for them.
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.

Should be able to import an xpub descriptor to a privkey-enabled wallet if the wallet has the privkeys

6 participants