detect empty wallet or create new wallet
From a comment in https://github.com/dtr-org/unit-e/pull/37
[We are] trying to use as much bitcoin functionality as possible. Bitcoins CWallet already has methods to set the master key but I do suspect it was not their intention for them to be used /after/ a wallet has been created already. When the node comes up it automatically creates a wallet. Upon creation the wallet is preseeded with a list of reserve keys. It is thus tricky to distinguish a newly created wallet from a wallet which is already in use. Nevertheless you could import a new masterkey into an already used wallet, which is why setting a new masterkey will automatically backup your existing wallet (although in most cases this will be an empty wallet).
Ways to solve this:
- create and open a completely new wallet
- detect whether current wallet is used, if used -> fail, of not used -> replace master key
I'm in favor of the second solution, with the addition of a "force" argument to importmasterkey which will replace the key unconditionally.
We could detect that a wallet is unused if the mapAddressBook collection is empty, for example.
Related: https://github.com/bitcoin/bitcoin/pull/15454#
Also related: https://github.com/bitcoin/bitcoin/commit/7687f7873b75c3cbdfa15ab570211dc39d24ab80