BitcoinTechnicalSecurityIntermediate

A Key That Only Watches

Paste an xpub (extended public key) into a wallet and you get a watch-only wallet that shows balance and history without any private key. What an xpub contains, the difference between xpub, ypub and zpub, how to set up a watch-only wallet in Sparrow, Electrum and Bitcoin Core, and the two reasons to keep an xpub private even though it is a public key, checked against the BIP-32 text and test vectors.

·12min·Updated

The hardware wallet stays in a drawer, and the balance is checked on a phone or laptop. New receiving addresses are made there too. No private key has ever touched that device. What makes this possible is a single string, the extended public key, usually called an xpub. A wallet that runs on public information alone like this is called a watch-only wallet.

The usual explanation of an xpub goes like this: it is a public key, so nobody can steal coins with it, and the only thing to watch is privacy. That is half right. The BIP-32 text itself says extended public keys must be treated more carefully than regular public keys.

What an xpub contains

An HD wallet grows keys from one seed like branches of a tree. Every node of the tree has an extended key, which is a key bundled with a 32-byte value called the chain code. The chain code is what lets you compute the child keys below that node.

BIP-32 serializes an extended key into 78 bytes.

LengthContent
4 bytesVersion (0x0488B21E for a mainnet public key)
1 byteDepth (0 for the master key)
4 bytesFingerprint of the parent key
4 bytesChild number
32 bytesChain code
33 bytesPublic key

Add a 4-byte checksum and encode it in Base58 and you get exactly 111 characters, which start with xpub because of the version bytes. Two things are missing from the table. Of the path from the master down to this key, only the last step (the child number) is recorded, and the address format to build from this key is not recorded at all. Those gaps are the root of the confusion described below.

The function that derives a child public key from an extended public key is defined only for normal children. BIP-32 sets child numbers of 2^31 and above apart as hardened children, and says that asking for a hardened child from an extended public key must return failure. So an xpub produces every normal child public key below it, with no limit on how many. That is why a watch-only wallet can keep handing out fresh receiving addresses.

The xpub a wallet exports is one account

A standard native SegWit path looks like m/84'/0'/0'/0/5. The first three steps (purpose, coin, account) are hardened, and the last two are normal. The key a wallet shows under "export xpub" is usually the last hardened node, the account key m/84'/0'/0', at depth 3. Hardened derivation needs the private key, so the export cuts at the point below which public keys alone are enough.

Below the account key are two branches. 0 is for receiving addresses you give to others, and 1 is for change addresses where the remainder of your own transactions comes back. An account xpub produces both. Whoever holds it sees the money coming in and also, through the change addresses, which transactions were yours going out.

The use cases section of BIP-32 was aware of this difference. If an auditor needs every incoming and outgoing payment, you hand over all the account extended public keys. If an insecure web server takes payments, the server gets not the account key but only the key of the receiving branch, M/i'/0. Someone who breaks into that server can then see at most the incoming payments, cannot steal the money, and cannot easily tell which transactions were outgoing. The picture the document had in mind was to hand out only the branch that is needed. What real wallet menus give out is the larger account as a whole.

xpub, ypub, zpub

Export the same account and one wallet gives you a string starting with xpub, another one starting with zpub. This is what is left of an attempt to fill the gap noted above, the missing address format, with the version bytes. SLIP-132 is the registry that collects these values.

PrefixVersion bytesAddress formatPath
xpub0x0488b21eP2PKH or P2SHm/44'/0'
ypub0x049d7cb2Nested SegWit P2WPKH-P2SHm/49'/0'
zpub0x04b24746Native SegWit P2WPKHm/84'/0'

Only the first 4 bytes change and the other 74 stay the same. Take the account key in the BIP-84 test vectors, zpub6rFR7y4Q2AijBEqT..., change the version to 0x0488b21e and re-encode it, and you get xpub6CatWdiZiodmUeTD.... That is the very xpub in the example of the output descriptors post. It is one key under two names.

The trouble is that wallets read the prefix differently. Electrum decides the address format from the prefix. In the version table in its source, xpub maps to standard and zpub to p2wpkh, and a standard wallet makes P2PKH addresses. Paste a native SegWit account into Electrum in its xpub form and it opens addresses starting with 1, showing a balance of zero. It is the same kind of failure as a seed restore that shows a zero balance. Bitcoin Core goes the other way and accepts only the xpub prefix for mainnet extended public keys, because DecodeExtPubKey in its source compares the version bytes against that one value. To give Core a zpub, you have to rewrite it as an xpub by swapping the version.

Descriptors were introduced to end this confusion. Write wpkh([73c5da0a/84h/0h/0h]xpub6Cat.../0/*) and the address format (wpkh), the fingerprint of the starting point and the path all sit on one line with the key. When two wallets can exchange descriptors, that is better than passing around a bare xpub.

Setting up a watch-only wallet

The steps are the same in every wallet. Take the account xpub or descriptor from the signing device, put it into a wallet on an online device, and match the address format.

Sparrow offers a Watch Only Wallet option on the screen where you pick the keystore type for a new wallet. First set the script type in the wallet settings (Native Segwit or whichever the original wallet used), then paste the xpub into that option. If you instead use Connected Hardware Wallet or Airgapped Hardware Wallet on the same screen, connecting the device directly or reading the file or QR it exports, the fingerprint and path come in along with the xpub. A wallet made that way holds no private keys either.

Electrum shows the master public key in the Information window under the Wallet menu of the signing wallet. On the online machine, create a new wallet with File, New/Restore, choose Standard wallet, then "Use public or private keys", and paste the key. When the wallet opens, a notice says it is a watching-only wallet. As noted above, Electrum picks the format from the prefix, so a native SegWit account goes in as a zpub.

Bitcoin Core uses a separate wallet that does not accept private keys, and the descriptors go into it. The example uses keys from the BIP-84 test vector seed (abandon eleven times, then about), which everyone knows. Do not use them for real; substitute your own descriptors.

bitcoin-cli -named createwallet wallet_name=watch disable_private_keys=true blank=true
bitcoin-cli -rpcwallet=watch importdescriptors '[
 {"desc":"wpkh([73c5da0a/84h/0h/0h]xpub6CatWdiZiodmUeTDp8LT5or8nmbKNcuyvz7WyksVFkKB4RHwCD3XyuvPEbvqAQY3rAPshWcMLoP2fMFMKHPJ4ZeZXYVUhLv1VMrjPC7PW6V/0/*)#afwvtk2s",
  "timestamp":1704067200,"active":true,"internal":false},
 {"desc":"wpkh([73c5da0a/84h/0h/0h]xpub6CatWdiZiodmUeTDp8LT5or8nmbKNcuyvz7WyksVFkKB4RHwCD3XyuvPEbvqAQY3rAPshWcMLoP2fMFMKHPJ4ZeZXYVUhLv1VMrjPC7PW6V/1/*)#vatdkr6g",
  "timestamp":1704067200,"active":true,"internal":true}
]'

The receiving branch (/0/*) and the change branch (/1/*) go in separately, with "internal":true on the change side. The eight characters after # are the descriptor checksum; get the checksum for your own descriptor with getdescriptorinfo. timestamp is a Unix time around the day the wallet was first created. Passing "now" skips the old blocks and the balance shows zero.

Whichever wallet you used, the last step is the same. Compare the first receiving address in the watch-only wallet, character by character, with the first address shown on the hardware wallet's screen. If they match, the xpub and the format went in correctly. For an account that has issued many addresses, the gap limit has to fit as well. A watch-only wallet also stops scanning after about twenty empty addresses in a row.

The device in the drawer signs

Press send in a watch-only wallet and it builds the transaction but cannot sign it. You export that unfinished transaction as a PSBT, carry it to the hardware wallet or offline computer, sign it there, bring it back and broadcast it. The watch-only wallet fills in the UTXOs for the transaction and the BIP-32 derivation path of each input, and the signing device follows those paths to find its own private keys. This round trip is the day-to-day running of cold storage, which keeps keys away from online devices.

Reason to hide it 1. Past and future are both visible

When an ordinary public key is exposed, one address is exposed. When an account xpub is exposed, every address the account has used and every address it will use are exposed. Balance, times and amounts received, spending detected through change addresses, all of it is visible. And it cannot be undone. Unlike a password, an xpub cannot be changed; it comes from the key itself, so once it leaks the account is someone else's to watch for good. The only remedy is to move the coins to another account or a new seed, and that move is visible to the same person.

So where you put an xpub matters. Paste it into a portfolio app or a tax calculation service and that company's server holds the whole account. Light wallets that query a server for addresses are worth thinking about too. An Electrum server does not receive the xpub and takes queries address by address, but when one connection asks about an account's addresses one after another, the server can tell they belong to one person. Connect to your own node and this problem goes away.

Reason to hide it 2. Together with one private key, it opens the whole account

The security section of BIP-32 names one weakness that is easy to miss. Knowing a parent extended public key plus the private key of any one normal child below it is equivalent to knowing the parent extended private key, and therefore every private key below it. That is why the document says extended public keys must be treated more carefully than regular public keys.

The reason lies in the formula for normal derivation. The private key of a normal child is the parent private key plus one value. That value comes from hashing the parent's chain code, its public key and the child number, all three of which are in the xpub. Subtract that value from the child's private key and out comes the parent's private key.

Here it is with the BIP-84 test vectors. The calculation used only two things: the account key zpub6rFR7y4Q... and the private key KyZpNDKn... of the first receiving address m/84'/0'/0'/0/0. Two subtractions, walking back up through the receiving-branch node to the account node, produced the account private key, and serialized again it matched the test vector's zprvAdG4iTXWBoAR... character for character. Deriving the private key of the change address m/84'/0'/0'/1/0 from that account key also matched the vector. Only one address's key had leaked, and both branches, every coin in that account, were open.

A single private key leaks on its own more often than you might think. Using a wallet's private key export to pull out one address's key and put it in another app, pasting one key into a sweep tool as with a paper wallet, handing it to someone who says "give me just that address's key and I'll get the coins out." Each looks like giving up one address holding a few coins. If that account's xpub has already gone somewhere, what was really given up is the whole account. In an account whose xpub you have handed out, do not export any individual private key. If you already have, treat the account as compromised and move the coins.

The damage has a boundary, because the account node 0' is hardened. A hardened child's key is computed from the parent's private key, not its public key, so the same subtraction cannot climb above it. BIP-32 states that this is the very reason hardened keys exist and why they are used at the account level. Even if one account's keys leak, the master key and other accounts stay safe. In a multisig setup, leaking one signer's xpub along with one private key below it hands over that signer's account key, but meeting the quorum still takes other signers' keys.

When a watch-only wallet is fooled

A watch-only wallet cannot steal money, but it can show you the wrong receiving address. If the online computer has malware, it can swap the xpub in the wallet file for the attacker's, or just replace the address on the screen. The wallet then shows a perfectly normal new address, and money sent there goes to the attacker. Before receiving a large amount, display that receiving address on the hardware wallet's screen and check that it is the same. This is why hardware wallets have an address display function.

Summary

  • An xpub is public information that builds every address in an account. It has no private key and cannot sign, but it carries neither the path nor the address format.
  • xpub, ypub and zpub are different prefixes on the same key. Check whether a wallet picks the format from the prefix, and exchange descriptors where you can.
  • After setting up a watch-only wallet, compare the first receiving address against the hardware wallet's screen.
  • Anyone you give an xpub to sees the account's past and future. A shared xpub cannot be taken back.
  • In an account whose xpub is in other hands, exporting even one private key on its own is the same as handing over the whole account.

An xpub is less dangerous than a private key, but far more dangerous than the public key of a single address. If the point of a watch-only wallet is to keep the keys in a drawer, the xpub should not be left lying around either.

Related