Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: marley on June 18, 2014, 02:54:01 PM



Title: Bitcoin wallet doesn't store public keys
Post by: marley on June 18, 2014, 02:54:01 PM
Why the qt bitcoin client doesn't store the public keys associated with the private keys?
Sure it can be computed from the private key alone, but it is a costly operation to do every single time when it could just store a few extra bytes in the wallet.
Any other good reason for this behaviour? Anyone knows if every wallet does the same?

Thanks,


Title: Re: Bitcoin wallet doesn't store public keys
Post by: wumpus on June 18, 2014, 03:13:13 PM
Where did you get this information? It's wrong.
Bitcoin Core's wallet does store the public keys.
See here for the code that loads keys: https://github.com/bitcoin/bitcoin/blob/master/src/walletdb.cpp#L409


Title: Re: Bitcoin wallet doesn't store public keys
Post by: marley on June 20, 2014, 12:32:39 PM
Where did you get this information? It's wrong.
Bitcoin Core's wallet does store the public keys.
See here for the code that loads keys: https://github.com/bitcoin/bitcoin/blob/master/src/walletdb.cpp#L409

My point is that it appears that bitcoin client is not using this "stored" public keys value, because every time it needs to check the public key associated with a private key it just sets the private key to a EC_KEY openssl object and then asks for its public key. What openssl does is to compute the public key related to the private key, so if bitcoin is storing the public keys it's being useless.
Also the CKey class that is used in the wallet to persist the keys to storage only represents private key data, not public keys, so every time it needs to know the public key, it have to go through openssl mechanism.
You can easily check this by modifying the code to store a key pair where the public key is not related to the private key (just for demonstrations sake), so what it will do is actually store only the private key, and query for its corresponding public key, which will result in the correct private/public keys. So, by this behaviour we see that it's not actually using the stored public keys.
I'm just wondering why this happens.