8

I was trying to learn working of bitcoin and trying to understand the working of bitcoin wallet.

I can see the behavior of how an HD wallet starts with an address and adds additional addresses later for receiving bitcoin.

I read bip-0032, but still am confused. If I regenerate a wallet from the seed (in another mobile phone), how the system will populate the wallet with all previous addresses containing a balance? Will it try to download the earlier blockchain and search it, or is there another way?

How do we locate a list of all addresses we created with the HD wallet? How can we verify the balance of those addresses?

I know that this question is going to receive down votes, please give me some ideas.

Also it would be helpful if somebody can share some links where I could get some help on clarifying doubts (not programming, general working).

1

Wallets don't actually have bitcoins, what the store are public and private keys. This link explains the basics.

Anyone who knows these pieces of info can use the bitcoins stored at that address. You don't even need a wallet!

Wallets provide a way to generate new addresses for each transaction and remember the associated private keys. They can also provide features so that if you need to recreate the wallet it can recreate for you the same addresses and associated private keys that it did before so that you can spend any bitcoins stored at an address that you created with the wallet.

  • This is only true if you considet a wallet to just represent a single balance. It's not true when you want it to maintain accounting information (transaction time, from/to whom, messages, ...), for example. In some settings, maintaining the accounting information may actually be more important than just keeping the correct balance spendable, because of legal or administrative reasons. – Pieter Wuille Mar 23 '16 at 6:20
1

The seed derivation works by increasing a "nonce" and deriving an address from that. So, by example, if we pretend that we have 1234 as our seed, then we could increase that seed by 1 for each address derivation. If we use a simple sha256 hash of that as our private key, then we get the following addresses:

NonceFinal SeedAddress
0 12341B3PHXB6g5YGLvzTkxrrPEdLbpvfEoC5Qi
1 12351NxaF9cYPL53fNTHMN6wtmq1w8bmHEe3gp
2 12361DPF9wFiuFowUK4NwJdbd2W9L59bPPnWNM
3 123713ehhpMndUoh5E4QcHJ7QSWwuKC7ySpxrL
4 123817CF5qesU9KXBHAsa9tEz1126kqSDy56tC
5 12391MfTGTiszztzzjzKajvS1Wc3TznqpfyPh7
6 124016t3QqxUVssdTAYUibdY8K4aCFYsP6pUWE

This is obviously oversimplified, and not the derivation that is used in most BIP32 implementations. As to how they are restored, you simply keep deriving addresses until you hit a bunch in a row that are unused. This is called a lookup gap, and is normally configurable.

  • 2
    Unrelated, but that table is amazing, I didn't know you could do that on SE. – fluffyponyza May 22 '16 at 18:15
  • This is a great reply, thank you. But to close the loop, it sounds like the bip44 protocol calls for a lookup on the blockchain of the public key for the deterministically generated key pairs until the configured lookup gap threshold is hit. – Bill Jan 24 '18 at 4:20
1

By no means am I an expert but I'm currently reading Mastering Bitcoin by Andreas M. Antonopoulos and I had this question too. Chapter 5 discusses in depth how HD wallets work and how they can restore addresses from a seed phrase and password/salt. The book mentions there are billions of possible addresses but not how your new wallet works out which of these addresses was actually being used in the past.

This question helped me get it straight in my head: How to find the wallet balance?

Wallet software will have a set number of addresses it generates and checks for a balance upon wallet initialization (maybe 50? this will vary and is configurable in some software). The wallet will probably include some logic like "look 50 addresses past the last used address found". You can design this to fit your need, for example if you were a merchant accepting payments on a busy website, you might need a higher bound, in case 50 customers in a row generate a payment address but then don't complete checkout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.