The wallet currently does not know the master key that was used to generate the automatically generated descriptors. This makes it difficult to add new automatically generated descriptors when new ones are introduced. So instead of losing this information after the descriptors are created, have CWallet
store it. The xpub will be stored in a new activehdkey
field. The private key must be one of the keys that is used by the descriptors, and will be extracted upon loading.
As this is a new field, wallets will be automatically upgraded upon loading. This loading is backwards compatible and uses a new non-required flag WALLET_FLAG_GLOBAL_HD_KEY
to signal that the upgrade completed. The upgrade will search for an xpub that is shared by pkh()
, wpkh()
, and sh(wpkh()
descriptors with the derivation pattern that we use. For new wallets, the xpub will be set during descriptor creation rather than trying to reverse engineer it. The flag will be set for all wallets, regardless of whether such an xpub was found or can even exist, in order to indicate that the upgrade will not need to be run in the future.
This allows us to have a gethdkey
command which is useful for those who need a simple way to get an xpub from a wallet. gethdkey
can also take a boolean parameter to indicate whether it should also output the corresponding xprv.
Supercedes #23417