This commit adds the ability to backup and restore wallets using the HD Master Seed defined in BIP 32.
Please feel free to provide any feedback or comments. I always wanted this functionality in the reference client and thought others would find it useful as well. I tried to make this version a fully functional change that minimizes impact to the rest of the client, but if other approaches would be better, etc. please let me know.
This change enables wallets to be saved offline using a short key instead of requiring a prior wallet.dat file to restore. The ability to backup and restore from a key is available in other clients, but not in Bitcoin core clients. Bitcoin core exports the HD Master Extended Key in ‘dumpwallet’ and this key can be used to manually recover addresses, however the core client requires the HD Master Seed to function and this is only found in the wallet.dat file itself.
To implement recovery this commit adds a new “Encrypt Wallet Advanced” menu option and dialog window. Recovery during wallet encryption was the least disruptive method because normal wallet encryption already generates a new HD Master Seed and inserts it into the wallet to use from that point forward. The recovery process implemented instead inserts a manually entered HD Master Seed to use instead of the auto generated seed. Once the recovered seed is added a simple ‘-rescan=1’ scans the blockchain and repopulates the wallet.
The Encrypted Wallet Advanced dialog offers three options:
- Standard wallet encryption – This option encrypts the wallet with a new auto generated seed, this is identical to normal wallet encryption.
- Recover wallet – This option allows the user to enter a backup seed to recover the wallet and then encrypts the wallet as normal
- Manually generate seed – This option allows the user to roll dice to create a new seed which guarantees full entropy and then encrypts the wallet as normal
The dialog window provides user instructions depending on the options selected. Keys are exported and imported in base58 format. The -rescan command was also modified to optionally force rescanning from the genesis block. This was needed because by default -rescan only scans from the time the wallet.dat file was created, which for newly created wallet.dat files would skip all prior addresses trying to be recovered.
Below are files changed or added along with a description of the changes. I also added comments in the code where I thought it would help.
Changes by file
New dialog window and methods
- A src/qt/encryptwalletadvanceddialog.cpp
- A src/qt/encryptwalletadvanceddialog.h
- A src/qt/forms/encryptwalletadvanceddialog.ui
Update wallet routines to encrypt using user entered backup seed
Update ‘-rescan’ to have ‘-rescan=1’ force a rescan from the genesis block
- M src/wallet/wallet.cpp
- M src/wallet/wallet.h
Export the HD Master Seed in ‘dumpwallet’
- M src/wallet/rpcdump.cpp
Add “Encrypt Wallet Advanced” menu option and associated plumbing
- M src/qt/bitcoingui.cpp
- M src/qt/bitcoingui.h
- M src/qt/walletframe.cpp
- M src/qt/walletframe.h
- M src/qt/walletview.cpp
- M src/qt/walletview.h
Pass recovery seed from dialog window to wallet encryption routines
- M src/qt/walletmodel.cpp
- M src/qt/walletmodel.h
New file additions to make
- M contrib/bitcoin-qt.pro
- M src/Makefile.qt.include
- M src/qt/CMakeLists.txt