Let's say I want to import an extended public key from an electrum wallet. I get the following xpub in p2wpkh format. However, to import this into bitcoin core, recreating the same set of addresses, I need to convert this extended public key to legacy format, that is, from
zpub to xpub. Here's the script to solve this:
Open the electrum console. If the console tab is not visible, go to the View tab and check the "Console" option.
In the console, run the following command (replace the example zpub with yours):
convert_xkey(xkey="zpub6nbRrTgWWqsdSf3m...Vqv56GXGzNFvwRwupveNwVzEvrcPff",xtype="standard")
The result will be your same extended public key, but in legacy format compatible with Bitcoin Core:
"xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS"
Now you need to get the XPUB checksum for both the receiving and change addresses so that you can assemble the descriptors using the getdescriptorinfo command. Let's start by getting the XPUB checksum for the receiving addresses, by typing the following command (replace xpub with yours):
getdescriptorinfo "wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/0/*)"
The descriptor will be located at:
{
"descriptor": "wpkh(xpub.../0/*)#0f874rex",
...
}Note the generated checksum: #0f874rex (Obviously it will be different in yours).
Now for the change descriptor:
getdescriptorinfo "wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/1/*)"
Note down the checksum of the descriptor we will use for change.
Now that you have obtained the checksum of both you can assemble the descriptors, but first,
create a new wallet in bitcoin core, check the option
"disable private keys" so that it is possible to import wallets for watch_only in Bitcoin Core (Bitcoin core has differentiation and separates the files for wallets with private keys and for watch_only wallets with only public keys). The wallet in descriptor format is created by default in Bitcoin Core if you are using the updated version.
Assembling the descriptorsCreate the descriptor using the following command (replace the example xpubs and #checksum with yours), you can import one descriptor of each or import everything at once, I will leave both paths for you to choose:
1. Import descriptors individually
Receiving addressesimportdescriptors '[{"desc":"wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/0/*)#0f874rex","timestamp":"now","active":true, "watch_only":true}]' Change addressesimportdescriptors '[{"desc":"wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/1/*)#7azlgkf7","timestamp":"now","active":true,"internal":true,"watch_only":true}]' 2. Import all descriptors at once (Receiving and Change)To simplify, you can import both descriptors at the same time:
importdescriptors '[{"desc":"wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/0/*)#7azlgkf7","timestamp":"now","active":true, "watch_only":true},{"desc":"wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/1/*)#7azlgkf7","timestamp":"now","active":true,"internal":true,"watch_only":true}]'When you import both, you will get the following return:
{
"success": true,
"warnings": [
"Range not given, using default keypool range"
]
}
Ignore the warning, as it is saying that you have not defined an address range limit, however this is optional, by default it will import with 1000 addresses from each descriptor if you do not define a limit, but if you want, you can use the "range" parameter and manually define the amount of addresses as I show below.
3. Adjust the address range (optional)To set a range limit, add the
"range" parameter in the descriptor with a range limit, e.g. starting from index 0 (the first address) to 999 (to add up to 1000 which is the default used in Bitcoin Core) example:
"range":[0,999]
So the final result of the descriptors with the range would be like this:
importdescriptors '[{"desc":"wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/0/*)#0f874rex","timestamp":"now","active":true, "watch_only":true,"range":[0,999]},{"desc":"wpkh(xpub68vuF8LgDUnfk4...STPZBqQgcdUsY3vcndhSVgX14CG36k1HfqVNhTn6hoxS/1/*)#7azlgkf7","timestamp":"now","active":true,"internal":true,"watch_only":true,"range":[0,999]}]'