Hi guys, I may be missing something crucial here, but I'm sure you guys will correct me soon enough.
HD wallets let a user worry less about making a new backup for change addresses because the change addresses are all deterministically generatable from the seed. That let me to thinking about restoring from an hd backup. Say you only have the seed and you've lost everything else. Your wallet needs to know which addressess are yours when it loads the blockchain, it should show you your balance based on how many addresses on the blockchain have spendable coins and belong to you. But here's the question, how deep in the tree of generatable addresses should it go when looking? How can it know beforehand (on a restore) how deep in the tree you went in creating addresses for yourself.
For example, consider receiving an address as you load the blockchain, you ask yourself "is this my address"? No. Is this my first change address? No. Is this my second change address? No. ... When do you stop and say, "okay, this address isn't in my tree of addresses"?
Thanks in advance for the insight!
Honestly, I have no idea how the HD wallets handle recovery from a seed, so take everything I say in the post purely as a guess or as "the way I think I would probably do it if I were writing the software".
The way you seem to be looking at it seems to be backwards. If I understand correctly you're assuming that you start with the first address in the first block in the blockchain and check it against every possible address that might be generated from the seed. Then you move to the next address in the blockchain and repeat the process, and continue until the entire blockchain has been loaded/processed.
If I were writing the software, I think I'd probably process the entire blockchain as if I didn't have any addresses at all, building up the complete UTXO list.
Next, I'd collect a list of all unique addresses in the UTXO list.
Then, I'd generate the first candidate address from my seed, and check to see if the address exists in the UTXO list. If so, then I've found an address that has some bitcoins associated with it.
I'd continue to generate each possible address from the seed and check for "exists in the UTXO list" until I've encountered some predefined gap with no balances (perhaps 5000 addresses?).
So, if I generate from the seed "gap" number of consecutive candidate addresses without having found any of them in the UTXO, I'd assume I've found all addresses with a balance and I'd be done.
I might ask the user if the resulting balance appears to be correct. If they answer "yes", then the wallet is completely recovered. If they answer "no", then I might continue to search for a larger gap.