<p>You've hit upon an important issue, and one that there isn't a perfect solution to.</p>

<p>Your thin client (most clients that use deterministic wallets are thin clients, but not all) goes through the following process:</p>

<ol>
<li>Generate an address.</li>
<li>Look at the blockchain, and see if it has a balance.</li>
<li>Repeat with next address, unless the addresses we've checked have had a balance of zero for a long time.</li>
</ol>

<p>The number of empty addresses you're willing to check before giving up is called the <a href="https://bitcoin.stackexchange.com/questions/4247/what-is-the-gap-limit-in-electrum">gap limit</a>.</p>

<p>The gap limit can't be too low, or there's a possibility that someone will backup from a wallet seed and not see all of their money. (If this happens to you, increase the gap limit and try again.) It also can't be too high, or you'll waste resources on the servers handling Electrum queries.</p>

<p>Your suggested scenario is actually the easiest to solve, because if each of those addresses has a transaction associated with it in the blockchain, the client knows that it showed that address to you at some point, and will keep searching.</p>

CC BY-SA 3.0
2014-10-21T05:32:08.280Z
I suppose a full client could handle a much higher gap limit, since it can check the block chain for an address itself, without querying an external server.
Nate Eldredge
CC BY-SA 3.0
2017-09-24T06:34:07.507Z
So in theory it in possible an existing address is never generated even at high limits, so even if you knew the exact address you couldn't regenerate the private key?
jiggunjer
CC BY-SA 3.0
2017-09-24T14:36:00.813Z
@jiggunjer No. Set the gap limit high enough, and it will generate every address the client has even shown you, and more.
Nick ODell
CC BY-SA 3.0
2017-09-24T15:24:18.153Z
@Nick_Odell how do you know? The generation is a random draw from an infinite set. So I don't see how anyone can *guarantee* a certain address will be drawn even if you keep drawing 24/7.
jiggunjer
CC BY-SA 3.0
2017-09-24T17:30:22.897Z
@jiggunjer It isn't drawn randomly - it's drawn deterministically from a random number.
Nick ODell
CC BY-SA 3.0
2017-09-24T18:12:49.040Z
Now it makes sense. Thanks. I had a different math definition of deterministic.
jiggunjer
CC BY-SA 3.0
2014-10-21T04:54:26.330Z
2014-10-21T05:02:05.517Z
2017-04-13T12:47:32.940Z
Nick ODell
<p>Say I backup the "seed/key" for a deterministic wallet. I then create 10,000 transactions, resulting in 10,000 new addresses being generated.</p>

<p>Say I then lose the wallet file and I restore from the "seed/key". How does the deterministic wallet know it has to generate 10,000 addresses to get the full balance?</p>

<deterministic-wallets>
How do deterministic wallets know how many keys to generate?
bitboy99921232323
# How do deterministic wallets know how many keys to generate?

(Asked by: username_0 on 2014-10-21)

Say I backup the "seed/key" for a deterministic wallet. I then create 10,000 transactions, resulting in 10,000 new addresses being generated.

Say I then lose the wallet file and I restore from the "seed/key". How does the deterministic wallet know it has to generate 10,000 addresses to get the full balance?

**Tags:** deterministic-wallets


---

## 1 answers

### Answer by username_1 on 2014-10-21. Score: 9

You've hit upon an important issue, and one that there isn't a perfect solution to.

Your thin client (most clients that use deterministic wallets are thin clients, but not all) goes through the following process:

1. Generate an address.
2. Look at the blockchain, and see if it has a balance.
3. Repeat with next address, unless the addresses we've checked have had a balance of zero for a long time.

The number of empty addresses you're willing to check before giving up is called the [gap limit](https://bitcoin.stackexchange.com/questions/4247/what-is-the-gap-limit-in-electrum).

The gap limit can't be too low, or there's a possibility that someone will backup from a wallet seed and not see all of their money. (If this happens to you, increase the gap limit and try again.) It also can't be too high, or you'll waste resources on the servers handling Electrum queries.

Your suggested scenario is actually the easiest to solve, because if each of those addresses has a transaction associated with it in the blockchain, the client knows that it showed that address to you at some point, and will keep searching.

> **Comments:**
> **username_2**: I suppose a full client could handle a much higher gap limit, since it can check the block chain for an address itself, without querying an external server.  
> **username_3**: So in theory it in possible an existing address is never generated even at high limits, so even if you knew the exact address you couldn't regenerate the private key?  
> **username_1**: @username_3 No. Set the gap limit high enough, and it will generate every address the client has even shown you, and more.  
> **username_3**: @Nick_Odell how do you know? The generation is a random draw from an infinite set. So I don't see how anyone can *guarantee* a certain address will be drawn even if you keep drawing 24/7.  
> **username_1**: @username_3 It isn't drawn randomly - it's drawn deterministically from a random number.  
> **username_3**: Now it makes sense. Thanks. I had a different math definition of deterministic.  

---

