Bitcoin Forum

Other => Beginners & Help => Topic started by: BusyBeaverHP on April 22, 2014, 05:11:41 PM



Title: Help me understand how private keys work in wallet.dat
Post by: BusyBeaverHP on April 22, 2014, 05:11:41 PM
I read that wallet.dat in Bitcoin Core has 100 private keys in it.

I also know that everytime you spend, a private key is used up by mechanics of sending and reallocation. For simple assumption, suppose that I've used my 99th private key, what would actually happen once the keypool refreshes itself?

Example: Suppose I have 2 BTC stored in a wallet.dat that already has 99 transactions, what would happen when the keypool refresh itself?


Title: Re: Help me understand how private keys work in wallet.dat
Post by: cp1 on April 22, 2014, 05:13:02 PM
It keeps 100 unused keys.  So every time you use one it will generate another one.  You should back it up frequently.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DannyHamilton on April 22, 2014, 05:14:59 PM
I read that wallet.dat in Bitcoin Core has 100 private keys in it.

Starts with a default of 100 private keys.  Additional keys are generated as needed.

I also know that everytime you spend, a private key is used up by mechanics of sending and reallocation. For simple assumption, suppose that I've used my 99th private key, what would actually happen once the keypool refreshes itself?

Example: Suppose I have 2 BTC stored in a wallet.dat that already has 99 transactions, what would happen when the keypool refresh itself?

The keypool remains refreshed all the time.  It acts like a queue.  Each time the software needs a new address and private key (if you request a new receiving address, or if the software needs an address for the change from the transaction), it takes the next one from the front of the queue.  At the same time, it generates a new address and private key and adds it to the back of the queue.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: odolvlobo on April 22, 2014, 05:46:24 PM
It keeps 100 unused keys.  So every time you use one it will generate another one.  You should back it up frequently.

You don't need to back it up frequently. You only need to back it up as often as every 100 keys. One key is used every time you send bitcoins and one is used every time you allocate a new key to receive bitcoins.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DannyHamilton on April 22, 2014, 05:51:31 PM
It keeps 100 unused keys.  So every time you use one it will generate another one.  You should back it up frequently.

You don't need to back it up frequently.

I suppose that depends on how many transactions you are sending, how many receiving addresses you are generating, and what you mean when you say "frequently".

You only need to back it up as often as every 100 keys. One key is used every time you send bitcoins and one is used every time you allocate a new key to receive bitcoins.

While this is technically true, I tend to be concerned about the possibility that the most recent backup is unusable (for example, backing up a corrupted file before realizing that it is corrupted).

Instead, I prefer to back up every 25 keys, and keep the 3 most recent backups.  If you use a new address for every transaction (like you should), and you average 4 transactions per day, then backing up weekly would make sense.  Some people might use the word "frequent" to describe weekly backups.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: blacksails on April 22, 2014, 06:52:31 PM
Is this something that a "normal" user needs to know, or is this more advanced stuff?
And why would you need 100 unused keys when it generates a new one when you need it anyway?


Title: Re: Help me understand how private keys work in wallet.dat
Post by: cp1 on April 22, 2014, 07:07:59 PM
Is this something that a "normal" user needs to know, or is this more advanced stuff?
And why would you need 100 unused keys when it generates a new one when you need it anyway?

You'd have to back it up every transaction otherwise.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DannyHamilton on April 22, 2014, 07:11:51 PM
Is this something that a "normal" user needs to know, or is this more advanced stuff?
And why would you need 100 unused keys when it generates a new one when you need it anyway?

A "normal" user of the Bitcoin-Qt (or Bitcoin Core) wallet needs to know that they have to backup their wallet regularly.

Otherwise, they will lose all their bitcoins if their hard drive crashes (or their computer is stolen, burns in a fire, or is destroyed in a flood, tornado, or earthquake).

Since "regularly" can mean different things to different people, it would be good for a "normal" user of the Bitcoin-Qt (or Bitcoin Core) wallet to keep track of how many new addresses they've used (including change addresses) and backup their wallet every 25 addresses or so. Then keep the most recent 3 backups in 3 separate secure locations.

If you don't ever store enough bitcoins to care whether they are permanently lost, then the backups are not important.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: blacksails on April 22, 2014, 07:36:45 PM
Is this something that a "normal" user needs to know, or is this more advanced stuff?
And why would you need 100 unused keys when it generates a new one when you need it anyway?

A "normal" user of the Bitcoin-Qt (or Bitcoin Core) wallet needs to know that they have to backup their wallet regularly.

Otherwise, they will lose all their bitcoins if their hard drive crashes (or their computer is stolen, burns in a fire, or is destroyed in a flood, tornado, or earthquake).

Since "regularly" can mean different things to different people, it would be good for a "normal" user of the Bitcoin-Qt (or Bitcoin Core) wallet to keep track of how many new addresses they've used (including change addresses) and backup their wallet every 25 addresses or so. Then keep the most recent 3 backups in 3 separate secure locations.

If you don't ever store enough bitcoins to care whether they are permanently lost, then the backups are not important.
Okay, that cleared it up pretty good. Thank you! :)


Title: Re: Help me understand how private keys work in wallet.dat
Post by: boumalo on April 22, 2014, 08:11:01 PM
It is still a bit complicated to safely keep bitcoins on your computer long term


Title: Re: Help me understand how private keys work in wallet.dat
Post by: cp1 on April 22, 2014, 08:14:16 PM
If you use a deterministic wallet such as electrum then you don't need to worry about how many keys you've used.  It will keep track of as many as you want using a seed phrase.  Just write down that phrase and it's all you need.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: far9090 on April 23, 2014, 09:57:35 AM
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CDYQFjAB&url=https%3A%2F%2Fen.bitcoin.it%2Fwiki%2FHow_to_import_private_keys&ei=fo5XU-uiFYuP5ASBzoDABg&usg=AFQjCNEcOmZQ89bxEmtCKgfzlWTsIs7s9w&bvm=bv.65177938,d.bGE


Title: Re: Help me understand how private keys work in wallet.dat
Post by: Tammy Chan on April 23, 2014, 03:23:58 PM
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CDYQFjAB&url=https%3A%2F%2Fen.bitcoin.it%2Fwiki%2FHow_to_import_private_keys&ei=fo5XU-uiFYuP5ASBzoDABg&usg=AFQjCNEcOmZQ89bxEmtCKgfzlWTsIs7s9w&bvm=bv.65177938,d.bGE

This messy thing is indeed just https://en.bitcoin.it/wiki/How_to_import_private_keys, and it doesn't really help answering OP's question.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: Tammy Chan on April 23, 2014, 03:26:20 PM
If you use a deterministic wallet such as electrum then you don't need to worry about how many keys you've used.  It will keep track of as many as you want using a seed phrase.  Just write down that phrase and it's all you need.

Exactly.

And just a side note, you could set the keypool size to whatever you want with "-keypool=<n>".
So, if you don't want to make backup so "frequently", you could use the argument to set a larger keypool.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DeathAndTaxes on April 23, 2014, 03:36:59 PM
They client doesn't wait until the keypool is exhausted before refreshing.  It refreshes the keypool whenever possible if the current size is less than the preset seize. 

If the wallet is not encrypted the keypool refreshes each time a new key is used.
If the wallet is locked (encrypted) the client will refresh the keypool the next time the wallet is unlocked.  
This means in situations where the wallet is not routinely unlocked (like a server watching wallet) they keypool will eventually be exhausted.  

You can get the current size of the keypool by the rpc command getinfo.

Eventually the Bitcoin-core wallet will probably use deterministic wallets making the concept of a keypool obsolete.



Title: Re: Help me understand how private keys work in wallet.dat
Post by: BusyBeaverHP on April 24, 2014, 06:45:57 AM
With the keypool queuing 100 unused private keys after every transaction, does this mean that a wallet will grow in size (because of used private/public keypairs) after every transaction?

One wallet example:

1. Received BTC (Generated new request code)
Wallet total: 1 Pub - 1 Priv - 100 unused Priv

2. Spent BTC
Wallet total: 2 Pub - 2 Priv - 100 unused Priv

3.  Received BTC (Generated new request code)
Wallet total: 3 Pub - 3 Priv - 100 unused Priv

4. Spent BTC
Wallet total: 4 Pub - 4 Priv - 100 unused Priv

... and so on...

Is this correct?


Title: Re: Help me understand how private keys work in wallet.dat
Post by: odolvlobo on April 24, 2014, 03:29:26 PM
With the keypool queuing 100 unused private keys after every transaction, does this mean that a wallet will grow in size (because of used private/public keypairs) after every transaction?
...

Is this correct?

That is correct.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: deepceleron on April 30, 2014, 06:40:32 AM
If you plan on making a long-term backup, for the purpose of safeguard against data loss, after encrypting with a passphrase, you might consider starting Bitcoin once with a large keypool option such as bitcoin-qt -keypool=2000. This will fill the wallet with future keys that will keep your backup from becoming obsolete for a long time.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: BusyBeaverHP on May 05, 2014, 11:36:21 PM
If you plan on making a long-term backup, for the purpose of safeguard against data loss, after encrypting with a passphrase, you might consider starting Bitcoin once with a large keypool option such as bitcoin-qt -keypool=2000. This will fill the wallet with future keys that will keep your backup from becoming obsolete for a long time.
Good idea. Do I change it in the shortcut properties, or somewhere within the program itself?

Another question: If I kept my wallet in cold storage, and I deposit more coins to a known address in that cold storage wallet, does that change the number of keys in use? How would that affect the existing unused keypool?


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DeathAndTaxes on May 06, 2014, 02:27:24 AM
If you plan on making a long-term backup, for the purpose of safeguard against data loss, after encrypting with a passphrase, you might consider starting Bitcoin once with a large keypool option such as bitcoin-qt -keypool=2000. This will fill the wallet with future keys that will keep your backup from becoming obsolete for a long time.
Good idea. Do I change it in the shortcut properties, or somewhere within the program itself?

Neither since you only need to set it once.  In windows the easiest way is probably use the command prompt and type the command exactly as shown above.  Once the keypool is set loading it in the future won't unset it so there is no need to add it to a shortcut.  Understand it can take a while to generate a large number of keys and there will be no progress indicated by the system so just be patient eventually once the keypool is expanded the client will load normally.

Quote
Another question: If I kept my wallet in cold storage, and I deposit more coins to a known address in that cold storage wallet, does that change the number of keys in use? How would that affect the existing unused keypool?

It wouldn't use a new address thus it won't change the number of available keys in the keypool.




Title: Re: Help me understand how private keys work in wallet.dat
Post by: BusyBeaverHP on May 06, 2014, 02:44:15 AM
I just opened Bitcoin Core in CMD with bitcoin-qt -keypool=10000. The time it took from launch to UI, with regard to the 10,000 key pool, wasn't noticable.

How do I verify that my wallet has 10,000 keys in its pool? Should I notice an increase in the my wallet filesize?

It should be noted that I opened Bitcoin Core with an existing wallet with roughly 0.16 BTC in it.

So did this expand my existing wallet's keypool to 10,000?


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DeathAndTaxes on May 06, 2014, 03:49:52 AM
from the debug console type getinfo to get some stats including the number of keys currently in the keypool.  I forgot that if your wallet is encrypted the client will wai until the first time you unlock it (like sending coins) to expand the keypool.  You can force the wallet to unlock to generate the additional keys now.  You can do that from the debug console as well with walletpassphrase <your passphrase> <time to remain unlocked>.  I would set time to remain unlocked to something like 600 seconds to ensure sufficient time to generate all the keys.   Once it completes you can lock the wallet early if neccessary with lockwallet IIRC (type help in debug console to verify exact command).


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DannyHamilton on May 07, 2014, 11:25:27 AM
- snip -
I forgot that if your wallet is encrypted the client will wai until the first time you unlock it (like sending coins) to expand the keypool.
- snip -

This is new behavior in 0.9.x right?


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DeathAndTaxes on May 07, 2014, 01:59:44 PM
- snip -
I forgot that if your wallet is encrypted the client will wai until the first time you unlock it (like sending coins) to expand the keypool.
- snip -

This is new behavior in 0.9.x right?

I think it has always been this way as it doesn't write unecrypted keys to an encrypted wallet.  Without the wallet passphrase that is what it would have to do in order to expand the keypool.


Title: Re: Help me understand how private keys work in wallet.dat
Post by: DannyHamilton on May 07, 2014, 02:10:56 PM
- snip -
I forgot that if your wallet is encrypted the client will wai until the first time you unlock it (like sending coins) to expand the keypool.
- snip -
This is new behavior in 0.9.x right?
I think it has always been this way as it doesn't write unecrypted keys to an encrypted wallet.  Without the wallet passphrase that is what it would have to do in order to expand the keypool.

Duh.  I really didn't think that one through.  Thanks.