HD Wallets: add a threshold and pre-generate new keys, if keys are used

Here are two patches for the hdw-alpha branch, which enables a resync of the blockchain for HD wallets.